blob: 109edfe582f42f65875508fb638eaf2239d1bd15 [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();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001751 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1752 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001753
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001754 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001755 ASSERT_EQ(mReader->start(), OK);
1756
1757 // Since this test is run on a real device, all the input devices connected
1758 // to the test device will show up in mReader. We wait for those input devices to
1759 // show up before beginning the tests.
1760 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1761 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1762 }
1763
1764 virtual void TearDown() override {
1765 ASSERT_EQ(mReader->stop(), OK);
1766 mTestListener.clear();
1767 mFakePolicy.clear();
1768 }
1769};
1770
1771TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1772 // An invalid input device that is only used for this test.
1773 class InvalidUinputDevice : public UinputDevice {
1774 public:
1775 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1776
1777 private:
1778 void configureDevice(int fd, uinput_user_dev* device) override {}
1779 };
1780
1781 const size_t numDevices = mFakePolicy->getInputDevices().size();
1782
1783 // UinputDevice does not set any event or key bits, so InputReader should not
1784 // consider it as a valid device.
1785 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1786 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1787 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1788 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1789
1790 invalidDevice.reset();
1791 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1792 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1793 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1794}
1795
1796TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1797 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1798
1799 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1800 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1801 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1802 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1803
1804 // Find the test device by its name.
1805 std::vector<InputDeviceInfo> inputDevices;
1806 mReader->getInputDevices(inputDevices);
1807 InputDeviceInfo* keyboardInfo = nullptr;
1808 const char* keyboardName = keyboard->getName();
1809 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1810 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1811 keyboardInfo = &inputDevices[i];
1812 break;
1813 }
1814 }
1815 ASSERT_NE(keyboardInfo, nullptr);
1816 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1817 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1818 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1819
1820 keyboard.reset();
1821 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1822 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1823 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1824}
1825
1826TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1827 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1828 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1829
1830 NotifyConfigurationChangedArgs configChangedArgs;
1831 ASSERT_NO_FATAL_FAILURE(
1832 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001833 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001834 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1835
1836 NotifyKeyArgs keyArgs;
1837 keyboard->pressAndReleaseHomeKey();
1838 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1839 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001840 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001841 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001842 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1843 prevTimestamp = keyArgs.eventTime;
1844
1845 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1846 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001847 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001848 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1849}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850
Arthur Hungaab25622020-01-16 11:22:11 +08001851// --- TouchProcessTest ---
1852class TouchIntegrationTest : public InputReaderIntegrationTest {
1853protected:
1854 static const int32_t FIRST_SLOT = 0;
1855 static const int32_t SECOND_SLOT = 1;
1856 static const int32_t FIRST_TRACKING_ID = 0;
1857 static const int32_t SECOND_TRACKING_ID = 1;
1858 const std::string UNIQUE_ID = "local:0";
1859
1860 virtual void SetUp() override {
1861 InputReaderIntegrationTest::SetUp();
1862 // At least add an internal display.
1863 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1864 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
1865 ViewportType::VIEWPORT_INTERNAL);
1866
1867 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1868 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1869 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1870 }
1871
1872 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1873 int32_t orientation, const std::string& uniqueId,
1874 std::optional<uint8_t> physicalPort,
1875 ViewportType viewportType) {
1876 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1877 physicalPort, viewportType);
1878 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1879 }
1880
1881 std::unique_ptr<UinputTouchScreen> mDevice;
1882};
1883
1884TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1885 NotifyMotionArgs args;
1886 const Point centerPoint = mDevice->getCenterPoint();
1887
1888 // ACTION_DOWN
1889 mDevice->sendDown(centerPoint);
1890 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1891 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1892
1893 // ACTION_MOVE
1894 mDevice->sendMove(centerPoint + Point(1, 1));
1895 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1897
1898 // ACTION_UP
1899 mDevice->sendUp();
1900 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1901 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1902}
1903
1904TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1905 NotifyMotionArgs args;
1906 const Point centerPoint = mDevice->getCenterPoint();
1907
1908 // ACTION_DOWN
1909 mDevice->sendDown(centerPoint);
1910 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1911 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1912
1913 // ACTION_POINTER_DOWN (Second slot)
1914 const Point secondPoint = centerPoint + Point(100, 100);
1915 mDevice->sendSlot(SECOND_SLOT);
1916 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1917 mDevice->sendDown(secondPoint + Point(1, 1));
1918 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1919 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1920 args.action);
1921
1922 // ACTION_MOVE (Second slot)
1923 mDevice->sendMove(secondPoint);
1924 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1926
1927 // ACTION_POINTER_UP (Second slot)
1928 mDevice->sendUp();
1929 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1930 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1931 args.action);
1932
1933 // ACTION_UP
1934 mDevice->sendSlot(FIRST_SLOT);
1935 mDevice->sendUp();
1936 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1937 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1938}
1939
1940TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1941 NotifyMotionArgs args;
1942 const Point centerPoint = mDevice->getCenterPoint();
1943
1944 // ACTION_DOWN
1945 mDevice->sendDown(centerPoint);
1946 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1947 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1948
1949 // ACTION_POINTER_DOWN (Second slot)
1950 const Point secondPoint = centerPoint + Point(100, 100);
1951 mDevice->sendSlot(SECOND_SLOT);
1952 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1953 mDevice->sendDown(secondPoint);
1954 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1955 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1956 args.action);
1957
1958 // ACTION_MOVE (Second slot)
1959 mDevice->sendMove(secondPoint + Point(1, 1));
1960 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1962
1963 // Send MT_TOOL_PALM, which indicates that the touch IC has determined this to be a grip event.
1964 // Expect to receive ACTION_CANCEL, to abort the entire gesture.
1965 mDevice->sendToolType(MT_TOOL_PALM);
1966 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1967 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, args.action);
1968
1969 // ACTION_POINTER_UP (Second slot)
1970 mDevice->sendUp();
1971
1972 // ACTION_UP
1973 mDevice->sendSlot(FIRST_SLOT);
1974 mDevice->sendUp();
1975
1976 // Expect no event received after abort the entire gesture.
1977 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1978}
1979
Michael Wrightd02c5b62014-02-10 15:10:22 -08001980// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001981class InputDeviceTest : public testing::Test {
1982protected:
1983 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001984 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001985 static const int32_t DEVICE_ID;
1986 static const int32_t DEVICE_GENERATION;
1987 static const int32_t DEVICE_CONTROLLER_NUMBER;
1988 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001989 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001990
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001991 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001992 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001993 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001994 FakeInputReaderContext* mFakeContext;
1995
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001996 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001997
Prabir Pradhan28efc192019-11-05 01:10:04 +00001998 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001999 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002000 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002001 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002002 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2003
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002004 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002005 InputDeviceIdentifier identifier;
2006 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002007 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002008 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2009 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002010 }
2011
Prabir Pradhan28efc192019-11-05 01:10:04 +00002012 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002013 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002014 delete mFakeContext;
2015 mFakeListener.clear();
2016 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002017 }
2018};
2019
2020const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002021const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002022const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002023const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2024const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
2025const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
2026 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002027const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002028
2029TEST_F(InputDeviceTest, ImmutableProperties) {
2030 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002031 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002032 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002033}
2034
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002035TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2036 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002037}
2038
Michael Wrightd02c5b62014-02-10 15:10:22 -08002039TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2040 // Configuration.
2041 InputReaderConfiguration config;
2042 mDevice->configure(ARBITRARY_TIME, &config, 0);
2043
2044 // Reset.
2045 mDevice->reset(ARBITRARY_TIME);
2046
2047 NotifyDeviceResetArgs resetArgs;
2048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2049 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2050 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2051
2052 // Metadata.
2053 ASSERT_TRUE(mDevice->isIgnored());
2054 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2055
2056 InputDeviceInfo info;
2057 mDevice->getDeviceInfo(&info);
2058 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002059 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002060 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2061 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2062
2063 // State queries.
2064 ASSERT_EQ(0, mDevice->getMetaState());
2065
2066 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2067 << "Ignored device should return unknown key code state.";
2068 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2069 << "Ignored device should return unknown scan code state.";
2070 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2071 << "Ignored device should return unknown switch state.";
2072
2073 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2074 uint8_t flags[2] = { 0, 1 };
2075 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2076 << "Ignored device should never mark any key codes.";
2077 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2078 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2079}
2080
2081TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2082 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002083 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002084
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002085 FakeInputMapper& mapper1 =
2086 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002087 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2088 mapper1.setMetaState(AMETA_ALT_ON);
2089 mapper1.addSupportedKeyCode(AKEYCODE_A);
2090 mapper1.addSupportedKeyCode(AKEYCODE_B);
2091 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2092 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2093 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2094 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2095 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002096
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002097 FakeInputMapper& mapper2 =
2098 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002099 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100
2101 InputReaderConfiguration config;
2102 mDevice->configure(ARBITRARY_TIME, &config, 0);
2103
2104 String8 propertyValue;
2105 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2106 << "Device should have read configuration during configuration phase.";
2107 ASSERT_STREQ("value", propertyValue.string());
2108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002109 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2110 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002111
2112 // Reset
2113 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002114 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2115 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002116
2117 NotifyDeviceResetArgs resetArgs;
2118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2119 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2120 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2121
2122 // Metadata.
2123 ASSERT_FALSE(mDevice->isIgnored());
2124 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2125
2126 InputDeviceInfo info;
2127 mDevice->getDeviceInfo(&info);
2128 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002129 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002130 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2131 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2132
2133 // State queries.
2134 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2135 << "Should query mappers and combine meta states.";
2136
2137 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2138 << "Should return unknown key code state when source not supported.";
2139 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2140 << "Should return unknown scan code state when source not supported.";
2141 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2142 << "Should return unknown switch state when source not supported.";
2143
2144 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2145 << "Should query mapper when source is supported.";
2146 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2147 << "Should query mapper when source is supported.";
2148 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2149 << "Should query mapper when source is supported.";
2150
2151 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2152 uint8_t flags[4] = { 0, 0, 0, 1 };
2153 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2154 << "Should do nothing when source is unsupported.";
2155 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2156 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2157 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2158 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2159
2160 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2161 << "Should query mapper when source is supported.";
2162 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2163 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2164 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2165 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2166
2167 // Event handling.
2168 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002169 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002170 mDevice->process(&event, 1);
2171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002172 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2173 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002174}
2175
Arthur Hung2c9a3342019-07-23 14:18:59 +08002176// A single input device is associated with a specific display. Check that:
2177// 1. Device is disabled if the viewport corresponding to the associated display is not found
2178// 2. Device is disabled when setEnabled API is called
2179TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002180 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002181
2182 // First Configuration.
2183 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2184
2185 // Device should be enabled by default.
2186 ASSERT_TRUE(mDevice->isEnabled());
2187
2188 // Prepare associated info.
2189 constexpr uint8_t hdmi = 1;
2190 const std::string UNIQUE_ID = "local:1";
2191
2192 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2193 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2194 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2195 // Device should be disabled because it is associated with a specific display via
2196 // input port <-> display port association, but the corresponding display is not found
2197 ASSERT_FALSE(mDevice->isEnabled());
2198
2199 // Prepare displays.
2200 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2201 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2202 ViewportType::VIEWPORT_INTERNAL);
2203 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2204 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2205 ASSERT_TRUE(mDevice->isEnabled());
2206
2207 // Device should be disabled after set disable.
2208 mFakePolicy->addDisabledDevice(mDevice->getId());
2209 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2210 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2211 ASSERT_FALSE(mDevice->isEnabled());
2212
2213 // Device should still be disabled even found the associated display.
2214 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2215 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2216 ASSERT_FALSE(mDevice->isEnabled());
2217}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002218
2219// --- InputMapperTest ---
2220
2221class InputMapperTest : public testing::Test {
2222protected:
2223 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002224 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002225 static const int32_t DEVICE_ID;
2226 static const int32_t DEVICE_GENERATION;
2227 static const int32_t DEVICE_CONTROLLER_NUMBER;
2228 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002229 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002230
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002231 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002232 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002233 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002234 FakeInputReaderContext* mFakeContext;
2235 InputDevice* mDevice;
2236
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002237 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002238 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002239 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002240 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002241 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2242 InputDeviceIdentifier identifier;
2243 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002244 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002245 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002246
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002247 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002248 }
2249
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002250 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2251
Prabir Pradhan28efc192019-11-05 01:10:04 +00002252 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002253 delete mDevice;
2254 delete mFakeContext;
2255 mFakeListener.clear();
2256 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257 }
2258
2259 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002260 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002261 }
2262
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002263 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002264 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2265 mFakeContext->updatePointerDisplay();
2266 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002267 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2268 }
2269
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002270 template <class T, typename... Args>
2271 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002272 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002273 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002275 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002276 }
2277
2278 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002279 int32_t orientation, const std::string& uniqueId,
2280 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002281 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002282 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002283 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2284 }
2285
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002286 void clearViewports() {
2287 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 }
2289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002290 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2291 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002292 RawEvent event;
2293 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002294 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002295 event.type = type;
2296 event.code = code;
2297 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002298 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002299 }
2300
2301 static void assertMotionRange(const InputDeviceInfo& info,
2302 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2303 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002304 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2306 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2307 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2308 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2309 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2310 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2311 }
2312
2313 static void assertPointerCoords(const PointerCoords& coords,
2314 float x, float y, float pressure, float size,
2315 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2316 float orientation, float distance) {
2317 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2318 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2319 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2320 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2321 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2322 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2323 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2324 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2325 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2326 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2327 }
2328
2329 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2330 float actualX, actualY;
2331 controller->getPosition(&actualX, &actualY);
2332 ASSERT_NEAR(x, actualX, 1);
2333 ASSERT_NEAR(y, actualY, 1);
2334 }
2335};
2336
2337const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002338const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002339const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002340const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2341const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2342const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002343const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002344
2345// --- SwitchInputMapperTest ---
2346
2347class SwitchInputMapperTest : public InputMapperTest {
2348protected:
2349};
2350
2351TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002352 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002354 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002355}
2356
2357TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002358 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002359
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002360 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002361 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002362
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002363 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002364 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365}
2366
2367TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002368 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002369
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002370 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2371 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2372 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2373 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002374
2375 NotifySwitchArgs args;
2376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2377 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002378 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2379 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002380 args.switchMask);
2381 ASSERT_EQ(uint32_t(0), args.policyFlags);
2382}
2383
2384
2385// --- KeyboardInputMapperTest ---
2386
2387class KeyboardInputMapperTest : public InputMapperTest {
2388protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002389 const std::string UNIQUE_ID = "local:0";
2390
2391 void prepareDisplay(int32_t orientation);
2392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002393 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002394 int32_t originalKeyCode, int32_t rotatedKeyCode,
2395 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396};
2397
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002398/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2399 * orientation.
2400 */
2401void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2402 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002403 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002404}
2405
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002406void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002407 int32_t originalScanCode, int32_t originalKeyCode,
2408 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409 NotifyKeyArgs args;
2410
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002411 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2413 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2414 ASSERT_EQ(originalScanCode, args.scanCode);
2415 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002416 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002418 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2420 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2421 ASSERT_EQ(originalScanCode, args.scanCode);
2422 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002423 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424}
2425
Michael Wrightd02c5b62014-02-10 15:10:22 -08002426TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002427 KeyboardInputMapper& mapper =
2428 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2429 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002431 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432}
2433
2434TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2435 const int32_t USAGE_A = 0x070004;
2436 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002437 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2438 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002440 KeyboardInputMapper& mapper =
2441 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2442 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002443
2444 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002445 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446 NotifyKeyArgs args;
2447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2448 ASSERT_EQ(DEVICE_ID, args.deviceId);
2449 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2450 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2451 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2452 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2453 ASSERT_EQ(KEY_HOME, args.scanCode);
2454 ASSERT_EQ(AMETA_NONE, args.metaState);
2455 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2456 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2458
2459 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002460 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2462 ASSERT_EQ(DEVICE_ID, args.deviceId);
2463 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2464 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2465 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2466 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2467 ASSERT_EQ(KEY_HOME, args.scanCode);
2468 ASSERT_EQ(AMETA_NONE, args.metaState);
2469 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2470 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2471 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2472
2473 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002474 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2475 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2477 ASSERT_EQ(DEVICE_ID, args.deviceId);
2478 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2479 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2480 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2481 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2482 ASSERT_EQ(0, args.scanCode);
2483 ASSERT_EQ(AMETA_NONE, args.metaState);
2484 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2485 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2486 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2487
2488 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002489 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2490 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2492 ASSERT_EQ(DEVICE_ID, args.deviceId);
2493 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2494 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2495 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2496 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2497 ASSERT_EQ(0, args.scanCode);
2498 ASSERT_EQ(AMETA_NONE, args.metaState);
2499 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2500 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2501 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2502
2503 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002504 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2505 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2507 ASSERT_EQ(DEVICE_ID, args.deviceId);
2508 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2509 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2510 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2511 ASSERT_EQ(0, args.keyCode);
2512 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2513 ASSERT_EQ(AMETA_NONE, args.metaState);
2514 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2515 ASSERT_EQ(0U, args.policyFlags);
2516 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2517
2518 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002519 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2520 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2522 ASSERT_EQ(DEVICE_ID, args.deviceId);
2523 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2524 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2525 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2526 ASSERT_EQ(0, args.keyCode);
2527 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2528 ASSERT_EQ(AMETA_NONE, args.metaState);
2529 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2530 ASSERT_EQ(0U, args.policyFlags);
2531 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2532}
2533
2534TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002537
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002538 KeyboardInputMapper& mapper =
2539 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2540 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002541
2542 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002543 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002544
2545 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002546 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002547 NotifyKeyArgs args;
2548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2549 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002550 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002551 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2552
2553 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002554 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2556 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002557 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002558
2559 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002560 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2562 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002563 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564
2565 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002566 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2568 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002569 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002570 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2571}
2572
2573TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2577 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002579 KeyboardInputMapper& mapper =
2580 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2581 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002582
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002583 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002584 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2585 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2586 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2587 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2588 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2589 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2590 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2591 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2592}
2593
2594TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002595 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2596 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2597 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002601 KeyboardInputMapper& mapper =
2602 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2603 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002605 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002606 ASSERT_NO_FATAL_FAILURE(
2607 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2608 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2609 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2610 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2611 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2613 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002615 clearViewports();
2616 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002617 ASSERT_NO_FATAL_FAILURE(
2618 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2619 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2620 AKEYCODE_DPAD_UP, DISPLAY_ID));
2621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2622 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2624 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002626 clearViewports();
2627 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002628 ASSERT_NO_FATAL_FAILURE(
2629 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2630 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2631 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2633 AKEYCODE_DPAD_UP, DISPLAY_ID));
2634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2635 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002637 clearViewports();
2638 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002639 ASSERT_NO_FATAL_FAILURE(
2640 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2641 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2642 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2644 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2646 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
2648 // Special case: if orientation changes while key is down, we still emit the same keycode
2649 // in the key up as we did in the key down.
2650 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002651 clearViewports();
2652 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002653 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2655 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2656 ASSERT_EQ(KEY_UP, args.scanCode);
2657 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2658
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002659 clearViewports();
2660 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002661 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2663 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2664 ASSERT_EQ(KEY_UP, args.scanCode);
2665 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2666}
2667
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002668TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2669 // If the keyboard is not orientation aware,
2670 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002671 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002673 KeyboardInputMapper& mapper =
2674 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2675 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002676 NotifyKeyArgs args;
2677
2678 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002679 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002681 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2683 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2684
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002685 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002686 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002688 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2690 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2691}
2692
2693TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2694 // If the keyboard is orientation aware,
2695 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002696 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002697
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002698 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002699 KeyboardInputMapper& mapper =
2700 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2701 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002702 NotifyKeyArgs args;
2703
2704 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2705 // ^--- already checked by the previous test
2706
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002707 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002708 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002709 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002711 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2713 ASSERT_EQ(DISPLAY_ID, args.displayId);
2714
2715 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002716 clearViewports();
2717 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002718 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002719 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002721 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2723 ASSERT_EQ(newDisplayId, args.displayId);
2724}
2725
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002727 KeyboardInputMapper& mapper =
2728 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2729 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002730
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002731 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002732 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002734 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002735 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002736}
2737
2738TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002739 KeyboardInputMapper& mapper =
2740 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2741 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002743 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002744 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002745
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002746 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002747 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748}
2749
2750TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002751 KeyboardInputMapper& mapper =
2752 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2753 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002756
2757 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2758 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002759 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760 ASSERT_TRUE(flags[0]);
2761 ASSERT_FALSE(flags[1]);
2762}
2763
2764TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002765 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2766 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2767 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2768 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2769 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002772 KeyboardInputMapper& mapper =
2773 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2774 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775
2776 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002777 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2778 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2779 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780
2781 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002782 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2783 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002784 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2785 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2786 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788
2789 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002790 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2791 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002792 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2793 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2794 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002795 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796
2797 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002798 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2799 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002800 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2801 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2802 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002803 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804
2805 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002806 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2807 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002808 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2809 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2810 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002811 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812
2813 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002814 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2815 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2818 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820
2821 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002822 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2823 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2826 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002827 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828}
2829
Arthur Hung2c9a3342019-07-23 14:18:59 +08002830TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2831 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002832 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2833 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2834 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2835 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002836
2837 // keyboard 2.
2838 const std::string USB2 = "USB2";
2839 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002840 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002841 InputDeviceIdentifier identifier;
2842 identifier.name = "KEYBOARD2";
2843 identifier.location = USB2;
2844 std::unique_ptr<InputDevice> device2 =
2845 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002846 identifier);
2847 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2848 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2849 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2850 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2851 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002853 KeyboardInputMapper& mapper =
2854 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2855 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002857 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002858 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002859 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002860 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2861 device2->reset(ARBITRARY_TIME);
2862
2863 // Prepared displays and associated info.
2864 constexpr uint8_t hdmi1 = 0;
2865 constexpr uint8_t hdmi2 = 1;
2866 const std::string SECONDARY_UNIQUE_ID = "local:1";
2867
2868 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2869 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2870
2871 // No associated display viewport found, should disable the device.
2872 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2873 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2874 ASSERT_FALSE(device2->isEnabled());
2875
2876 // Prepare second display.
2877 constexpr int32_t newDisplayId = 2;
2878 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2879 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2880 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2881 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2882 // Default device will reconfigure above, need additional reconfiguration for another device.
2883 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2884 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2885
2886 // Device should be enabled after the associated display is found.
2887 ASSERT_TRUE(mDevice->isEnabled());
2888 ASSERT_TRUE(device2->isEnabled());
2889
2890 // Test pad key events
2891 ASSERT_NO_FATAL_FAILURE(
2892 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2893 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2894 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2895 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2896 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2897 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2898 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2899
2900 ASSERT_NO_FATAL_FAILURE(
2901 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2902 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2903 AKEYCODE_DPAD_RIGHT, newDisplayId));
2904 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2905 AKEYCODE_DPAD_DOWN, newDisplayId));
2906 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2907 AKEYCODE_DPAD_LEFT, newDisplayId));
2908}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002910// --- KeyboardInputMapperTest_ExternalDevice ---
2911
2912class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2913protected:
2914 virtual void SetUp() override {
2915 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2916 }
2917};
2918
2919TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002920 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2921 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002922
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002923 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2924 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2925 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2926 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002927
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002928 KeyboardInputMapper& mapper =
2929 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2930 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002931
2932 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2933 NotifyKeyArgs args;
2934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2935 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2936
2937 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2939 ASSERT_EQ(uint32_t(0), args.policyFlags);
2940
2941 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2943 ASSERT_EQ(uint32_t(0), args.policyFlags);
2944
2945 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2947 ASSERT_EQ(uint32_t(0), args.policyFlags);
2948
2949 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2951 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2952
2953 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2955 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2956}
2957
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002958TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002959 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002960
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002961 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2962 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2963 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002964
Powei Fengd041c5d2019-05-03 17:11:33 -07002965 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002966 KeyboardInputMapper& mapper =
2967 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2968 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002969
2970 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2971 NotifyKeyArgs args;
2972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2973 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2974
2975 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2977 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2978
2979 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2981 ASSERT_EQ(uint32_t(0), args.policyFlags);
2982
2983 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2985 ASSERT_EQ(uint32_t(0), args.policyFlags);
2986
2987 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2989 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2990
2991 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2993 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2994}
2995
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996// --- CursorInputMapperTest ---
2997
2998class CursorInputMapperTest : public InputMapperTest {
2999protected:
3000 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3001
3002 sp<FakePointerController> mFakePointerController;
3003
Prabir Pradhan28efc192019-11-05 01:10:04 +00003004 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005 InputMapperTest::SetUp();
3006
3007 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003008 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 }
3010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003011 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3012 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003013
3014 void prepareDisplay(int32_t orientation) {
3015 const std::string uniqueId = "local:0";
3016 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
3017 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3018 orientation, uniqueId, NO_PORT, viewportType);
3019 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020};
3021
3022const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3023
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003024void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3025 int32_t originalY, int32_t rotatedX,
3026 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 NotifyMotionArgs args;
3028
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003029 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3030 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3031 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3035 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3036 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3037 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3038}
3039
3040TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003042 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003044 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045}
3046
3047TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003049 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003051 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052}
3053
3054TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003056 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057
3058 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060
3061 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003062 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3063 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3065 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3066
3067 // When the bounds are set, then there should be a valid motion range.
3068 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3069
3070 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003071 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072
3073 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3074 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3075 1, 800 - 1, 0.0f, 0.0f));
3076 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3077 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3078 2, 480 - 1, 0.0f, 0.0f));
3079 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3080 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3081 0.0f, 1.0f, 0.0f, 0.0f));
3082}
3083
3084TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003086 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087
3088 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090
3091 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3092 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3093 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3094 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3095 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3096 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3097 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3098 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3099 0.0f, 1.0f, 0.0f, 0.0f));
3100}
3101
3102TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003104 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105
3106 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3107
3108 NotifyMotionArgs args;
3109
3110 // Button press.
3111 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003112 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3113 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3115 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3116 ASSERT_EQ(DEVICE_ID, args.deviceId);
3117 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3118 ASSERT_EQ(uint32_t(0), args.policyFlags);
3119 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3120 ASSERT_EQ(0, args.flags);
3121 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3122 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3123 ASSERT_EQ(0, args.edgeFlags);
3124 ASSERT_EQ(uint32_t(1), args.pointerCount);
3125 ASSERT_EQ(0, args.pointerProperties[0].id);
3126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3128 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3129 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3130 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3131 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3132
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3134 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3135 ASSERT_EQ(DEVICE_ID, args.deviceId);
3136 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3137 ASSERT_EQ(uint32_t(0), args.policyFlags);
3138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3139 ASSERT_EQ(0, args.flags);
3140 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3141 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3142 ASSERT_EQ(0, args.edgeFlags);
3143 ASSERT_EQ(uint32_t(1), args.pointerCount);
3144 ASSERT_EQ(0, args.pointerProperties[0].id);
3145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3147 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3148 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3149 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3150 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3151
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003153 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3154 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3156 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3157 ASSERT_EQ(DEVICE_ID, args.deviceId);
3158 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3159 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3161 ASSERT_EQ(0, args.flags);
3162 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3163 ASSERT_EQ(0, args.buttonState);
3164 ASSERT_EQ(0, args.edgeFlags);
3165 ASSERT_EQ(uint32_t(1), args.pointerCount);
3166 ASSERT_EQ(0, args.pointerProperties[0].id);
3167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3169 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3170 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3171 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3172 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3173
3174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3175 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3176 ASSERT_EQ(DEVICE_ID, args.deviceId);
3177 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3178 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003179 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3180 ASSERT_EQ(0, args.flags);
3181 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3182 ASSERT_EQ(0, args.buttonState);
3183 ASSERT_EQ(0, args.edgeFlags);
3184 ASSERT_EQ(uint32_t(1), args.pointerCount);
3185 ASSERT_EQ(0, args.pointerProperties[0].id);
3186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3188 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3189 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3190 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3191 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3192}
3193
3194TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003196 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197
3198 NotifyMotionArgs args;
3199
3200 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003201 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3202 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3206 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3207
3208 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003209 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3210 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3214 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3215}
3216
3217TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003219 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220
3221 NotifyMotionArgs args;
3222
3223 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003224 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3225 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3229 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3230
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3234 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3235
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003237 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3238 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3242 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3243
3244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3247 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3248}
3249
3250TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253
3254 NotifyMotionArgs args;
3255
3256 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003257 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3258 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3259 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3260 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3264 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3265 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3266
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3270 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3271 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3272
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003274 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3275 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3276 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3280 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3281 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3282
3283 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003284 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3285 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3289 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3290
3291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3294 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3295}
3296
3297TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003299 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003301 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3303 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3304 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3305 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3306 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3307 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3308 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3309 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3310}
3311
3312TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313 addConfigurationProperty("cursor.mode", "navigation");
3314 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003315 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003317 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3319 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3320 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3321 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3322 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3323 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3324 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3325 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3326
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003327 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3329 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3330 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3331 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3332 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3333 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3334 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3335 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3336
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003337 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3339 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3340 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3341 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3345 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3346
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003347 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3350 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3351 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3352 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3354 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3355 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3356}
3357
3358TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361
3362 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3363 mFakePointerController->setPosition(100, 200);
3364 mFakePointerController->setButtonState(0);
3365
3366 NotifyMotionArgs motionArgs;
3367 NotifyKeyArgs keyArgs;
3368
3369 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003370 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3371 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3373 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3374 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3375 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3377 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3378
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3381 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3382 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3384 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3385
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003386 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3387 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390 ASSERT_EQ(0, motionArgs.buttonState);
3391 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3393 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3394
3395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003396 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397 ASSERT_EQ(0, motionArgs.buttonState);
3398 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3400 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3401
3402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003404 ASSERT_EQ(0, motionArgs.buttonState);
3405 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3407 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3408
3409 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003410 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3411 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3412 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3414 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3415 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3416 motionArgs.buttonState);
3417 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3418 mFakePointerController->getButtonState());
3419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3420 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3421
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3424 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3425 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3426 mFakePointerController->getButtonState());
3427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3428 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3429
3430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3431 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3432 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3433 motionArgs.buttonState);
3434 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3435 mFakePointerController->getButtonState());
3436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3437 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3438
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003439 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3440 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003442 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3444 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3446 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3447
3448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003450 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3451 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3453 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3454
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003455 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3456 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3459 ASSERT_EQ(0, motionArgs.buttonState);
3460 ASSERT_EQ(0, mFakePointerController->getButtonState());
3461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3462 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 -08003463 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3464 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003465
3466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467 ASSERT_EQ(0, motionArgs.buttonState);
3468 ASSERT_EQ(0, mFakePointerController->getButtonState());
3469 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3471 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 -08003472
Michael Wrightd02c5b62014-02-10 15:10:22 -08003473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3474 ASSERT_EQ(0, motionArgs.buttonState);
3475 ASSERT_EQ(0, mFakePointerController->getButtonState());
3476 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3478 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3479
3480 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003481 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3482 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3484 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3485 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003486
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3490 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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
3494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3496 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3497 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3499 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3500
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003501 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3502 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505 ASSERT_EQ(0, motionArgs.buttonState);
3506 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003507 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
3510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003512 ASSERT_EQ(0, motionArgs.buttonState);
3513 ASSERT_EQ(0, mFakePointerController->getButtonState());
3514
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3516 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3518 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3519 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3520
3521 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003522 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3523 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3525 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3526 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003527
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3531 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3533 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3534
3535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3537 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3538 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3540 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3541
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003542 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3543 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 ASSERT_EQ(0, motionArgs.buttonState);
3547 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3549 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 -08003550
3551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3553 ASSERT_EQ(0, motionArgs.buttonState);
3554 ASSERT_EQ(0, mFakePointerController->getButtonState());
3555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3556 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3557
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3559 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3560 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3561
3562 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003563 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3564 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3566 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3567 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003568
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003571 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3572 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3574 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3575
3576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3577 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3578 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3579 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3581 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3582
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003583 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3584 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587 ASSERT_EQ(0, motionArgs.buttonState);
3588 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3590 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 -08003591
3592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3593 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3594 ASSERT_EQ(0, motionArgs.buttonState);
3595 ASSERT_EQ(0, mFakePointerController->getButtonState());
3596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3597 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3598
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3600 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3601 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3602
3603 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003604 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3605 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3607 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3608 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003609
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3613 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3615 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3616
3617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3619 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3620 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3622 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3623
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003624 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3625 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_EQ(0, motionArgs.buttonState);
3629 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3631 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 -08003632
3633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3634 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3635 ASSERT_EQ(0, motionArgs.buttonState);
3636 ASSERT_EQ(0, mFakePointerController->getButtonState());
3637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3638 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3639
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3641 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3642 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3643}
3644
3645TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003647 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003648
3649 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3650 mFakePointerController->setPosition(100, 200);
3651 mFakePointerController->setButtonState(0);
3652
3653 NotifyMotionArgs args;
3654
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003655 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3656 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3657 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003659 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3662 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3663 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3664}
3665
3666TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003667 addConfigurationProperty("cursor.mode", "pointer");
3668 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003670
3671 NotifyDeviceResetArgs resetArgs;
3672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3673 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3674 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3675
3676 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3677 mFakePointerController->setPosition(100, 200);
3678 mFakePointerController->setButtonState(0);
3679
3680 NotifyMotionArgs args;
3681
3682 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003683 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3684 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3685 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3687 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3690 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3691 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3692
3693 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003694 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3695 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3697 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3698 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3700 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3702 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3703 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3705 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3706
3707 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003708 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3709 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3711 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3712 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3714 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3716 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3717 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3719 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3720
3721 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003722 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3723 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3724 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3726 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3729 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3730 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3731
3732 // Disable pointer capture and check that the device generation got bumped
3733 // and events are generated the usual way.
3734 const uint32_t generation = mFakeContext->getGeneration();
3735 mFakePolicy->setPointerCapture(false);
3736 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3737 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3738
3739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3740 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3741 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3742
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003743 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3744 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3745 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3747 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3750 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3751 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3752}
3753
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003754TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003755 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003756
Garfield Tan888a6a42020-01-09 11:39:16 -08003757 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003758 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003759 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3760 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3761 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3762 ViewportType::VIEWPORT_EXTERNAL);
3763 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3764 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3765
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003766 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3767 mFakePointerController->setPosition(100, 200);
3768 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003769
3770 NotifyMotionArgs args;
3771 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3772 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3773 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3775 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3778 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3779 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3780 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3781}
3782
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783// --- TouchInputMapperTest ---
3784
3785class TouchInputMapperTest : public InputMapperTest {
3786protected:
3787 static const int32_t RAW_X_MIN;
3788 static const int32_t RAW_X_MAX;
3789 static const int32_t RAW_Y_MIN;
3790 static const int32_t RAW_Y_MAX;
3791 static const int32_t RAW_TOUCH_MIN;
3792 static const int32_t RAW_TOUCH_MAX;
3793 static const int32_t RAW_TOOL_MIN;
3794 static const int32_t RAW_TOOL_MAX;
3795 static const int32_t RAW_PRESSURE_MIN;
3796 static const int32_t RAW_PRESSURE_MAX;
3797 static const int32_t RAW_ORIENTATION_MIN;
3798 static const int32_t RAW_ORIENTATION_MAX;
3799 static const int32_t RAW_DISTANCE_MIN;
3800 static const int32_t RAW_DISTANCE_MAX;
3801 static const int32_t RAW_TILT_MIN;
3802 static const int32_t RAW_TILT_MAX;
3803 static const int32_t RAW_ID_MIN;
3804 static const int32_t RAW_ID_MAX;
3805 static const int32_t RAW_SLOT_MIN;
3806 static const int32_t RAW_SLOT_MAX;
3807 static const float X_PRECISION;
3808 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003809 static const float X_PRECISION_VIRTUAL;
3810 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811
3812 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003813 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814
3815 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3816
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003817 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003818 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003819
Michael Wrightd02c5b62014-02-10 15:10:22 -08003820 enum Axes {
3821 POSITION = 1 << 0,
3822 TOUCH = 1 << 1,
3823 TOOL = 1 << 2,
3824 PRESSURE = 1 << 3,
3825 ORIENTATION = 1 << 4,
3826 MINOR = 1 << 5,
3827 ID = 1 << 6,
3828 DISTANCE = 1 << 7,
3829 TILT = 1 << 8,
3830 SLOT = 1 << 9,
3831 TOOL_TYPE = 1 << 10,
3832 };
3833
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003834 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3835 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003836 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003838 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839 int32_t toRawX(float displayX);
3840 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003841 float toCookedX(float rawX, float rawY);
3842 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003844 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003846 float toDisplayY(int32_t rawY, int32_t displayHeight);
3847
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848};
3849
3850const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3851const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3852const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3853const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3854const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3855const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3856const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3857const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003858const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3859const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3861const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3862const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3863const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3864const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3865const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3866const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3867const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3868const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3869const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3870const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3871const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003872const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3873 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3874const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3875 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003876const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3877 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878
3879const float TouchInputMapperTest::GEOMETRIC_SCALE =
3880 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3881 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3882
3883const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3884 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3885 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3886};
3887
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003888void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003889 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003890 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3891}
3892
3893void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3894 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3895 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896}
3897
Santos Cordonfa5cf462017-04-05 10:37:00 -07003898void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003899 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3900 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003901 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003902}
3903
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003905 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3906 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3907 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3908 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909}
3910
Jason Gerecke489fda82012-09-07 17:19:40 -07003911void TouchInputMapperTest::prepareLocationCalibration() {
3912 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3913}
3914
Michael Wrightd02c5b62014-02-10 15:10:22 -08003915int32_t TouchInputMapperTest::toRawX(float displayX) {
3916 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3917}
3918
3919int32_t TouchInputMapperTest::toRawY(float displayY) {
3920 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3921}
3922
Jason Gerecke489fda82012-09-07 17:19:40 -07003923float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3924 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3925 return rawX;
3926}
3927
3928float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3929 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3930 return rawY;
3931}
3932
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003934 return toDisplayX(rawX, DISPLAY_WIDTH);
3935}
3936
3937float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3938 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003939}
3940
3941float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003942 return toDisplayY(rawY, DISPLAY_HEIGHT);
3943}
3944
3945float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3946 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003947}
3948
3949
3950// --- SingleTouchInputMapperTest ---
3951
3952class SingleTouchInputMapperTest : public TouchInputMapperTest {
3953protected:
3954 void prepareButtons();
3955 void prepareAxes(int axes);
3956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003957 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3958 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3959 void processUp(SingleTouchInputMapper& mappery);
3960 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3961 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3962 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3963 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3964 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3965 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966};
3967
3968void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003969 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003970}
3971
3972void SingleTouchInputMapperTest::prepareAxes(int axes) {
3973 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003974 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
3975 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976 }
3977 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003978 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
3979 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980 }
3981 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003982 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
3983 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 }
3985 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003986 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
3987 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988 }
3989 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003990 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3991 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992 }
3993}
3994
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003995void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003996 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3997 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3998 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999}
4000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004001void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004002 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4003 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004}
4005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004006void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004007 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008}
4009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004010void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004011 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004012}
4013
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004014void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4015 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004016 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017}
4018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004019void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004020 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021}
4022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004023void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4024 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004025 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4026 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027}
4028
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004029void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4030 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004031 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032}
4033
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004034void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004035 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036}
4037
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 prepareButtons();
4040 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004041 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004043 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044}
4045
4046TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004047 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4048 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 prepareButtons();
4050 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004051 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004053 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054}
4055
4056TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057 prepareButtons();
4058 prepareAxes(POSITION);
4059 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004060 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004062 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063}
4064
4065TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066 prepareButtons();
4067 prepareAxes(POSITION);
4068 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004069 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004071 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072}
4073
4074TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 addConfigurationProperty("touch.deviceType", "touchScreen");
4076 prepareDisplay(DISPLAY_ORIENTATION_0);
4077 prepareButtons();
4078 prepareAxes(POSITION);
4079 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004080 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081
4082 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004083 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084
4085 // Virtual key is down.
4086 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4087 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4088 processDown(mapper, x, y);
4089 processSync(mapper);
4090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4091
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004092 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093
4094 // Virtual key is up.
4095 processUp(mapper);
4096 processSync(mapper);
4097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004099 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100}
4101
4102TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 addConfigurationProperty("touch.deviceType", "touchScreen");
4104 prepareDisplay(DISPLAY_ORIENTATION_0);
4105 prepareButtons();
4106 prepareAxes(POSITION);
4107 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004108 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
4110 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004111 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112
4113 // Virtual key is down.
4114 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4115 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4116 processDown(mapper, x, y);
4117 processSync(mapper);
4118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004120 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121
4122 // Virtual key is up.
4123 processUp(mapper);
4124 processSync(mapper);
4125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004127 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128}
4129
4130TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 addConfigurationProperty("touch.deviceType", "touchScreen");
4132 prepareDisplay(DISPLAY_ORIENTATION_0);
4133 prepareButtons();
4134 prepareAxes(POSITION);
4135 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004136 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137
4138 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4139 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 ASSERT_TRUE(flags[0]);
4142 ASSERT_FALSE(flags[1]);
4143}
4144
4145TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 addConfigurationProperty("touch.deviceType", "touchScreen");
4147 prepareDisplay(DISPLAY_ORIENTATION_0);
4148 prepareButtons();
4149 prepareAxes(POSITION);
4150 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004151 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152
4153 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4154
4155 NotifyKeyArgs args;
4156
4157 // Press virtual key.
4158 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4159 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4160 processDown(mapper, x, y);
4161 processSync(mapper);
4162
4163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4164 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4165 ASSERT_EQ(DEVICE_ID, args.deviceId);
4166 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4167 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4168 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4169 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4170 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4171 ASSERT_EQ(KEY_HOME, args.scanCode);
4172 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4173 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4174
4175 // Release virtual key.
4176 processUp(mapper);
4177 processSync(mapper);
4178
4179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4180 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4181 ASSERT_EQ(DEVICE_ID, args.deviceId);
4182 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4183 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4184 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4185 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4186 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4187 ASSERT_EQ(KEY_HOME, args.scanCode);
4188 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4189 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4190
4191 // Should not have sent any motions.
4192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4193}
4194
4195TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 addConfigurationProperty("touch.deviceType", "touchScreen");
4197 prepareDisplay(DISPLAY_ORIENTATION_0);
4198 prepareButtons();
4199 prepareAxes(POSITION);
4200 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004201 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202
4203 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4204
4205 NotifyKeyArgs keyArgs;
4206
4207 // Press virtual key.
4208 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4209 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4210 processDown(mapper, x, y);
4211 processSync(mapper);
4212
4213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4214 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4215 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4216 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4217 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4218 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4219 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4220 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4221 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4223 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4224
4225 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4226 // into the display area.
4227 y -= 100;
4228 processMove(mapper, x, y);
4229 processSync(mapper);
4230
4231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4232 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4233 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4234 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4235 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4236 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4237 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4238 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4239 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4240 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4241 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4242 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4243
4244 NotifyMotionArgs motionArgs;
4245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4247 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4248 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4249 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4250 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4251 ASSERT_EQ(0, motionArgs.flags);
4252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4253 ASSERT_EQ(0, motionArgs.buttonState);
4254 ASSERT_EQ(0, motionArgs.edgeFlags);
4255 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4259 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4260 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4261 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4263
4264 // Keep moving out of bounds. Should generate a pointer move.
4265 y -= 50;
4266 processMove(mapper, x, y);
4267 processSync(mapper);
4268
4269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4275 ASSERT_EQ(0, motionArgs.flags);
4276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4277 ASSERT_EQ(0, motionArgs.buttonState);
4278 ASSERT_EQ(0, motionArgs.edgeFlags);
4279 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4283 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4284 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4285 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4287
4288 // Release out of bounds. Should generate a pointer up.
4289 processUp(mapper);
4290 processSync(mapper);
4291
4292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4297 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4298 ASSERT_EQ(0, motionArgs.flags);
4299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4300 ASSERT_EQ(0, motionArgs.buttonState);
4301 ASSERT_EQ(0, motionArgs.edgeFlags);
4302 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4306 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4307 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4308 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4310
4311 // Should not have sent any more keys or motions.
4312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4314}
4315
4316TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 addConfigurationProperty("touch.deviceType", "touchScreen");
4318 prepareDisplay(DISPLAY_ORIENTATION_0);
4319 prepareButtons();
4320 prepareAxes(POSITION);
4321 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004322 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323
4324 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4325
4326 NotifyMotionArgs motionArgs;
4327
4328 // Initially go down out of bounds.
4329 int32_t x = -10;
4330 int32_t y = -10;
4331 processDown(mapper, x, y);
4332 processSync(mapper);
4333
4334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4335
4336 // Move into the display area. Should generate a pointer down.
4337 x = 50;
4338 y = 75;
4339 processMove(mapper, x, y);
4340 processSync(mapper);
4341
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4344 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4345 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4346 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4347 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4348 ASSERT_EQ(0, motionArgs.flags);
4349 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4350 ASSERT_EQ(0, motionArgs.buttonState);
4351 ASSERT_EQ(0, motionArgs.edgeFlags);
4352 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4353 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4356 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4357 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4358 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4359 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4360
4361 // Release. Should generate a pointer up.
4362 processUp(mapper);
4363 processSync(mapper);
4364
4365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4367 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4368 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4369 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4370 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4371 ASSERT_EQ(0, motionArgs.flags);
4372 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4373 ASSERT_EQ(0, motionArgs.buttonState);
4374 ASSERT_EQ(0, motionArgs.edgeFlags);
4375 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4376 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4379 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4380 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4381 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4383
4384 // Should not have sent any more keys or motions.
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4387}
4388
Santos Cordonfa5cf462017-04-05 10:37:00 -07004389TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004390 addConfigurationProperty("touch.deviceType", "touchScreen");
4391 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4392
4393 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4394 prepareButtons();
4395 prepareAxes(POSITION);
4396 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004397 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004398
4399 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4400
4401 NotifyMotionArgs motionArgs;
4402
4403 // Down.
4404 int32_t x = 100;
4405 int32_t y = 125;
4406 processDown(mapper, x, y);
4407 processSync(mapper);
4408
4409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4410 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4411 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4412 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4413 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4414 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4415 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4416 ASSERT_EQ(0, motionArgs.flags);
4417 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4418 ASSERT_EQ(0, motionArgs.buttonState);
4419 ASSERT_EQ(0, motionArgs.edgeFlags);
4420 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4421 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4424 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4425 1, 0, 0, 0, 0, 0, 0, 0));
4426 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4427 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4428 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4429
4430 // Move.
4431 x += 50;
4432 y += 75;
4433 processMove(mapper, x, y);
4434 processSync(mapper);
4435
4436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4437 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4438 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4439 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4440 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4441 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4443 ASSERT_EQ(0, motionArgs.flags);
4444 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4445 ASSERT_EQ(0, motionArgs.buttonState);
4446 ASSERT_EQ(0, motionArgs.edgeFlags);
4447 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4451 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4452 1, 0, 0, 0, 0, 0, 0, 0));
4453 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4454 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4456
4457 // Up.
4458 processUp(mapper);
4459 processSync(mapper);
4460
4461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4462 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4463 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4464 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4465 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4466 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4467 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4468 ASSERT_EQ(0, motionArgs.flags);
4469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4470 ASSERT_EQ(0, motionArgs.buttonState);
4471 ASSERT_EQ(0, motionArgs.edgeFlags);
4472 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4476 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4477 1, 0, 0, 0, 0, 0, 0, 0));
4478 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4479 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4480 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4481
4482 // Should not have sent any more keys or motions.
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4485}
4486
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 addConfigurationProperty("touch.deviceType", "touchScreen");
4489 prepareDisplay(DISPLAY_ORIENTATION_0);
4490 prepareButtons();
4491 prepareAxes(POSITION);
4492 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004493 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494
4495 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4496
4497 NotifyMotionArgs motionArgs;
4498
4499 // Down.
4500 int32_t x = 100;
4501 int32_t y = 125;
4502 processDown(mapper, x, y);
4503 processSync(mapper);
4504
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4506 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4507 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4508 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4509 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4510 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4511 ASSERT_EQ(0, motionArgs.flags);
4512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4513 ASSERT_EQ(0, motionArgs.buttonState);
4514 ASSERT_EQ(0, motionArgs.edgeFlags);
4515 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4516 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4519 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4520 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4521 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4523
4524 // Move.
4525 x += 50;
4526 y += 75;
4527 processMove(mapper, x, y);
4528 processSync(mapper);
4529
4530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4531 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4532 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4533 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4534 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4536 ASSERT_EQ(0, motionArgs.flags);
4537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4538 ASSERT_EQ(0, motionArgs.buttonState);
4539 ASSERT_EQ(0, motionArgs.edgeFlags);
4540 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4541 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4544 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4545 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4546 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4547 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4548
4549 // Up.
4550 processUp(mapper);
4551 processSync(mapper);
4552
4553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4554 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4555 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4556 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4557 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4558 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4559 ASSERT_EQ(0, motionArgs.flags);
4560 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4561 ASSERT_EQ(0, motionArgs.buttonState);
4562 ASSERT_EQ(0, motionArgs.edgeFlags);
4563 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4564 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4567 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4568 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4569 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4571
4572 // Should not have sent any more keys or motions.
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4575}
4576
4577TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578 addConfigurationProperty("touch.deviceType", "touchScreen");
4579 prepareButtons();
4580 prepareAxes(POSITION);
4581 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004582 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583
4584 NotifyMotionArgs args;
4585
4586 // Rotation 90.
4587 prepareDisplay(DISPLAY_ORIENTATION_90);
4588 processDown(mapper, toRawX(50), toRawY(75));
4589 processSync(mapper);
4590
4591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4592 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4593 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4594
4595 processUp(mapper);
4596 processSync(mapper);
4597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4598}
4599
4600TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601 addConfigurationProperty("touch.deviceType", "touchScreen");
4602 prepareButtons();
4603 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004604 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605
4606 NotifyMotionArgs args;
4607
4608 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004609 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 prepareDisplay(DISPLAY_ORIENTATION_0);
4611 processDown(mapper, toRawX(50), toRawY(75));
4612 processSync(mapper);
4613
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4615 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4616 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4617
4618 processUp(mapper);
4619 processSync(mapper);
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4621
4622 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004623 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 prepareDisplay(DISPLAY_ORIENTATION_90);
4625 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4626 processSync(mapper);
4627
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4629 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4630 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4631
4632 processUp(mapper);
4633 processSync(mapper);
4634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4635
4636 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004637 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638 prepareDisplay(DISPLAY_ORIENTATION_180);
4639 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4640 processSync(mapper);
4641
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4643 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4644 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4645
4646 processUp(mapper);
4647 processSync(mapper);
4648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4649
4650 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004651 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652 prepareDisplay(DISPLAY_ORIENTATION_270);
4653 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4654 processSync(mapper);
4655
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4657 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4658 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4659
4660 processUp(mapper);
4661 processSync(mapper);
4662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4663}
4664
4665TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 addConfigurationProperty("touch.deviceType", "touchScreen");
4667 prepareDisplay(DISPLAY_ORIENTATION_0);
4668 prepareButtons();
4669 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004670 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671
4672 // These calculations are based on the input device calibration documentation.
4673 int32_t rawX = 100;
4674 int32_t rawY = 200;
4675 int32_t rawPressure = 10;
4676 int32_t rawToolMajor = 12;
4677 int32_t rawDistance = 2;
4678 int32_t rawTiltX = 30;
4679 int32_t rawTiltY = 110;
4680
4681 float x = toDisplayX(rawX);
4682 float y = toDisplayY(rawY);
4683 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4684 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4685 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4686 float distance = float(rawDistance);
4687
4688 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4689 float tiltScale = M_PI / 180;
4690 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4691 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4692 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4693 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4694
4695 processDown(mapper, rawX, rawY);
4696 processPressure(mapper, rawPressure);
4697 processToolMajor(mapper, rawToolMajor);
4698 processDistance(mapper, rawDistance);
4699 processTilt(mapper, rawTiltX, rawTiltY);
4700 processSync(mapper);
4701
4702 NotifyMotionArgs args;
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4705 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4706 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4707}
4708
Jason Gerecke489fda82012-09-07 17:19:40 -07004709TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004710 addConfigurationProperty("touch.deviceType", "touchScreen");
4711 prepareDisplay(DISPLAY_ORIENTATION_0);
4712 prepareLocationCalibration();
4713 prepareButtons();
4714 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004715 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004716
4717 int32_t rawX = 100;
4718 int32_t rawY = 200;
4719
4720 float x = toDisplayX(toCookedX(rawX, rawY));
4721 float y = toDisplayY(toCookedY(rawX, rawY));
4722
4723 processDown(mapper, rawX, rawY);
4724 processSync(mapper);
4725
4726 NotifyMotionArgs args;
4727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4729 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4730}
4731
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 addConfigurationProperty("touch.deviceType", "touchScreen");
4734 prepareDisplay(DISPLAY_ORIENTATION_0);
4735 prepareButtons();
4736 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004737 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738
4739 NotifyMotionArgs motionArgs;
4740 NotifyKeyArgs keyArgs;
4741
4742 processDown(mapper, 100, 200);
4743 processSync(mapper);
4744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4745 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4746 ASSERT_EQ(0, motionArgs.buttonState);
4747
4748 // press BTN_LEFT, release BTN_LEFT
4749 processKey(mapper, BTN_LEFT, 1);
4750 processSync(mapper);
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4753 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4754
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4757 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4758
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759 processKey(mapper, BTN_LEFT, 0);
4760 processSync(mapper);
4761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004762 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004764
4765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004767 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768
4769 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4770 processKey(mapper, BTN_RIGHT, 1);
4771 processKey(mapper, BTN_MIDDLE, 1);
4772 processSync(mapper);
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4775 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4776 motionArgs.buttonState);
4777
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4780 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4781
4782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4784 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4785 motionArgs.buttonState);
4786
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787 processKey(mapper, BTN_RIGHT, 0);
4788 processSync(mapper);
4789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004790 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004792
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004795 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796
4797 processKey(mapper, BTN_MIDDLE, 0);
4798 processSync(mapper);
4799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004805 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806
4807 // press BTN_BACK, release BTN_BACK
4808 processKey(mapper, BTN_BACK, 1);
4809 processSync(mapper);
4810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4811 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4812 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004813
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004816 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4817
4818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4819 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4820 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821
4822 processKey(mapper, BTN_BACK, 0);
4823 processSync(mapper);
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004825 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004827
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004830 ASSERT_EQ(0, motionArgs.buttonState);
4831
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4833 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4834 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4835
4836 // press BTN_SIDE, release BTN_SIDE
4837 processKey(mapper, BTN_SIDE, 1);
4838 processSync(mapper);
4839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4840 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4841 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004842
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004845 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4846
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4848 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4849 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850
4851 processKey(mapper, BTN_SIDE, 0);
4852 processSync(mapper);
4853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004854 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004856
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004859 ASSERT_EQ(0, motionArgs.buttonState);
4860
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4862 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4863 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4864
4865 // press BTN_FORWARD, release BTN_FORWARD
4866 processKey(mapper, BTN_FORWARD, 1);
4867 processSync(mapper);
4868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4869 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4870 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004871
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004874 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4875
4876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4877 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4878 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879
4880 processKey(mapper, BTN_FORWARD, 0);
4881 processSync(mapper);
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004885
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004888 ASSERT_EQ(0, motionArgs.buttonState);
4889
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4891 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4892 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4893
4894 // press BTN_EXTRA, release BTN_EXTRA
4895 processKey(mapper, BTN_EXTRA, 1);
4896 processSync(mapper);
4897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4898 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4899 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004900
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004903 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4904
4905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4906 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4907 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908
4909 processKey(mapper, BTN_EXTRA, 0);
4910 processSync(mapper);
4911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004914
4915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004917 ASSERT_EQ(0, motionArgs.buttonState);
4918
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4920 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4921 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4922
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4924
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925 // press BTN_STYLUS, release BTN_STYLUS
4926 processKey(mapper, BTN_STYLUS, 1);
4927 processSync(mapper);
4928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004930 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4931
4932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4934 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935
4936 processKey(mapper, BTN_STYLUS, 0);
4937 processSync(mapper);
4938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004941
4942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004944 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945
4946 // press BTN_STYLUS2, release BTN_STYLUS2
4947 processKey(mapper, BTN_STYLUS2, 1);
4948 processSync(mapper);
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004951 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4952
4953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4954 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4955 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956
4957 processKey(mapper, BTN_STYLUS2, 0);
4958 processSync(mapper);
4959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004962
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004965 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966
4967 // release touch
4968 processUp(mapper);
4969 processSync(mapper);
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4971 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4972 ASSERT_EQ(0, motionArgs.buttonState);
4973}
4974
4975TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 addConfigurationProperty("touch.deviceType", "touchScreen");
4977 prepareDisplay(DISPLAY_ORIENTATION_0);
4978 prepareButtons();
4979 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004980 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981
4982 NotifyMotionArgs motionArgs;
4983
4984 // default tool type is finger
4985 processDown(mapper, 100, 200);
4986 processSync(mapper);
4987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4990
4991 // eraser
4992 processKey(mapper, BTN_TOOL_RUBBER, 1);
4993 processSync(mapper);
4994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4997
4998 // stylus
4999 processKey(mapper, BTN_TOOL_RUBBER, 0);
5000 processKey(mapper, BTN_TOOL_PEN, 1);
5001 processSync(mapper);
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5005
5006 // brush
5007 processKey(mapper, BTN_TOOL_PEN, 0);
5008 processKey(mapper, BTN_TOOL_BRUSH, 1);
5009 processSync(mapper);
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5013
5014 // pencil
5015 processKey(mapper, BTN_TOOL_BRUSH, 0);
5016 processKey(mapper, BTN_TOOL_PENCIL, 1);
5017 processSync(mapper);
5018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5021
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005022 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 processKey(mapper, BTN_TOOL_PENCIL, 0);
5024 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5025 processSync(mapper);
5026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5029
5030 // mouse
5031 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5032 processKey(mapper, BTN_TOOL_MOUSE, 1);
5033 processSync(mapper);
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5037
5038 // lens
5039 processKey(mapper, BTN_TOOL_MOUSE, 0);
5040 processKey(mapper, BTN_TOOL_LENS, 1);
5041 processSync(mapper);
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5044 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5045
5046 // double-tap
5047 processKey(mapper, BTN_TOOL_LENS, 0);
5048 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5049 processSync(mapper);
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5053
5054 // triple-tap
5055 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5056 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5057 processSync(mapper);
5058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5061
5062 // quad-tap
5063 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5064 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5065 processSync(mapper);
5066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5069
5070 // finger
5071 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5072 processKey(mapper, BTN_TOOL_FINGER, 1);
5073 processSync(mapper);
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5077
5078 // stylus trumps finger
5079 processKey(mapper, BTN_TOOL_PEN, 1);
5080 processSync(mapper);
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5084
5085 // eraser trumps stylus
5086 processKey(mapper, BTN_TOOL_RUBBER, 1);
5087 processSync(mapper);
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5091
5092 // mouse trumps eraser
5093 processKey(mapper, BTN_TOOL_MOUSE, 1);
5094 processSync(mapper);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5098
5099 // back to default tool type
5100 processKey(mapper, BTN_TOOL_MOUSE, 0);
5101 processKey(mapper, BTN_TOOL_RUBBER, 0);
5102 processKey(mapper, BTN_TOOL_PEN, 0);
5103 processKey(mapper, BTN_TOOL_FINGER, 0);
5104 processSync(mapper);
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5108}
5109
5110TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 addConfigurationProperty("touch.deviceType", "touchScreen");
5112 prepareDisplay(DISPLAY_ORIENTATION_0);
5113 prepareButtons();
5114 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005115 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005116 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117
5118 NotifyMotionArgs motionArgs;
5119
5120 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5121 processKey(mapper, BTN_TOOL_FINGER, 1);
5122 processMove(mapper, 100, 200);
5123 processSync(mapper);
5124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5125 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5127 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5128
5129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5130 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5132 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5133
5134 // move a little
5135 processMove(mapper, 150, 250);
5136 processSync(mapper);
5137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5138 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5140 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5141
5142 // down when BTN_TOUCH is pressed, pressure defaults to 1
5143 processKey(mapper, BTN_TOUCH, 1);
5144 processSync(mapper);
5145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5146 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5148 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5149
5150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5151 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5153 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5154
5155 // up when BTN_TOUCH is released, hover restored
5156 processKey(mapper, BTN_TOUCH, 0);
5157 processSync(mapper);
5158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5159 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5161 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5162
5163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5164 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5166 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5167
5168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5169 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5171 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5172
5173 // exit hover when pointer goes away
5174 processKey(mapper, BTN_TOOL_FINGER, 0);
5175 processSync(mapper);
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5177 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5179 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5180}
5181
5182TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183 addConfigurationProperty("touch.deviceType", "touchScreen");
5184 prepareDisplay(DISPLAY_ORIENTATION_0);
5185 prepareButtons();
5186 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005187 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188
5189 NotifyMotionArgs motionArgs;
5190
5191 // initially hovering because pressure is 0
5192 processDown(mapper, 100, 200);
5193 processPressure(mapper, 0);
5194 processSync(mapper);
5195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5198 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5199
5200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5201 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5203 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5204
5205 // move a little
5206 processMove(mapper, 150, 250);
5207 processSync(mapper);
5208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5211 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5212
5213 // down when pressure is non-zero
5214 processPressure(mapper, RAW_PRESSURE_MAX);
5215 processSync(mapper);
5216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5217 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5219 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5220
5221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5222 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5224 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5225
5226 // up when pressure becomes 0, hover restored
5227 processPressure(mapper, 0);
5228 processSync(mapper);
5229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5230 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5232 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5233
5234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5237 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5238
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5242 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5243
5244 // exit hover when pointer goes away
5245 processUp(mapper);
5246 processSync(mapper);
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5250 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5251}
5252
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253// --- MultiTouchInputMapperTest ---
5254
5255class MultiTouchInputMapperTest : public TouchInputMapperTest {
5256protected:
5257 void prepareAxes(int axes);
5258
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005259 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5260 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5261 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5262 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5263 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5264 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5265 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5266 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5267 void processId(MultiTouchInputMapper& mapper, int32_t id);
5268 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5269 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5270 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5271 void processMTSync(MultiTouchInputMapper& mapper);
5272 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273};
5274
5275void MultiTouchInputMapperTest::prepareAxes(int axes) {
5276 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005277 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5278 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279 }
5280 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005281 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5282 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005284 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5285 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286 }
5287 }
5288 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005289 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5290 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005292 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5293 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294 }
5295 }
5296 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005297 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5298 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299 }
5300 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005301 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5302 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303 }
5304 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005305 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5306 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005307 }
5308 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005309 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5310 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311 }
5312 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005313 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5314 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315 }
5316 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 }
5319}
5320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005321void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5322 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005323 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5324 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325}
5326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005327void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5328 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005329 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330}
5331
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005332void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5333 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005334 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335}
5336
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005337void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005338 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339}
5340
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005341void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005342 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343}
5344
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005345void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5346 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005347 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348}
5349
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005350void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005351 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352}
5353
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005354void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005355 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356}
5357
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005358void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005359 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360}
5361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005362void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005363 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364}
5365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005367 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368}
5369
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005370void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5371 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005372 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373}
5374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005376 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377}
5378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005379void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005380 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381}
5382
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384 addConfigurationProperty("touch.deviceType", "touchScreen");
5385 prepareDisplay(DISPLAY_ORIENTATION_0);
5386 prepareAxes(POSITION);
5387 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005388 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389
5390 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5391
5392 NotifyMotionArgs motionArgs;
5393
5394 // Two fingers down at once.
5395 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5396 processPosition(mapper, x1, y1);
5397 processMTSync(mapper);
5398 processPosition(mapper, x2, y2);
5399 processMTSync(mapper);
5400 processSync(mapper);
5401
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5404 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5405 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5406 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5407 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5408 ASSERT_EQ(0, motionArgs.flags);
5409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5410 ASSERT_EQ(0, motionArgs.buttonState);
5411 ASSERT_EQ(0, motionArgs.edgeFlags);
5412 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5413 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5416 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5417 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5418 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5420
5421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5422 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5423 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5424 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5425 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5426 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5427 motionArgs.action);
5428 ASSERT_EQ(0, motionArgs.flags);
5429 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5430 ASSERT_EQ(0, motionArgs.buttonState);
5431 ASSERT_EQ(0, motionArgs.edgeFlags);
5432 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5433 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5435 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5438 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5440 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5441 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5442 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5444
5445 // Move.
5446 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5447 processPosition(mapper, x1, y1);
5448 processMTSync(mapper);
5449 processPosition(mapper, x2, y2);
5450 processMTSync(mapper);
5451 processSync(mapper);
5452
5453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5455 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5456 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5457 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5459 ASSERT_EQ(0, motionArgs.flags);
5460 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5461 ASSERT_EQ(0, motionArgs.buttonState);
5462 ASSERT_EQ(0, motionArgs.edgeFlags);
5463 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5464 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5466 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5469 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5471 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5472 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5473 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5475
5476 // First finger up.
5477 x2 += 15; y2 -= 20;
5478 processPosition(mapper, x2, y2);
5479 processMTSync(mapper);
5480 processSync(mapper);
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_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5488 motionArgs.action);
5489 ASSERT_EQ(0, motionArgs.flags);
5490 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5491 ASSERT_EQ(0, motionArgs.buttonState);
5492 ASSERT_EQ(0, motionArgs.edgeFlags);
5493 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5496 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5499 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5501 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5502 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5503 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5504 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5505
5506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5507 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5508 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5509 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5510 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5512 ASSERT_EQ(0, motionArgs.flags);
5513 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5514 ASSERT_EQ(0, motionArgs.buttonState);
5515 ASSERT_EQ(0, motionArgs.edgeFlags);
5516 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5517 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5520 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5521 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5522 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5523 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5524
5525 // Move.
5526 x2 += 20; y2 -= 25;
5527 processPosition(mapper, x2, y2);
5528 processMTSync(mapper);
5529 processSync(mapper);
5530
5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5532 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5533 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5534 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5535 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5537 ASSERT_EQ(0, motionArgs.flags);
5538 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5539 ASSERT_EQ(0, motionArgs.buttonState);
5540 ASSERT_EQ(0, motionArgs.edgeFlags);
5541 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5542 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5545 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5546 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5547 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5548 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5549
5550 // New finger down.
5551 int32_t x3 = 700, y3 = 300;
5552 processPosition(mapper, x2, y2);
5553 processMTSync(mapper);
5554 processPosition(mapper, x3, y3);
5555 processMTSync(mapper);
5556 processSync(mapper);
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5560 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5561 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5562 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5563 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5564 motionArgs.action);
5565 ASSERT_EQ(0, motionArgs.flags);
5566 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5567 ASSERT_EQ(0, motionArgs.buttonState);
5568 ASSERT_EQ(0, motionArgs.edgeFlags);
5569 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5572 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5575 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5577 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5578 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5579 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5580 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5581
5582 // Second finger up.
5583 x3 += 30; y3 -= 20;
5584 processPosition(mapper, x3, y3);
5585 processMTSync(mapper);
5586 processSync(mapper);
5587
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5589 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5590 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5591 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5592 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5593 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5594 motionArgs.action);
5595 ASSERT_EQ(0, motionArgs.flags);
5596 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5597 ASSERT_EQ(0, motionArgs.buttonState);
5598 ASSERT_EQ(0, motionArgs.edgeFlags);
5599 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5600 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5602 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5605 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5607 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5608 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5609 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5610 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5611
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5613 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5614 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5615 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5616 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5618 ASSERT_EQ(0, motionArgs.flags);
5619 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5620 ASSERT_EQ(0, motionArgs.buttonState);
5621 ASSERT_EQ(0, motionArgs.edgeFlags);
5622 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5626 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5627 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5628 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5629 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5630
5631 // Last finger up.
5632 processMTSync(mapper);
5633 processSync(mapper);
5634
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5636 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5637 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5638 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5639 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5640 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5641 ASSERT_EQ(0, motionArgs.flags);
5642 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5643 ASSERT_EQ(0, motionArgs.buttonState);
5644 ASSERT_EQ(0, motionArgs.edgeFlags);
5645 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5646 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5649 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5650 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5651 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5653
5654 // Should not have sent any more keys or motions.
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5657}
5658
5659TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660 addConfigurationProperty("touch.deviceType", "touchScreen");
5661 prepareDisplay(DISPLAY_ORIENTATION_0);
5662 prepareAxes(POSITION | ID);
5663 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005664 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005665
5666 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5667
5668 NotifyMotionArgs motionArgs;
5669
5670 // Two fingers down at once.
5671 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5672 processPosition(mapper, x1, y1);
5673 processId(mapper, 1);
5674 processMTSync(mapper);
5675 processPosition(mapper, x2, y2);
5676 processId(mapper, 2);
5677 processMTSync(mapper);
5678 processSync(mapper);
5679
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5681 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5682 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5683 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5686 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5687
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5689 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5690 motionArgs.action);
5691 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5692 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5694 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5697 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5699 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5700
5701 // Move.
5702 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5703 processPosition(mapper, x1, y1);
5704 processId(mapper, 1);
5705 processMTSync(mapper);
5706 processPosition(mapper, x2, y2);
5707 processId(mapper, 2);
5708 processMTSync(mapper);
5709 processSync(mapper);
5710
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5713 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5714 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5716 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5719 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5721 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5722
5723 // First finger up.
5724 x2 += 15; y2 -= 20;
5725 processPosition(mapper, x2, y2);
5726 processId(mapper, 2);
5727 processMTSync(mapper);
5728 processSync(mapper);
5729
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5731 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5732 motionArgs.action);
5733 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5734 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5736 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5739 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5741 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5742
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5745 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5746 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5749 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5750
5751 // Move.
5752 x2 += 20; y2 -= 25;
5753 processPosition(mapper, x2, y2);
5754 processId(mapper, 2);
5755 processMTSync(mapper);
5756 processSync(mapper);
5757
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5760 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5761 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5764 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5765
5766 // New finger down.
5767 int32_t x3 = 700, y3 = 300;
5768 processPosition(mapper, x2, y2);
5769 processId(mapper, 2);
5770 processMTSync(mapper);
5771 processPosition(mapper, x3, y3);
5772 processId(mapper, 3);
5773 processMTSync(mapper);
5774 processSync(mapper);
5775
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5777 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5778 motionArgs.action);
5779 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5780 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5782 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5785 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5787 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5788
5789 // Second finger up.
5790 x3 += 30; y3 -= 20;
5791 processPosition(mapper, x3, y3);
5792 processId(mapper, 3);
5793 processMTSync(mapper);
5794 processSync(mapper);
5795
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5797 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5798 motionArgs.action);
5799 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5800 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5802 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5805 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5807 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5808
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5811 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5812 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5815 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5816
5817 // Last finger up.
5818 processMTSync(mapper);
5819 processSync(mapper);
5820
5821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5822 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5823 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5824 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5827 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5828
5829 // Should not have sent any more keys or motions.
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5832}
5833
5834TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 addConfigurationProperty("touch.deviceType", "touchScreen");
5836 prepareDisplay(DISPLAY_ORIENTATION_0);
5837 prepareAxes(POSITION | ID | SLOT);
5838 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005839 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005840
5841 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5842
5843 NotifyMotionArgs motionArgs;
5844
5845 // Two fingers down at once.
5846 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5847 processPosition(mapper, x1, y1);
5848 processId(mapper, 1);
5849 processSlot(mapper, 1);
5850 processPosition(mapper, x2, y2);
5851 processId(mapper, 2);
5852 processSync(mapper);
5853
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5855 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5856 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5857 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5860 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5861
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5863 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5864 motionArgs.action);
5865 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5866 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5868 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5871 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5873 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5874
5875 // Move.
5876 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5877 processSlot(mapper, 0);
5878 processPosition(mapper, x1, y1);
5879 processSlot(mapper, 1);
5880 processPosition(mapper, x2, y2);
5881 processSync(mapper);
5882
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5885 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5886 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5888 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5891 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5893 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5894
5895 // First finger up.
5896 x2 += 15; y2 -= 20;
5897 processSlot(mapper, 0);
5898 processId(mapper, -1);
5899 processSlot(mapper, 1);
5900 processPosition(mapper, x2, y2);
5901 processSync(mapper);
5902
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5904 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5905 motionArgs.action);
5906 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5907 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5909 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5912 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5914 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5915
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5918 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5919 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5922 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5923
5924 // Move.
5925 x2 += 20; y2 -= 25;
5926 processPosition(mapper, x2, y2);
5927 processSync(mapper);
5928
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5931 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5932 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5935 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5936
5937 // New finger down.
5938 int32_t x3 = 700, y3 = 300;
5939 processPosition(mapper, x2, y2);
5940 processSlot(mapper, 0);
5941 processId(mapper, 3);
5942 processPosition(mapper, x3, y3);
5943 processSync(mapper);
5944
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5946 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5947 motionArgs.action);
5948 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5949 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5951 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5954 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5956 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5957
5958 // Second finger up.
5959 x3 += 30; y3 -= 20;
5960 processSlot(mapper, 1);
5961 processId(mapper, -1);
5962 processSlot(mapper, 0);
5963 processPosition(mapper, x3, y3);
5964 processSync(mapper);
5965
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5967 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5968 motionArgs.action);
5969 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5970 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5972 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5975 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5977 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5978
5979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5981 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5982 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5985 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5986
5987 // Last finger up.
5988 processId(mapper, -1);
5989 processSync(mapper);
5990
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5993 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5994 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5997 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5998
5999 // Should not have sent any more keys or motions.
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6002}
6003
6004TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 addConfigurationProperty("touch.deviceType", "touchScreen");
6006 prepareDisplay(DISPLAY_ORIENTATION_0);
6007 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006008 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009
6010 // These calculations are based on the input device calibration documentation.
6011 int32_t rawX = 100;
6012 int32_t rawY = 200;
6013 int32_t rawTouchMajor = 7;
6014 int32_t rawTouchMinor = 6;
6015 int32_t rawToolMajor = 9;
6016 int32_t rawToolMinor = 8;
6017 int32_t rawPressure = 11;
6018 int32_t rawDistance = 0;
6019 int32_t rawOrientation = 3;
6020 int32_t id = 5;
6021
6022 float x = toDisplayX(rawX);
6023 float y = toDisplayY(rawY);
6024 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6025 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6026 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6027 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6028 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6029 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6030 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6031 float distance = float(rawDistance);
6032
6033 processPosition(mapper, rawX, rawY);
6034 processTouchMajor(mapper, rawTouchMajor);
6035 processTouchMinor(mapper, rawTouchMinor);
6036 processToolMajor(mapper, rawToolMajor);
6037 processToolMinor(mapper, rawToolMinor);
6038 processPressure(mapper, rawPressure);
6039 processOrientation(mapper, rawOrientation);
6040 processDistance(mapper, rawDistance);
6041 processId(mapper, id);
6042 processMTSync(mapper);
6043 processSync(mapper);
6044
6045 NotifyMotionArgs args;
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6047 ASSERT_EQ(0, args.pointerProperties[0].id);
6048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6049 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6050 orientation, distance));
6051}
6052
6053TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006054 addConfigurationProperty("touch.deviceType", "touchScreen");
6055 prepareDisplay(DISPLAY_ORIENTATION_0);
6056 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6057 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006058 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059
6060 // These calculations are based on the input device calibration documentation.
6061 int32_t rawX = 100;
6062 int32_t rawY = 200;
6063 int32_t rawTouchMajor = 140;
6064 int32_t rawTouchMinor = 120;
6065 int32_t rawToolMajor = 180;
6066 int32_t rawToolMinor = 160;
6067
6068 float x = toDisplayX(rawX);
6069 float y = toDisplayY(rawY);
6070 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6071 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6072 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6073 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6074 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6075
6076 processPosition(mapper, rawX, rawY);
6077 processTouchMajor(mapper, rawTouchMajor);
6078 processTouchMinor(mapper, rawTouchMinor);
6079 processToolMajor(mapper, rawToolMajor);
6080 processToolMinor(mapper, rawToolMinor);
6081 processMTSync(mapper);
6082 processSync(mapper);
6083
6084 NotifyMotionArgs args;
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6087 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6088}
6089
6090TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 addConfigurationProperty("touch.deviceType", "touchScreen");
6092 prepareDisplay(DISPLAY_ORIENTATION_0);
6093 prepareAxes(POSITION | TOUCH | TOOL);
6094 addConfigurationProperty("touch.size.calibration", "diameter");
6095 addConfigurationProperty("touch.size.scale", "10");
6096 addConfigurationProperty("touch.size.bias", "160");
6097 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006098 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099
6100 // These calculations are based on the input device calibration documentation.
6101 // Note: We only provide a single common touch/tool value because the device is assumed
6102 // not to emit separate values for each pointer (isSummed = 1).
6103 int32_t rawX = 100;
6104 int32_t rawY = 200;
6105 int32_t rawX2 = 150;
6106 int32_t rawY2 = 250;
6107 int32_t rawTouchMajor = 5;
6108 int32_t rawToolMajor = 8;
6109
6110 float x = toDisplayX(rawX);
6111 float y = toDisplayY(rawY);
6112 float x2 = toDisplayX(rawX2);
6113 float y2 = toDisplayY(rawY2);
6114 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6115 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6116 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6117
6118 processPosition(mapper, rawX, rawY);
6119 processTouchMajor(mapper, rawTouchMajor);
6120 processToolMajor(mapper, rawToolMajor);
6121 processMTSync(mapper);
6122 processPosition(mapper, rawX2, rawY2);
6123 processTouchMajor(mapper, rawTouchMajor);
6124 processToolMajor(mapper, rawToolMajor);
6125 processMTSync(mapper);
6126 processSync(mapper);
6127
6128 NotifyMotionArgs args;
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6130 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6131
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6133 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6134 args.action);
6135 ASSERT_EQ(size_t(2), args.pointerCount);
6136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6137 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6139 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6140}
6141
6142TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143 addConfigurationProperty("touch.deviceType", "touchScreen");
6144 prepareDisplay(DISPLAY_ORIENTATION_0);
6145 prepareAxes(POSITION | TOUCH | TOOL);
6146 addConfigurationProperty("touch.size.calibration", "area");
6147 addConfigurationProperty("touch.size.scale", "43");
6148 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006149 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150
6151 // These calculations are based on the input device calibration documentation.
6152 int32_t rawX = 100;
6153 int32_t rawY = 200;
6154 int32_t rawTouchMajor = 5;
6155 int32_t rawToolMajor = 8;
6156
6157 float x = toDisplayX(rawX);
6158 float y = toDisplayY(rawY);
6159 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6160 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6161 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6162
6163 processPosition(mapper, rawX, rawY);
6164 processTouchMajor(mapper, rawTouchMajor);
6165 processToolMajor(mapper, rawToolMajor);
6166 processMTSync(mapper);
6167 processSync(mapper);
6168
6169 NotifyMotionArgs args;
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6172 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6173}
6174
6175TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176 addConfigurationProperty("touch.deviceType", "touchScreen");
6177 prepareDisplay(DISPLAY_ORIENTATION_0);
6178 prepareAxes(POSITION | PRESSURE);
6179 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6180 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006181 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182
Michael Wrightaa449c92017-12-13 21:21:43 +00006183 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006184 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006185 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6186 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6187 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6188
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189 // These calculations are based on the input device calibration documentation.
6190 int32_t rawX = 100;
6191 int32_t rawY = 200;
6192 int32_t rawPressure = 60;
6193
6194 float x = toDisplayX(rawX);
6195 float y = toDisplayY(rawY);
6196 float pressure = float(rawPressure) * 0.01f;
6197
6198 processPosition(mapper, rawX, rawY);
6199 processPressure(mapper, rawPressure);
6200 processMTSync(mapper);
6201 processSync(mapper);
6202
6203 NotifyMotionArgs args;
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6206 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6207}
6208
6209TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 addConfigurationProperty("touch.deviceType", "touchScreen");
6211 prepareDisplay(DISPLAY_ORIENTATION_0);
6212 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006213 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214
6215 NotifyMotionArgs motionArgs;
6216 NotifyKeyArgs keyArgs;
6217
6218 processId(mapper, 1);
6219 processPosition(mapper, 100, 200);
6220 processSync(mapper);
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6223 ASSERT_EQ(0, motionArgs.buttonState);
6224
6225 // press BTN_LEFT, release BTN_LEFT
6226 processKey(mapper, BTN_LEFT, 1);
6227 processSync(mapper);
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6230 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6231
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6234 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6235
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 processKey(mapper, BTN_LEFT, 0);
6237 processSync(mapper);
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006241
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006244 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245
6246 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6247 processKey(mapper, BTN_RIGHT, 1);
6248 processKey(mapper, BTN_MIDDLE, 1);
6249 processSync(mapper);
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6252 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6253 motionArgs.buttonState);
6254
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6257 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6258
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6261 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6262 motionArgs.buttonState);
6263
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264 processKey(mapper, BTN_RIGHT, 0);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006268 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006269
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 processKey(mapper, BTN_MIDDLE, 0);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283
6284 // press BTN_BACK, release BTN_BACK
6285 processKey(mapper, BTN_BACK, 1);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6288 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6289 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006290
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006293 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6294
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298
6299 processKey(mapper, BTN_BACK, 0);
6300 processSync(mapper);
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307 ASSERT_EQ(0, motionArgs.buttonState);
6308
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6310 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6311 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6312
6313 // press BTN_SIDE, release BTN_SIDE
6314 processKey(mapper, BTN_SIDE, 1);
6315 processSync(mapper);
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6317 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6318 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006319
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006322 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6323
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6326 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006327
6328 processKey(mapper, BTN_SIDE, 0);
6329 processSync(mapper);
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006332 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006336 ASSERT_EQ(0, motionArgs.buttonState);
6337
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6339 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6340 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6341
6342 // press BTN_FORWARD, release BTN_FORWARD
6343 processKey(mapper, BTN_FORWARD, 1);
6344 processSync(mapper);
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6346 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6347 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006348
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006351 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6352
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6355 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356
6357 processKey(mapper, BTN_FORWARD, 0);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006365 ASSERT_EQ(0, motionArgs.buttonState);
6366
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6368 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6369 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6370
6371 // press BTN_EXTRA, release BTN_EXTRA
6372 processKey(mapper, BTN_EXTRA, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6375 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6376 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006377
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006380 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6381
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6384 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385
6386 processKey(mapper, BTN_EXTRA, 0);
6387 processSync(mapper);
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006394 ASSERT_EQ(0, motionArgs.buttonState);
6395
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6397 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6398 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6399
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6401
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402 // press BTN_STYLUS, release BTN_STYLUS
6403 processKey(mapper, BTN_STYLUS, 1);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006407 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6408
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6411 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412
6413 processKey(mapper, BTN_STYLUS, 0);
6414 processSync(mapper);
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006418
6419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006421 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422
6423 // press BTN_STYLUS2, release BTN_STYLUS2
6424 processKey(mapper, BTN_STYLUS2, 1);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006428 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6429
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6432 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433
6434 processKey(mapper, BTN_STYLUS2, 0);
6435 processSync(mapper);
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006442 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443
6444 // release touch
6445 processId(mapper, -1);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6449 ASSERT_EQ(0, motionArgs.buttonState);
6450}
6451
6452TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 addConfigurationProperty("touch.deviceType", "touchScreen");
6454 prepareDisplay(DISPLAY_ORIENTATION_0);
6455 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006456 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457
6458 NotifyMotionArgs motionArgs;
6459
6460 // default tool type is finger
6461 processId(mapper, 1);
6462 processPosition(mapper, 100, 200);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6467
6468 // eraser
6469 processKey(mapper, BTN_TOOL_RUBBER, 1);
6470 processSync(mapper);
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6474
6475 // stylus
6476 processKey(mapper, BTN_TOOL_RUBBER, 0);
6477 processKey(mapper, BTN_TOOL_PEN, 1);
6478 processSync(mapper);
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6481 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6482
6483 // brush
6484 processKey(mapper, BTN_TOOL_PEN, 0);
6485 processKey(mapper, BTN_TOOL_BRUSH, 1);
6486 processSync(mapper);
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6490
6491 // pencil
6492 processKey(mapper, BTN_TOOL_BRUSH, 0);
6493 processKey(mapper, BTN_TOOL_PENCIL, 1);
6494 processSync(mapper);
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6498
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006499 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 processKey(mapper, BTN_TOOL_PENCIL, 0);
6501 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6502 processSync(mapper);
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6506
6507 // mouse
6508 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6509 processKey(mapper, BTN_TOOL_MOUSE, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6514
6515 // lens
6516 processKey(mapper, BTN_TOOL_MOUSE, 0);
6517 processKey(mapper, BTN_TOOL_LENS, 1);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6522
6523 // double-tap
6524 processKey(mapper, BTN_TOOL_LENS, 0);
6525 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6530
6531 // triple-tap
6532 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6533 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6538
6539 // quad-tap
6540 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6541 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6542 processSync(mapper);
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6546
6547 // finger
6548 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6549 processKey(mapper, BTN_TOOL_FINGER, 1);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6554
6555 // stylus trumps finger
6556 processKey(mapper, BTN_TOOL_PEN, 1);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6561
6562 // eraser trumps stylus
6563 processKey(mapper, BTN_TOOL_RUBBER, 1);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6568
6569 // mouse trumps eraser
6570 processKey(mapper, BTN_TOOL_MOUSE, 1);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6575
6576 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6577 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6578 processSync(mapper);
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6582
6583 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6584 processToolType(mapper, MT_TOOL_PEN);
6585 processSync(mapper);
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6589
6590 // back to default tool type
6591 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6592 processKey(mapper, BTN_TOOL_MOUSE, 0);
6593 processKey(mapper, BTN_TOOL_RUBBER, 0);
6594 processKey(mapper, BTN_TOOL_PEN, 0);
6595 processKey(mapper, BTN_TOOL_FINGER, 0);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6600}
6601
6602TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006603 addConfigurationProperty("touch.deviceType", "touchScreen");
6604 prepareDisplay(DISPLAY_ORIENTATION_0);
6605 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006606 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006607 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006608
6609 NotifyMotionArgs motionArgs;
6610
6611 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6612 processId(mapper, 1);
6613 processPosition(mapper, 100, 200);
6614 processSync(mapper);
6615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6616 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6618 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6619
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6621 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6623 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6624
6625 // move a little
6626 processPosition(mapper, 150, 250);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6631 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6632
6633 // down when BTN_TOUCH is pressed, pressure defaults to 1
6634 processKey(mapper, BTN_TOUCH, 1);
6635 processSync(mapper);
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6637 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6639 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6640
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6644 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6645
6646 // up when BTN_TOUCH is released, hover restored
6647 processKey(mapper, BTN_TOUCH, 0);
6648 processSync(mapper);
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6652 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6653
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6657 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6658
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6662 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6663
6664 // exit hover when pointer goes away
6665 processId(mapper, -1);
6666 processSync(mapper);
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6671}
6672
6673TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006674 addConfigurationProperty("touch.deviceType", "touchScreen");
6675 prepareDisplay(DISPLAY_ORIENTATION_0);
6676 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006677 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006678
6679 NotifyMotionArgs motionArgs;
6680
6681 // initially hovering because pressure is 0
6682 processId(mapper, 1);
6683 processPosition(mapper, 100, 200);
6684 processPressure(mapper, 0);
6685 processSync(mapper);
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6687 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6689 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6690
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6694 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6695
6696 // move a little
6697 processPosition(mapper, 150, 250);
6698 processSync(mapper);
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6702 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6703
6704 // down when pressure becomes non-zero
6705 processPressure(mapper, RAW_PRESSURE_MAX);
6706 processSync(mapper);
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6710 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6711
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6713 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6715 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6716
6717 // up when pressure becomes 0, hover restored
6718 processPressure(mapper, 0);
6719 processSync(mapper);
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6721 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6723 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6724
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6728 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6729
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6733 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6734
6735 // exit hover when pointer goes away
6736 processId(mapper, -1);
6737 processSync(mapper);
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6742}
6743
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006744/**
6745 * Set the input device port <--> display port associations, and check that the
6746 * events are routed to the display that matches the display port.
6747 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6748 */
6749TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006750 const std::string usb2 = "USB2";
6751 const uint8_t hdmi1 = 0;
6752 const uint8_t hdmi2 = 1;
6753 const std::string secondaryUniqueId = "uniqueId2";
6754 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6755
6756 addConfigurationProperty("touch.deviceType", "touchScreen");
6757 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006758 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006759
6760 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6761 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6762
6763 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6764 // for this input device is specified, and the matching viewport is not present,
6765 // the input device should be disabled (at the mapper level).
6766
6767 // Add viewport for display 2 on hdmi2
6768 prepareSecondaryDisplay(type, hdmi2);
6769 // Send a touch event
6770 processPosition(mapper, 100, 100);
6771 processSync(mapper);
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6773
6774 // Add viewport for display 1 on hdmi1
6775 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6776 // Send a touch event again
6777 processPosition(mapper, 100, 100);
6778 processSync(mapper);
6779
6780 NotifyMotionArgs args;
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6782 ASSERT_EQ(DISPLAY_ID, args.displayId);
6783}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006784
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006785TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006786 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006787 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006788 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006789 fakePointerController->setPosition(100, 200);
6790 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006791 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6792
Garfield Tan888a6a42020-01-09 11:39:16 -08006793 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6794 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6795
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006796 prepareDisplay(DISPLAY_ORIENTATION_0);
6797 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006798 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006799
6800 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006801 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006802
6803 NotifyMotionArgs motionArgs;
6804 processPosition(mapper, 100, 100);
6805 processSync(mapper);
6806
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6808 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6809 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6810}
6811
Arthur Hung7c645402019-01-25 17:45:42 +08006812TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6813 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006814 prepareAxes(POSITION | ID | SLOT);
6815 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006816 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006817
6818 // Create the second touch screen device, and enable multi fingers.
6819 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006820 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006821 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006822 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006823 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006824 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006825 std::unique_ptr<InputDevice> device2 =
6826 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006827 identifier);
6828 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6829 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6830 0 /*flat*/, 0 /*fuzz*/);
6831 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6832 0 /*flat*/, 0 /*fuzz*/);
6833 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6834 0 /*flat*/, 0 /*fuzz*/);
6835 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6836 0 /*flat*/, 0 /*fuzz*/);
6837 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6838 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6839 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006840
6841 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006842 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006843 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6844 device2->reset(ARBITRARY_TIME);
6845
6846 // Setup PointerController.
6847 sp<FakePointerController> fakePointerController = new FakePointerController();
6848 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6849 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6850
6851 // Setup policy for associated displays and show touches.
6852 const uint8_t hdmi1 = 0;
6853 const uint8_t hdmi2 = 1;
6854 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6855 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6856 mFakePolicy->setShowTouches(true);
6857
6858 // Create displays.
6859 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6860 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6861
6862 // Default device will reconfigure above, need additional reconfiguration for another device.
6863 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6864 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6865
6866 // Two fingers down at default display.
6867 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6868 processPosition(mapper, x1, y1);
6869 processId(mapper, 1);
6870 processSlot(mapper, 1);
6871 processPosition(mapper, x2, y2);
6872 processId(mapper, 2);
6873 processSync(mapper);
6874
6875 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6876 fakePointerController->getSpots().find(DISPLAY_ID);
6877 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6878 ASSERT_EQ(size_t(2), iter->second.size());
6879
6880 // Two fingers down at second display.
6881 processPosition(mapper2, x1, y1);
6882 processId(mapper2, 1);
6883 processSlot(mapper2, 1);
6884 processPosition(mapper2, x2, y2);
6885 processId(mapper2, 2);
6886 processSync(mapper2);
6887
6888 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6889 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6890 ASSERT_EQ(size_t(2), iter->second.size());
6891}
6892
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006893TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006894 prepareAxes(POSITION);
6895 addConfigurationProperty("touch.deviceType", "touchScreen");
6896 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006897 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006898
6899 NotifyMotionArgs motionArgs;
6900 // Unrotated video frame
6901 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6902 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006903 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006904 processPosition(mapper, 100, 200);
6905 processSync(mapper);
6906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6907 ASSERT_EQ(frames, motionArgs.videoFrames);
6908
6909 // Subsequent touch events should not have any videoframes
6910 // This is implemented separately in FakeEventHub,
6911 // but that should match the behaviour of TouchVideoDevice.
6912 processPosition(mapper, 200, 200);
6913 processSync(mapper);
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6915 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6916}
6917
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006918TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006919 prepareAxes(POSITION);
6920 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006921 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006922 // Unrotated video frame
6923 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6924 NotifyMotionArgs motionArgs;
6925
6926 // Test all 4 orientations
6927 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6928 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6929 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6930 clearViewports();
6931 prepareDisplay(orientation);
6932 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006933 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006934 processPosition(mapper, 100, 200);
6935 processSync(mapper);
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6937 frames[0].rotate(orientation);
6938 ASSERT_EQ(frames, motionArgs.videoFrames);
6939 }
6940}
6941
6942TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006943 prepareAxes(POSITION);
6944 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006945 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006946 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6947 // so mix these.
6948 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6949 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6950 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6951 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6952 NotifyMotionArgs motionArgs;
6953
6954 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006955 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006956 processPosition(mapper, 100, 200);
6957 processSync(mapper);
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6959 std::for_each(frames.begin(), frames.end(),
6960 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6961 ASSERT_EQ(frames, motionArgs.videoFrames);
6962}
6963
Arthur Hung9da14732019-09-02 16:16:58 +08006964/**
6965 * If we had defined port associations, but the viewport is not ready, the touch device would be
6966 * expected to be disabled, and it should be enabled after the viewport has found.
6967 */
6968TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006969 constexpr uint8_t hdmi2 = 1;
6970 const std::string secondaryUniqueId = "uniqueId2";
6971 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6972
6973 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6974
6975 addConfigurationProperty("touch.deviceType", "touchScreen");
6976 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006977 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006978
6979 ASSERT_EQ(mDevice->isEnabled(), false);
6980
6981 // Add display on hdmi2, the device should be enabled and can receive touch event.
6982 prepareSecondaryDisplay(type, hdmi2);
6983 ASSERT_EQ(mDevice->isEnabled(), true);
6984
6985 // Send a touch event.
6986 processPosition(mapper, 100, 100);
6987 processSync(mapper);
6988
6989 NotifyMotionArgs args;
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6991 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6992}
6993
Arthur Hung6cd19a42019-08-30 19:04:12 +08006994
Arthur Hung6cd19a42019-08-30 19:04:12 +08006995
Arthur Hung421eb1c2020-01-16 00:09:42 +08006996TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006997 addConfigurationProperty("touch.deviceType", "touchScreen");
6998 prepareDisplay(DISPLAY_ORIENTATION_0);
6999 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007000 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007001
7002 NotifyMotionArgs motionArgs;
7003
7004 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7005 // finger down
7006 processId(mapper, 1);
7007 processPosition(mapper, x1, y1);
7008 processSync(mapper);
7009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7010 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7012
7013 // finger move
7014 processId(mapper, 1);
7015 processPosition(mapper, x2, y2);
7016 processSync(mapper);
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7020
7021 // finger up.
7022 processId(mapper, -1);
7023 processSync(mapper);
7024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7025 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7026 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7027
7028 // new finger down
7029 processId(mapper, 1);
7030 processPosition(mapper, x3, y3);
7031 processSync(mapper);
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7035}
7036
7037/**
7038 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
7039 * UP events should be ignored.
7040 */
7041TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007042 addConfigurationProperty("touch.deviceType", "touchScreen");
7043 prepareDisplay(DISPLAY_ORIENTATION_0);
7044 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007045 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007046
7047 NotifyMotionArgs motionArgs;
7048
7049 // default tool type is finger
7050 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7051 processId(mapper, 1);
7052 processPosition(mapper, x1, y1);
7053 processSync(mapper);
7054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7057
7058 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7059 processToolType(mapper, MT_TOOL_PALM);
7060 processSync(mapper);
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7062 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7063
7064 // Ignore the following MOVE and UP events if had detect a palm event.
7065 processId(mapper, 1);
7066 processPosition(mapper, x2, y2);
7067 processSync(mapper);
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7069
7070 // finger up.
7071 processId(mapper, -1);
7072 processSync(mapper);
7073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7074
7075 // new finger down
7076 processToolType(mapper, MT_TOOL_FINGER);
7077 processId(mapper, 1);
7078 processPosition(mapper, x3, y3);
7079 processSync(mapper);
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7081 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7083}
7084
arthurhungbf89a482020-04-17 17:37:55 +08007085/**
7086 * Test multi-touch should be canceled when received the MT_TOOL_PALM event from some finger,
7087 * and could be allowed again after all non-MT_TOOL_PALM is release and the new point is
7088 * MT_TOOL_FINGER.
7089 */
7090TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType2) {
7091 addConfigurationProperty("touch.deviceType", "touchScreen");
7092 prepareDisplay(DISPLAY_ORIENTATION_0);
7093 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7094 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7095
7096 NotifyMotionArgs motionArgs;
7097
7098 // default tool type is finger
7099 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7100 processId(mapper, 1);
7101 processPosition(mapper, x1, y1);
7102 processSync(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7104 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7106
7107 // Second finger down.
7108 processSlot(mapper, 1);
7109 processPosition(mapper, x2, y2);
7110 processId(mapper, 2);
7111 processSync(mapper);
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7113 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7114 motionArgs.action);
7115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7116
7117 // If the tool type of the first pointer changes to MT_TOOL_PALM,
7118 // the entire gesture should be aborted, so we expect to receive ACTION_CANCEL.
7119 processSlot(mapper, 0);
7120 processId(mapper, 1);
7121 processToolType(mapper, MT_TOOL_PALM);
7122 processSync(mapper);
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7124 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7125
7126 // Ignore the following MOVE and UP events if had detect a palm event.
7127 processSlot(mapper, 1);
7128 processId(mapper, 2);
7129 processPosition(mapper, x3, y3);
7130 processSync(mapper);
7131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7132
7133 // second finger up.
7134 processId(mapper, -1);
7135 processSync(mapper);
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7137
7138 // first finger move, but still in palm
7139 processSlot(mapper, 0);
7140 processId(mapper, 1);
7141 processPosition(mapper, x1 - 1, y1 - 1);
7142 processSync(mapper);
7143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7144
7145 // second finger down, expect as new finger down.
7146 processSlot(mapper, 1);
7147 processId(mapper, 2);
7148 processPosition(mapper, x2, y2);
7149 processSync(mapper);
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7151 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7153}
7154
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007155// --- MultiTouchInputMapperTest_ExternalDevice ---
7156
7157class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7158protected:
7159 virtual void SetUp() override {
7160 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7161 }
7162};
7163
7164/**
7165 * Expect fallback to internal viewport if device is external and external viewport is not present.
7166 */
7167TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7168 prepareAxes(POSITION);
7169 addConfigurationProperty("touch.deviceType", "touchScreen");
7170 prepareDisplay(DISPLAY_ORIENTATION_0);
7171 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7172
7173 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7174
7175 NotifyMotionArgs motionArgs;
7176
7177 // Expect the event to be sent to the internal viewport,
7178 // because an external viewport is not present.
7179 processPosition(mapper, 100, 100);
7180 processSync(mapper);
7181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7182 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7183
7184 // Expect the event to be sent to the external viewport if it is present.
7185 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
7186 processPosition(mapper, 100, 100);
7187 processSync(mapper);
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7189 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7190}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007191
7192/**
7193 * Test touch should not work if outside of surface.
7194 */
7195class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7196protected:
7197 void halfDisplayToCenterHorizontal(int32_t orientation) {
7198 std::optional<DisplayViewport> internalViewport =
7199 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
7200
7201 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7202 internalViewport->orientation = orientation;
7203 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7204 internalViewport->logicalLeft = 0;
7205 internalViewport->logicalTop = 0;
7206 internalViewport->logicalRight = DISPLAY_HEIGHT;
7207 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7208
7209 internalViewport->physicalLeft = 0;
7210 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7211 internalViewport->physicalRight = DISPLAY_HEIGHT;
7212 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7213
7214 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7215 internalViewport->deviceHeight = DISPLAY_WIDTH;
7216 } else {
7217 internalViewport->logicalLeft = 0;
7218 internalViewport->logicalTop = 0;
7219 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7220 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7221
7222 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7223 internalViewport->physicalTop = 0;
7224 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7225 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7226
7227 internalViewport->deviceWidth = DISPLAY_WIDTH;
7228 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7229 }
7230
7231 mFakePolicy->updateViewport(internalViewport.value());
7232 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7233 }
7234
7235 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7236 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7237 int32_t yExpected) {
7238 // touch on outside area should not work.
7239 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7242
7243 // touch on inside area should receive the event.
7244 NotifyMotionArgs args;
7245 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7246 processSync(mapper);
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7248 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7249 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7250
7251 // Reset.
7252 mapper.reset(ARBITRARY_TIME);
7253 }
7254};
7255
7256TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7257 addConfigurationProperty("touch.deviceType", "touchScreen");
7258 prepareDisplay(DISPLAY_ORIENTATION_0);
7259 prepareAxes(POSITION);
7260 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7261
7262 // Touch on center of normal display should work.
7263 const int32_t x = DISPLAY_WIDTH / 4;
7264 const int32_t y = DISPLAY_HEIGHT / 2;
7265 processPosition(mapper, toRawX(x), toRawY(y));
7266 processSync(mapper);
7267 NotifyMotionArgs args;
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7270 0.0f, 0.0f, 0.0f, 0.0f));
7271 // Reset.
7272 mapper.reset(ARBITRARY_TIME);
7273
7274 // Let physical display be different to device, and make surface and physical could be 1:1.
7275 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7276
7277 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7278 const int32_t yExpected = y;
7279 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7280}
7281
7282TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7283 addConfigurationProperty("touch.deviceType", "touchScreen");
7284 prepareDisplay(DISPLAY_ORIENTATION_0);
7285 prepareAxes(POSITION);
7286 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7287
7288 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7289 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7290
7291 const int32_t x = DISPLAY_WIDTH / 4;
7292 const int32_t y = DISPLAY_HEIGHT / 2;
7293
7294 // expect x/y = swap x/y then reverse y.
7295 const int32_t xExpected = y;
7296 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7297 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7298}
7299
7300TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7301 addConfigurationProperty("touch.deviceType", "touchScreen");
7302 prepareDisplay(DISPLAY_ORIENTATION_0);
7303 prepareAxes(POSITION);
7304 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7305
7306 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7307 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7308
7309 const int32_t x = DISPLAY_WIDTH / 4;
7310 const int32_t y = DISPLAY_HEIGHT / 2;
7311
7312 // expect x/y = swap x/y then reverse x.
7313 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7314 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7315 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7316}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007317} // namespace android