blob: 96d86b68fc18c32194b4a109266ddc879137266c [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();
Arthur Hungaab25622020-01-16 11:22:11 +08001751 mTestListener = new TestInputListener(50ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001752
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001753 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001754 ASSERT_EQ(mReader->start(), OK);
1755
1756 // Since this test is run on a real device, all the input devices connected
1757 // to the test device will show up in mReader. We wait for those input devices to
1758 // show up before beginning the tests.
1759 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1760 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1761 }
1762
1763 virtual void TearDown() override {
1764 ASSERT_EQ(mReader->stop(), OK);
1765 mTestListener.clear();
1766 mFakePolicy.clear();
1767 }
1768};
1769
1770TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1771 // An invalid input device that is only used for this test.
1772 class InvalidUinputDevice : public UinputDevice {
1773 public:
1774 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1775
1776 private:
1777 void configureDevice(int fd, uinput_user_dev* device) override {}
1778 };
1779
1780 const size_t numDevices = mFakePolicy->getInputDevices().size();
1781
1782 // UinputDevice does not set any event or key bits, so InputReader should not
1783 // consider it as a valid device.
1784 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1785 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1786 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1787 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1788
1789 invalidDevice.reset();
1790 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1791 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1792 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1793}
1794
1795TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1796 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1797
1798 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1799 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1800 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1801 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1802
1803 // Find the test device by its name.
1804 std::vector<InputDeviceInfo> inputDevices;
1805 mReader->getInputDevices(inputDevices);
1806 InputDeviceInfo* keyboardInfo = nullptr;
1807 const char* keyboardName = keyboard->getName();
1808 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1809 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1810 keyboardInfo = &inputDevices[i];
1811 break;
1812 }
1813 }
1814 ASSERT_NE(keyboardInfo, nullptr);
1815 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1816 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1817 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1818
1819 keyboard.reset();
1820 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1821 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1822 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1823}
1824
1825TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1826 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1827 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1828
1829 NotifyConfigurationChangedArgs configChangedArgs;
1830 ASSERT_NO_FATAL_FAILURE(
1831 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001832 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001833 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1834
1835 NotifyKeyArgs keyArgs;
1836 keyboard->pressAndReleaseHomeKey();
1837 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1838 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001839 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001840 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001841 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1842 prevTimestamp = keyArgs.eventTime;
1843
1844 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1845 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001846 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001847 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1848}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849
Arthur Hungaab25622020-01-16 11:22:11 +08001850// --- TouchProcessTest ---
1851class TouchIntegrationTest : public InputReaderIntegrationTest {
1852protected:
1853 static const int32_t FIRST_SLOT = 0;
1854 static const int32_t SECOND_SLOT = 1;
1855 static const int32_t FIRST_TRACKING_ID = 0;
1856 static const int32_t SECOND_TRACKING_ID = 1;
1857 const std::string UNIQUE_ID = "local:0";
1858
1859 virtual void SetUp() override {
1860 InputReaderIntegrationTest::SetUp();
1861 // At least add an internal display.
1862 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1863 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
1864 ViewportType::VIEWPORT_INTERNAL);
1865
1866 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1867 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1868 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1869 }
1870
1871 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1872 int32_t orientation, const std::string& uniqueId,
1873 std::optional<uint8_t> physicalPort,
1874 ViewportType viewportType) {
1875 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1876 physicalPort, viewportType);
1877 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1878 }
1879
1880 std::unique_ptr<UinputTouchScreen> mDevice;
1881};
1882
1883TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1884 NotifyMotionArgs args;
1885 const Point centerPoint = mDevice->getCenterPoint();
1886
1887 // ACTION_DOWN
1888 mDevice->sendDown(centerPoint);
1889 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1890 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1891
1892 // ACTION_MOVE
1893 mDevice->sendMove(centerPoint + Point(1, 1));
1894 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1896
1897 // ACTION_UP
1898 mDevice->sendUp();
1899 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1900 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1901}
1902
1903TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1904 NotifyMotionArgs args;
1905 const Point centerPoint = mDevice->getCenterPoint();
1906
1907 // ACTION_DOWN
1908 mDevice->sendDown(centerPoint);
1909 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1911
1912 // ACTION_POINTER_DOWN (Second slot)
1913 const Point secondPoint = centerPoint + Point(100, 100);
1914 mDevice->sendSlot(SECOND_SLOT);
1915 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1916 mDevice->sendDown(secondPoint + Point(1, 1));
1917 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1918 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1919 args.action);
1920
1921 // ACTION_MOVE (Second slot)
1922 mDevice->sendMove(secondPoint);
1923 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1925
1926 // ACTION_POINTER_UP (Second slot)
1927 mDevice->sendUp();
1928 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1929 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1930 args.action);
1931
1932 // ACTION_UP
1933 mDevice->sendSlot(FIRST_SLOT);
1934 mDevice->sendUp();
1935 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1936 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1937}
1938
1939TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1940 NotifyMotionArgs args;
1941 const Point centerPoint = mDevice->getCenterPoint();
1942
1943 // ACTION_DOWN
1944 mDevice->sendDown(centerPoint);
1945 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1946 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1947
1948 // ACTION_POINTER_DOWN (Second slot)
1949 const Point secondPoint = centerPoint + Point(100, 100);
1950 mDevice->sendSlot(SECOND_SLOT);
1951 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1952 mDevice->sendDown(secondPoint);
1953 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1954 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1955 args.action);
1956
1957 // ACTION_MOVE (Second slot)
1958 mDevice->sendMove(secondPoint + Point(1, 1));
1959 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1961
1962 // Send MT_TOOL_PALM, which indicates that the touch IC has determined this to be a grip event.
1963 // Expect to receive ACTION_CANCEL, to abort the entire gesture.
1964 mDevice->sendToolType(MT_TOOL_PALM);
1965 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1966 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, args.action);
1967
1968 // ACTION_POINTER_UP (Second slot)
1969 mDevice->sendUp();
1970
1971 // ACTION_UP
1972 mDevice->sendSlot(FIRST_SLOT);
1973 mDevice->sendUp();
1974
1975 // Expect no event received after abort the entire gesture.
1976 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1977}
1978
Michael Wrightd02c5b62014-02-10 15:10:22 -08001979// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001980class InputDeviceTest : public testing::Test {
1981protected:
1982 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001983 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001984 static const int32_t DEVICE_ID;
1985 static const int32_t DEVICE_GENERATION;
1986 static const int32_t DEVICE_CONTROLLER_NUMBER;
1987 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001988 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001989
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001990 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001991 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001992 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001993 FakeInputReaderContext* mFakeContext;
1994
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001995 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001996
Prabir Pradhan28efc192019-11-05 01:10:04 +00001997 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001998 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001999 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002000 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002001 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2002
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002003 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002004 InputDeviceIdentifier identifier;
2005 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002006 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002007 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2008 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002009 }
2010
Prabir Pradhan28efc192019-11-05 01:10:04 +00002011 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002012 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002013 delete mFakeContext;
2014 mFakeListener.clear();
2015 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002016 }
2017};
2018
2019const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002020const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002021const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002022const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2023const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
2024const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
2025 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002026const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002027
2028TEST_F(InputDeviceTest, ImmutableProperties) {
2029 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002030 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002031 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002032}
2033
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002034TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2035 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002036}
2037
Michael Wrightd02c5b62014-02-10 15:10:22 -08002038TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2039 // Configuration.
2040 InputReaderConfiguration config;
2041 mDevice->configure(ARBITRARY_TIME, &config, 0);
2042
2043 // Reset.
2044 mDevice->reset(ARBITRARY_TIME);
2045
2046 NotifyDeviceResetArgs resetArgs;
2047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2048 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2049 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2050
2051 // Metadata.
2052 ASSERT_TRUE(mDevice->isIgnored());
2053 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2054
2055 InputDeviceInfo info;
2056 mDevice->getDeviceInfo(&info);
2057 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002058 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002059 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2060 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2061
2062 // State queries.
2063 ASSERT_EQ(0, mDevice->getMetaState());
2064
2065 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2066 << "Ignored device should return unknown key code state.";
2067 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2068 << "Ignored device should return unknown scan code state.";
2069 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2070 << "Ignored device should return unknown switch state.";
2071
2072 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2073 uint8_t flags[2] = { 0, 1 };
2074 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2075 << "Ignored device should never mark any key codes.";
2076 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2077 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2078}
2079
2080TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2081 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002082 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002083
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002084 FakeInputMapper& mapper1 =
2085 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002086 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2087 mapper1.setMetaState(AMETA_ALT_ON);
2088 mapper1.addSupportedKeyCode(AKEYCODE_A);
2089 mapper1.addSupportedKeyCode(AKEYCODE_B);
2090 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2091 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2092 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2093 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2094 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002095
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002096 FakeInputMapper& mapper2 =
2097 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002098 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002099
2100 InputReaderConfiguration config;
2101 mDevice->configure(ARBITRARY_TIME, &config, 0);
2102
2103 String8 propertyValue;
2104 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2105 << "Device should have read configuration during configuration phase.";
2106 ASSERT_STREQ("value", propertyValue.string());
2107
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002108 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2109 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002110
2111 // Reset
2112 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002113 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2114 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002115
2116 NotifyDeviceResetArgs resetArgs;
2117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2118 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2119 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2120
2121 // Metadata.
2122 ASSERT_FALSE(mDevice->isIgnored());
2123 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2124
2125 InputDeviceInfo info;
2126 mDevice->getDeviceInfo(&info);
2127 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002128 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002129 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2130 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2131
2132 // State queries.
2133 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2134 << "Should query mappers and combine meta states.";
2135
2136 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2137 << "Should return unknown key code state when source not supported.";
2138 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2139 << "Should return unknown scan code state when source not supported.";
2140 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2141 << "Should return unknown switch state when source not supported.";
2142
2143 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2144 << "Should query mapper when source is supported.";
2145 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2146 << "Should query mapper when source is supported.";
2147 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2148 << "Should query mapper when source is supported.";
2149
2150 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2151 uint8_t flags[4] = { 0, 0, 0, 1 };
2152 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2153 << "Should do nothing when source is unsupported.";
2154 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2155 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2156 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2157 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2158
2159 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2160 << "Should query mapper when source is supported.";
2161 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2162 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2163 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2164 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2165
2166 // Event handling.
2167 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002168 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002169 mDevice->process(&event, 1);
2170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002171 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2172 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002173}
2174
Arthur Hung2c9a3342019-07-23 14:18:59 +08002175// A single input device is associated with a specific display. Check that:
2176// 1. Device is disabled if the viewport corresponding to the associated display is not found
2177// 2. Device is disabled when setEnabled API is called
2178TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002179 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002180
2181 // First Configuration.
2182 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2183
2184 // Device should be enabled by default.
2185 ASSERT_TRUE(mDevice->isEnabled());
2186
2187 // Prepare associated info.
2188 constexpr uint8_t hdmi = 1;
2189 const std::string UNIQUE_ID = "local:1";
2190
2191 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2192 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2193 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2194 // Device should be disabled because it is associated with a specific display via
2195 // input port <-> display port association, but the corresponding display is not found
2196 ASSERT_FALSE(mDevice->isEnabled());
2197
2198 // Prepare displays.
2199 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2200 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2201 ViewportType::VIEWPORT_INTERNAL);
2202 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2203 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2204 ASSERT_TRUE(mDevice->isEnabled());
2205
2206 // Device should be disabled after set disable.
2207 mFakePolicy->addDisabledDevice(mDevice->getId());
2208 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2209 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2210 ASSERT_FALSE(mDevice->isEnabled());
2211
2212 // Device should still be disabled even found the associated display.
2213 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2214 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2215 ASSERT_FALSE(mDevice->isEnabled());
2216}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002217
2218// --- InputMapperTest ---
2219
2220class InputMapperTest : public testing::Test {
2221protected:
2222 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002223 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002224 static const int32_t DEVICE_ID;
2225 static const int32_t DEVICE_GENERATION;
2226 static const int32_t DEVICE_CONTROLLER_NUMBER;
2227 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002228 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002229
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002230 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002231 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002232 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002233 FakeInputReaderContext* mFakeContext;
2234 InputDevice* mDevice;
2235
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002236 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002237 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002239 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002240 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2241 InputDeviceIdentifier identifier;
2242 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002243 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002244 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002245
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002246 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002247 }
2248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002249 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2250
Prabir Pradhan28efc192019-11-05 01:10:04 +00002251 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002252 delete mDevice;
2253 delete mFakeContext;
2254 mFakeListener.clear();
2255 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256 }
2257
2258 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002259 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002260 }
2261
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002262 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002263 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2264 mFakeContext->updatePointerDisplay();
2265 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002266 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2267 }
2268
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002269 template <class T, typename... Args>
2270 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002271 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002272 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002273 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002274 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002275 }
2276
2277 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002278 int32_t orientation, const std::string& uniqueId,
2279 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002280 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002281 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002282 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2283 }
2284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002285 void clearViewports() {
2286 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002287 }
2288
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002289 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2290 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002291 RawEvent event;
2292 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002293 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002294 event.type = type;
2295 event.code = code;
2296 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002297 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002298 }
2299
2300 static void assertMotionRange(const InputDeviceInfo& info,
2301 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2302 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002303 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002304 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2305 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2306 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2307 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2308 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2309 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2310 }
2311
2312 static void assertPointerCoords(const PointerCoords& coords,
2313 float x, float y, float pressure, float size,
2314 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2315 float orientation, float distance) {
2316 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2317 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2318 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2319 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2320 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2321 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2322 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2323 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2324 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2325 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2326 }
2327
2328 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2329 float actualX, actualY;
2330 controller->getPosition(&actualX, &actualY);
2331 ASSERT_NEAR(x, actualX, 1);
2332 ASSERT_NEAR(y, actualY, 1);
2333 }
2334};
2335
2336const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002337const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002338const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002339const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2340const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2341const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002342const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343
2344// --- SwitchInputMapperTest ---
2345
2346class SwitchInputMapperTest : public InputMapperTest {
2347protected:
2348};
2349
2350TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002351 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002353 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002354}
2355
2356TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002357 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002359 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002360 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002361
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002362 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002363 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002364}
2365
2366TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002367 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002369 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2370 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2371 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2372 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002373
2374 NotifySwitchArgs args;
2375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2376 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002377 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2378 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379 args.switchMask);
2380 ASSERT_EQ(uint32_t(0), args.policyFlags);
2381}
2382
2383
2384// --- KeyboardInputMapperTest ---
2385
2386class KeyboardInputMapperTest : public InputMapperTest {
2387protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002388 const std::string UNIQUE_ID = "local:0";
2389
2390 void prepareDisplay(int32_t orientation);
2391
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002392 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002393 int32_t originalKeyCode, int32_t rotatedKeyCode,
2394 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395};
2396
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002397/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2398 * orientation.
2399 */
2400void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2401 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002402 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002403}
2404
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002405void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002406 int32_t originalScanCode, int32_t originalKeyCode,
2407 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 NotifyKeyArgs args;
2409
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002410 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2412 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2413 ASSERT_EQ(originalScanCode, args.scanCode);
2414 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002415 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002417 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2419 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2420 ASSERT_EQ(originalScanCode, args.scanCode);
2421 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002422 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002423}
2424
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002426 KeyboardInputMapper& mapper =
2427 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2428 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002429
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002430 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431}
2432
2433TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2434 const int32_t USAGE_A = 0x070004;
2435 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002436 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2437 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002439 KeyboardInputMapper& mapper =
2440 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2441 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002442
2443 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002444 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445 NotifyKeyArgs args;
2446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2447 ASSERT_EQ(DEVICE_ID, args.deviceId);
2448 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2449 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2450 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2451 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2452 ASSERT_EQ(KEY_HOME, args.scanCode);
2453 ASSERT_EQ(AMETA_NONE, args.metaState);
2454 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2455 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2456 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2457
2458 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002459 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2461 ASSERT_EQ(DEVICE_ID, args.deviceId);
2462 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2463 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2464 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2465 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2466 ASSERT_EQ(KEY_HOME, args.scanCode);
2467 ASSERT_EQ(AMETA_NONE, args.metaState);
2468 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2469 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2470 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2471
2472 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002473 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2474 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2476 ASSERT_EQ(DEVICE_ID, args.deviceId);
2477 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2478 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2479 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2480 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2481 ASSERT_EQ(0, args.scanCode);
2482 ASSERT_EQ(AMETA_NONE, args.metaState);
2483 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2484 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2485 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2486
2487 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002488 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2489 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2491 ASSERT_EQ(DEVICE_ID, args.deviceId);
2492 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2493 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2494 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2495 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2496 ASSERT_EQ(0, args.scanCode);
2497 ASSERT_EQ(AMETA_NONE, args.metaState);
2498 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2499 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2500 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2501
2502 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002503 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2504 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2506 ASSERT_EQ(DEVICE_ID, args.deviceId);
2507 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2508 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2509 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2510 ASSERT_EQ(0, args.keyCode);
2511 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2512 ASSERT_EQ(AMETA_NONE, args.metaState);
2513 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2514 ASSERT_EQ(0U, args.policyFlags);
2515 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2516
2517 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002518 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2519 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2521 ASSERT_EQ(DEVICE_ID, args.deviceId);
2522 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2523 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2524 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2525 ASSERT_EQ(0, args.keyCode);
2526 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2527 ASSERT_EQ(AMETA_NONE, args.metaState);
2528 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2529 ASSERT_EQ(0U, args.policyFlags);
2530 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2531}
2532
2533TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002534 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002536
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002537 KeyboardInputMapper& mapper =
2538 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2539 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540
2541 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002542 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543
2544 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002545 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546 NotifyKeyArgs args;
2547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2548 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002549 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002550 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2551
2552 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002553 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2555 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002556 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002557
2558 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002559 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2561 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002562 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563
2564 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002565 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2567 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002568 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002569 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2570}
2571
2572TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002573 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002578 KeyboardInputMapper& mapper =
2579 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2580 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002582 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2584 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2586 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2588 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2590 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2591}
2592
2593TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002594 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2595 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2596 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2597 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002600 KeyboardInputMapper& mapper =
2601 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2602 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002604 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002605 ASSERT_NO_FATAL_FAILURE(
2606 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2608 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2610 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2612 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002614 clearViewports();
2615 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002616 ASSERT_NO_FATAL_FAILURE(
2617 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2618 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2619 AKEYCODE_DPAD_UP, DISPLAY_ID));
2620 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2621 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2622 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2623 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002625 clearViewports();
2626 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002627 ASSERT_NO_FATAL_FAILURE(
2628 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2629 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2630 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2631 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2632 AKEYCODE_DPAD_UP, DISPLAY_ID));
2633 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2634 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002636 clearViewports();
2637 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002638 ASSERT_NO_FATAL_FAILURE(
2639 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2640 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2641 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2642 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2643 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2644 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2645 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646
2647 // Special case: if orientation changes while key is down, we still emit the same keycode
2648 // in the key up as we did in the key down.
2649 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002650 clearViewports();
2651 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002652 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2654 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2655 ASSERT_EQ(KEY_UP, args.scanCode);
2656 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2657
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002658 clearViewports();
2659 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002660 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2662 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2663 ASSERT_EQ(KEY_UP, args.scanCode);
2664 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2665}
2666
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002667TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2668 // If the keyboard is not orientation aware,
2669 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002670 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002671
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002672 KeyboardInputMapper& mapper =
2673 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2674 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002675 NotifyKeyArgs args;
2676
2677 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002678 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002680 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2682 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2683
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002684 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002685 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002687 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2689 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2690}
2691
2692TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2693 // If the keyboard is orientation aware,
2694 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002695 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002696
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002697 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002698 KeyboardInputMapper& mapper =
2699 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2700 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002701 NotifyKeyArgs args;
2702
2703 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2704 // ^--- already checked by the previous test
2705
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002706 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002707 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002708 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002710 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2712 ASSERT_EQ(DISPLAY_ID, args.displayId);
2713
2714 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002715 clearViewports();
2716 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002717 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002718 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002720 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2722 ASSERT_EQ(newDisplayId, args.displayId);
2723}
2724
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002726 KeyboardInputMapper& mapper =
2727 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2728 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002730 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002731 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002733 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002734 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735}
2736
2737TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002738 KeyboardInputMapper& mapper =
2739 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2740 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002742 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002743 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002745 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002746 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747}
2748
2749TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002750 KeyboardInputMapper& mapper =
2751 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2752 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002754 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755
2756 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2757 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002758 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759 ASSERT_TRUE(flags[0]);
2760 ASSERT_FALSE(flags[1]);
2761}
2762
2763TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002764 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2765 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2766 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2767 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2768 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2769 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002771 KeyboardInputMapper& mapper =
2772 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2773 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774
2775 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002776 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2777 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2778 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779
2780 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002781 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2782 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002783 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2784 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2785 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002786 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787
2788 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002789 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2790 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002791 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2792 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2793 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002794 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795
2796 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002797 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2798 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002799 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2800 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002802 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803
2804 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002805 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2806 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002807 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2808 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2809 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002810 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811
2812 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002813 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2814 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002815 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2817 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002818 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819
2820 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002821 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2822 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827}
2828
Arthur Hung2c9a3342019-07-23 14:18:59 +08002829TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2830 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002831 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2832 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2833 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2834 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002835
2836 // keyboard 2.
2837 const std::string USB2 = "USB2";
2838 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002839 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002840 InputDeviceIdentifier identifier;
2841 identifier.name = "KEYBOARD2";
2842 identifier.location = USB2;
2843 std::unique_ptr<InputDevice> device2 =
2844 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002845 identifier);
2846 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2847 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2848 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2849 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2850 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002852 KeyboardInputMapper& mapper =
2853 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2854 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002856 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002857 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002858 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002859 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2860 device2->reset(ARBITRARY_TIME);
2861
2862 // Prepared displays and associated info.
2863 constexpr uint8_t hdmi1 = 0;
2864 constexpr uint8_t hdmi2 = 1;
2865 const std::string SECONDARY_UNIQUE_ID = "local:1";
2866
2867 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2868 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2869
2870 // No associated display viewport found, should disable the device.
2871 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2872 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2873 ASSERT_FALSE(device2->isEnabled());
2874
2875 // Prepare second display.
2876 constexpr int32_t newDisplayId = 2;
2877 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2878 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2879 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2880 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2881 // Default device will reconfigure above, need additional reconfiguration for another device.
2882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2883 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2884
2885 // Device should be enabled after the associated display is found.
2886 ASSERT_TRUE(mDevice->isEnabled());
2887 ASSERT_TRUE(device2->isEnabled());
2888
2889 // Test pad key events
2890 ASSERT_NO_FATAL_FAILURE(
2891 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2892 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2893 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2894 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2895 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2896 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2897 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2898
2899 ASSERT_NO_FATAL_FAILURE(
2900 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2901 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2902 AKEYCODE_DPAD_RIGHT, newDisplayId));
2903 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2904 AKEYCODE_DPAD_DOWN, newDisplayId));
2905 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2906 AKEYCODE_DPAD_LEFT, newDisplayId));
2907}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002908
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002909// --- KeyboardInputMapperTest_ExternalDevice ---
2910
2911class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2912protected:
2913 virtual void SetUp() override {
2914 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2915 }
2916};
2917
2918TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002919 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2920 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002921
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002922 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2923 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2924 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2925 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002926
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002927 KeyboardInputMapper& mapper =
2928 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2929 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002930
2931 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2932 NotifyKeyArgs args;
2933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2934 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2935
2936 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2938 ASSERT_EQ(uint32_t(0), args.policyFlags);
2939
2940 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2942 ASSERT_EQ(uint32_t(0), args.policyFlags);
2943
2944 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2946 ASSERT_EQ(uint32_t(0), args.policyFlags);
2947
2948 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2950 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2951
2952 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2954 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2955}
2956
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002957TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002958 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002959
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002960 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2961 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2962 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002963
Powei Fengd041c5d2019-05-03 17:11:33 -07002964 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002965 KeyboardInputMapper& mapper =
2966 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2967 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002968
2969 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2970 NotifyKeyArgs args;
2971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2972 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2973
2974 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2976 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2977
2978 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2980 ASSERT_EQ(uint32_t(0), args.policyFlags);
2981
2982 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2984 ASSERT_EQ(uint32_t(0), args.policyFlags);
2985
2986 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2988 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2989
2990 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2992 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2993}
2994
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995// --- CursorInputMapperTest ---
2996
2997class CursorInputMapperTest : public InputMapperTest {
2998protected:
2999 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3000
3001 sp<FakePointerController> mFakePointerController;
3002
Prabir Pradhan28efc192019-11-05 01:10:04 +00003003 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004 InputMapperTest::SetUp();
3005
3006 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003007 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008 }
3009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003010 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3011 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003012
3013 void prepareDisplay(int32_t orientation) {
3014 const std::string uniqueId = "local:0";
3015 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
3016 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3017 orientation, uniqueId, NO_PORT, viewportType);
3018 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019};
3020
3021const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003023void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3024 int32_t originalY, int32_t rotatedX,
3025 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 NotifyMotionArgs args;
3027
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003028 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3029 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3030 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3034 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3035 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3036 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3037}
3038
3039TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003041 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003043 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044}
3045
3046TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003048 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003050 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051}
3052
3053TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003055 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056
3057 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003058 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059
3060 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003061 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3062 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3064 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3065
3066 // When the bounds are set, then there should be a valid motion range.
3067 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3068
3069 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003070 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
3072 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3073 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3074 1, 800 - 1, 0.0f, 0.0f));
3075 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3076 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3077 2, 480 - 1, 0.0f, 0.0f));
3078 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3079 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3080 0.0f, 1.0f, 0.0f, 0.0f));
3081}
3082
3083TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086
3087 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
3090 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3091 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3092 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3093 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3094 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3095 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3096 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3097 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3098 0.0f, 1.0f, 0.0f, 0.0f));
3099}
3100
3101TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003103 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104
3105 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3106
3107 NotifyMotionArgs args;
3108
3109 // Button press.
3110 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003111 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3112 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3114 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3115 ASSERT_EQ(DEVICE_ID, args.deviceId);
3116 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3117 ASSERT_EQ(uint32_t(0), args.policyFlags);
3118 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3119 ASSERT_EQ(0, args.flags);
3120 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3121 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3122 ASSERT_EQ(0, args.edgeFlags);
3123 ASSERT_EQ(uint32_t(1), args.pointerCount);
3124 ASSERT_EQ(0, args.pointerProperties[0].id);
3125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3127 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3128 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3129 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3130 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3131
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3133 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3134 ASSERT_EQ(DEVICE_ID, args.deviceId);
3135 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3136 ASSERT_EQ(uint32_t(0), args.policyFlags);
3137 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3138 ASSERT_EQ(0, args.flags);
3139 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3140 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3141 ASSERT_EQ(0, args.edgeFlags);
3142 ASSERT_EQ(uint32_t(1), args.pointerCount);
3143 ASSERT_EQ(0, args.pointerProperties[0].id);
3144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3146 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3147 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3148 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3149 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3150
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003152 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3153 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3155 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3156 ASSERT_EQ(DEVICE_ID, args.deviceId);
3157 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3158 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3160 ASSERT_EQ(0, args.flags);
3161 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3162 ASSERT_EQ(0, args.buttonState);
3163 ASSERT_EQ(0, args.edgeFlags);
3164 ASSERT_EQ(uint32_t(1), args.pointerCount);
3165 ASSERT_EQ(0, args.pointerProperties[0].id);
3166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3168 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3169 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3170 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3171 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3172
3173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3174 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3175 ASSERT_EQ(DEVICE_ID, args.deviceId);
3176 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3177 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3179 ASSERT_EQ(0, args.flags);
3180 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3181 ASSERT_EQ(0, args.buttonState);
3182 ASSERT_EQ(0, args.edgeFlags);
3183 ASSERT_EQ(uint32_t(1), args.pointerCount);
3184 ASSERT_EQ(0, args.pointerProperties[0].id);
3185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3187 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3188 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3189 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3190 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3191}
3192
3193TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003195 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196
3197 NotifyMotionArgs args;
3198
3199 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003200 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3201 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3205 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3206
3207 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003208 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3209 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3213 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3214}
3215
3216TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003217 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003218 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219
3220 NotifyMotionArgs args;
3221
3222 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003223 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3224 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3226 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3228 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3229
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3233 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3234
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003236 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3237 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3241 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3242
3243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3246 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3247}
3248
3249TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003251 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
3253 NotifyMotionArgs args;
3254
3255 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003256 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3257 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3258 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3259 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3263 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3264 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3265
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3269 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3270 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3271
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003273 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3274 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3275 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3279 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3280 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3281
3282 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003283 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3284 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3288 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3289
3290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3293 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3294}
3295
3296TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003298 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003300 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3302 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3303 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3304 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3305 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3306 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3307 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3308 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3309}
3310
3311TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 addConfigurationProperty("cursor.mode", "navigation");
3313 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003314 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003316 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3318 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3319 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3320 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3321 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3322 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3323 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3324 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3325
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003326 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3328 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3329 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3330 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3331 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3332 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3333 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3334 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3335
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003336 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3338 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3339 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3340 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3341 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3345
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003346 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3350 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3351 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3352 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3354 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3355}
3356
3357TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360
3361 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3362 mFakePointerController->setPosition(100, 200);
3363 mFakePointerController->setButtonState(0);
3364
3365 NotifyMotionArgs motionArgs;
3366 NotifyKeyArgs keyArgs;
3367
3368 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003369 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3370 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3372 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3373 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3374 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3376 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3377
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3380 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3381 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3383 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3384
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003385 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3386 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003388 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389 ASSERT_EQ(0, motionArgs.buttonState);
3390 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3392 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3393
3394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003395 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396 ASSERT_EQ(0, motionArgs.buttonState);
3397 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3399 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3400
3401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003403 ASSERT_EQ(0, motionArgs.buttonState);
3404 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3406 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3407
3408 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003409 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3410 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3411 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3413 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3414 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3415 motionArgs.buttonState);
3416 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3417 mFakePointerController->getButtonState());
3418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3419 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3420
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3423 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3424 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3425 mFakePointerController->getButtonState());
3426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3427 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3428
3429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3431 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3432 motionArgs.buttonState);
3433 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3434 mFakePointerController->getButtonState());
3435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3436 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3437
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003438 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3439 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3443 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3445 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3446
3447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003449 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3450 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3452 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3453
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003454 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3455 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3458 ASSERT_EQ(0, motionArgs.buttonState);
3459 ASSERT_EQ(0, mFakePointerController->getButtonState());
3460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3461 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 -08003462 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3463 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003464
3465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466 ASSERT_EQ(0, motionArgs.buttonState);
3467 ASSERT_EQ(0, mFakePointerController->getButtonState());
3468 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3470 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003471
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3473 ASSERT_EQ(0, motionArgs.buttonState);
3474 ASSERT_EQ(0, mFakePointerController->getButtonState());
3475 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3477 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3478
3479 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003480 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3481 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3483 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3484 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003485
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003487 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3489 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3491 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3492
3493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3495 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3496 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3498 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3499
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003500 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3501 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504 ASSERT_EQ(0, motionArgs.buttonState);
3505 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3507 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3508
3509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003511 ASSERT_EQ(0, motionArgs.buttonState);
3512 ASSERT_EQ(0, mFakePointerController->getButtonState());
3513
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3515 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3517 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3518 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3519
3520 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003521 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3522 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3524 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3525 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003526
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003528 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3530 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3532 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3533
3534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3535 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3536 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3537 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3539 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3540
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003541 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3542 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545 ASSERT_EQ(0, motionArgs.buttonState);
3546 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3548 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 -08003549
3550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3552 ASSERT_EQ(0, motionArgs.buttonState);
3553 ASSERT_EQ(0, mFakePointerController->getButtonState());
3554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3555 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3556
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3558 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3559 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3560
3561 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003562 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3563 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3565 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3566 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003567
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3571 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3573 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3574
3575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3576 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3577 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3578 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3580 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3581
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003582 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3583 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 ASSERT_EQ(0, motionArgs.buttonState);
3587 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3589 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 -08003590
3591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3593 ASSERT_EQ(0, motionArgs.buttonState);
3594 ASSERT_EQ(0, mFakePointerController->getButtonState());
3595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3596 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3597
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3599 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3600 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3601
3602 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003603 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3604 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3606 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3607 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003608
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003610 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3612 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3614 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3615
3616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3618 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3619 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3621 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3622
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003623 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3624 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003626 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_EQ(0, motionArgs.buttonState);
3628 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3630 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 -08003631
3632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3634 ASSERT_EQ(0, motionArgs.buttonState);
3635 ASSERT_EQ(0, mFakePointerController->getButtonState());
3636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3637 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3638
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3640 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3641 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3642}
3643
3644TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003646 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647
3648 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3649 mFakePointerController->setPosition(100, 200);
3650 mFakePointerController->setButtonState(0);
3651
3652 NotifyMotionArgs args;
3653
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003654 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3655 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3656 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003658 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3661 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3662 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3663}
3664
3665TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003666 addConfigurationProperty("cursor.mode", "pointer");
3667 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003668 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003669
3670 NotifyDeviceResetArgs resetArgs;
3671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3672 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3673 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3674
3675 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3676 mFakePointerController->setPosition(100, 200);
3677 mFakePointerController->setButtonState(0);
3678
3679 NotifyMotionArgs args;
3680
3681 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003682 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3683 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3684 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3686 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3689 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3690 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3691
3692 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003693 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3694 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3696 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3697 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3699 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3701 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3704 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3705
3706 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003707 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3708 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3710 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3711 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3713 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3715 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3716 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3718 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3719
3720 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003721 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3722 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3723 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3725 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3728 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3729 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3730
3731 // Disable pointer capture and check that the device generation got bumped
3732 // and events are generated the usual way.
3733 const uint32_t generation = mFakeContext->getGeneration();
3734 mFakePolicy->setPointerCapture(false);
3735 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3736 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3737
3738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3739 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3740 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3741
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003742 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3743 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3744 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3746 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3749 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3750 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3751}
3752
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003753TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003754 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003755
Garfield Tan888a6a42020-01-09 11:39:16 -08003756 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003757 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003758 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3759 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3760 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3761 ViewportType::VIEWPORT_EXTERNAL);
3762 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3763 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3764
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003765 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3766 mFakePointerController->setPosition(100, 200);
3767 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003768
3769 NotifyMotionArgs args;
3770 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3771 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3772 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3774 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3777 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3778 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3779 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3780}
3781
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782// --- TouchInputMapperTest ---
3783
3784class TouchInputMapperTest : public InputMapperTest {
3785protected:
3786 static const int32_t RAW_X_MIN;
3787 static const int32_t RAW_X_MAX;
3788 static const int32_t RAW_Y_MIN;
3789 static const int32_t RAW_Y_MAX;
3790 static const int32_t RAW_TOUCH_MIN;
3791 static const int32_t RAW_TOUCH_MAX;
3792 static const int32_t RAW_TOOL_MIN;
3793 static const int32_t RAW_TOOL_MAX;
3794 static const int32_t RAW_PRESSURE_MIN;
3795 static const int32_t RAW_PRESSURE_MAX;
3796 static const int32_t RAW_ORIENTATION_MIN;
3797 static const int32_t RAW_ORIENTATION_MAX;
3798 static const int32_t RAW_DISTANCE_MIN;
3799 static const int32_t RAW_DISTANCE_MAX;
3800 static const int32_t RAW_TILT_MIN;
3801 static const int32_t RAW_TILT_MAX;
3802 static const int32_t RAW_ID_MIN;
3803 static const int32_t RAW_ID_MAX;
3804 static const int32_t RAW_SLOT_MIN;
3805 static const int32_t RAW_SLOT_MAX;
3806 static const float X_PRECISION;
3807 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003808 static const float X_PRECISION_VIRTUAL;
3809 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810
3811 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003812 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813
3814 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3815
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003816 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003817 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003818
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819 enum Axes {
3820 POSITION = 1 << 0,
3821 TOUCH = 1 << 1,
3822 TOOL = 1 << 2,
3823 PRESSURE = 1 << 3,
3824 ORIENTATION = 1 << 4,
3825 MINOR = 1 << 5,
3826 ID = 1 << 6,
3827 DISTANCE = 1 << 7,
3828 TILT = 1 << 8,
3829 SLOT = 1 << 9,
3830 TOOL_TYPE = 1 << 10,
3831 };
3832
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003833 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3834 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003835 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003837 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838 int32_t toRawX(float displayX);
3839 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003840 float toCookedX(float rawX, float rawY);
3841 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003843 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003844 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003845 float toDisplayY(int32_t rawY, int32_t displayHeight);
3846
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847};
3848
3849const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3850const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3851const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3852const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3853const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3854const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3855const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3856const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003857const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3858const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3860const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3861const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3862const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3863const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3864const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3865const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3866const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3867const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3868const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3869const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3870const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003871const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3872 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3873const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3874 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003875const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3876 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877
3878const float TouchInputMapperTest::GEOMETRIC_SCALE =
3879 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3880 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3881
3882const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3883 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3884 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3885};
3886
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003887void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003888 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003889 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3890}
3891
3892void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3893 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3894 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895}
3896
Santos Cordonfa5cf462017-04-05 10:37:00 -07003897void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003898 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3899 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003900 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003901}
3902
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003904 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3905 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3906 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3907 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908}
3909
Jason Gerecke489fda82012-09-07 17:19:40 -07003910void TouchInputMapperTest::prepareLocationCalibration() {
3911 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3912}
3913
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914int32_t TouchInputMapperTest::toRawX(float displayX) {
3915 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3916}
3917
3918int32_t TouchInputMapperTest::toRawY(float displayY) {
3919 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3920}
3921
Jason Gerecke489fda82012-09-07 17:19:40 -07003922float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3923 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3924 return rawX;
3925}
3926
3927float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3928 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3929 return rawY;
3930}
3931
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003933 return toDisplayX(rawX, DISPLAY_WIDTH);
3934}
3935
3936float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3937 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938}
3939
3940float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003941 return toDisplayY(rawY, DISPLAY_HEIGHT);
3942}
3943
3944float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3945 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946}
3947
3948
3949// --- SingleTouchInputMapperTest ---
3950
3951class SingleTouchInputMapperTest : public TouchInputMapperTest {
3952protected:
3953 void prepareButtons();
3954 void prepareAxes(int axes);
3955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003956 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3957 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3958 void processUp(SingleTouchInputMapper& mappery);
3959 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3960 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3961 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3962 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3963 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3964 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965};
3966
3967void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003968 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969}
3970
3971void SingleTouchInputMapperTest::prepareAxes(int axes) {
3972 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003973 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
3974 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975 }
3976 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003977 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
3978 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979 }
3980 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003981 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
3982 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983 }
3984 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003985 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
3986 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003987 }
3988 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003989 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3990 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 }
3992}
3993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003994void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003995 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3996 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3997 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998}
3999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004000void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004001 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4002 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003}
4004
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004005void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004006 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007}
4008
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004009void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004010 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011}
4012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004013void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4014 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004015 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016}
4017
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004018void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004019 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020}
4021
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004022void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4023 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004024 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4025 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026}
4027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004028void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4029 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004030 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031}
4032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004033void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004034 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035}
4036
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038 prepareButtons();
4039 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004040 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004042 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043}
4044
4045TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004046 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4047 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 prepareButtons();
4049 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004050 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004052 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053}
4054
4055TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 prepareButtons();
4057 prepareAxes(POSITION);
4058 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004059 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004061 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062}
4063
4064TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 prepareButtons();
4066 prepareAxes(POSITION);
4067 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004068 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004070 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071}
4072
4073TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074 addConfigurationProperty("touch.deviceType", "touchScreen");
4075 prepareDisplay(DISPLAY_ORIENTATION_0);
4076 prepareButtons();
4077 prepareAxes(POSITION);
4078 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004079 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080
4081 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004082 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083
4084 // Virtual key is down.
4085 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4086 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4087 processDown(mapper, x, y);
4088 processSync(mapper);
4089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004091 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092
4093 // Virtual key is up.
4094 processUp(mapper);
4095 processSync(mapper);
4096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4097
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004098 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099}
4100
4101TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 addConfigurationProperty("touch.deviceType", "touchScreen");
4103 prepareDisplay(DISPLAY_ORIENTATION_0);
4104 prepareButtons();
4105 prepareAxes(POSITION);
4106 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004107 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108
4109 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004110 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111
4112 // Virtual key is down.
4113 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4114 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4115 processDown(mapper, x, y);
4116 processSync(mapper);
4117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004119 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120
4121 // Virtual key is up.
4122 processUp(mapper);
4123 processSync(mapper);
4124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004126 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127}
4128
4129TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 addConfigurationProperty("touch.deviceType", "touchScreen");
4131 prepareDisplay(DISPLAY_ORIENTATION_0);
4132 prepareButtons();
4133 prepareAxes(POSITION);
4134 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004135 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136
4137 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4138 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004139 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 ASSERT_TRUE(flags[0]);
4141 ASSERT_FALSE(flags[1]);
4142}
4143
4144TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145 addConfigurationProperty("touch.deviceType", "touchScreen");
4146 prepareDisplay(DISPLAY_ORIENTATION_0);
4147 prepareButtons();
4148 prepareAxes(POSITION);
4149 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004150 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151
4152 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4153
4154 NotifyKeyArgs args;
4155
4156 // Press virtual key.
4157 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4158 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4159 processDown(mapper, x, y);
4160 processSync(mapper);
4161
4162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4163 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4164 ASSERT_EQ(DEVICE_ID, args.deviceId);
4165 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4166 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4167 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4168 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4169 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4170 ASSERT_EQ(KEY_HOME, args.scanCode);
4171 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4172 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4173
4174 // Release virtual key.
4175 processUp(mapper);
4176 processSync(mapper);
4177
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4179 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4180 ASSERT_EQ(DEVICE_ID, args.deviceId);
4181 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4182 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4183 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4184 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4185 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4186 ASSERT_EQ(KEY_HOME, args.scanCode);
4187 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4188 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4189
4190 // Should not have sent any motions.
4191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4192}
4193
4194TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 addConfigurationProperty("touch.deviceType", "touchScreen");
4196 prepareDisplay(DISPLAY_ORIENTATION_0);
4197 prepareButtons();
4198 prepareAxes(POSITION);
4199 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004200 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201
4202 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4203
4204 NotifyKeyArgs keyArgs;
4205
4206 // Press virtual key.
4207 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4208 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4209 processDown(mapper, x, y);
4210 processSync(mapper);
4211
4212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4213 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4214 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4215 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4216 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4217 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4218 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4219 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4220 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4222 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4223
4224 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4225 // into the display area.
4226 y -= 100;
4227 processMove(mapper, x, y);
4228 processSync(mapper);
4229
4230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4231 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4232 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4233 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4234 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4235 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4236 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4237 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4238 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4239 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4241 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4242
4243 NotifyMotionArgs motionArgs;
4244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4246 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4247 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4248 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4250 ASSERT_EQ(0, motionArgs.flags);
4251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4252 ASSERT_EQ(0, motionArgs.buttonState);
4253 ASSERT_EQ(0, motionArgs.edgeFlags);
4254 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4255 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4258 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4259 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4260 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4261 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4262
4263 // Keep moving out of bounds. Should generate a pointer move.
4264 y -= 50;
4265 processMove(mapper, x, y);
4266 processSync(mapper);
4267
4268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4269 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4270 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4274 ASSERT_EQ(0, motionArgs.flags);
4275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4276 ASSERT_EQ(0, motionArgs.buttonState);
4277 ASSERT_EQ(0, motionArgs.edgeFlags);
4278 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4279 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4282 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4283 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4284 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4286
4287 // Release out of bounds. Should generate a pointer up.
4288 processUp(mapper);
4289 processSync(mapper);
4290
4291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4293 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4294 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4295 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4296 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4297 ASSERT_EQ(0, motionArgs.flags);
4298 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4299 ASSERT_EQ(0, motionArgs.buttonState);
4300 ASSERT_EQ(0, motionArgs.edgeFlags);
4301 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4302 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4305 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4306 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4307 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4309
4310 // Should not have sent any more keys or motions.
4311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4313}
4314
4315TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 addConfigurationProperty("touch.deviceType", "touchScreen");
4317 prepareDisplay(DISPLAY_ORIENTATION_0);
4318 prepareButtons();
4319 prepareAxes(POSITION);
4320 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004321 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322
4323 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4324
4325 NotifyMotionArgs motionArgs;
4326
4327 // Initially go down out of bounds.
4328 int32_t x = -10;
4329 int32_t y = -10;
4330 processDown(mapper, x, y);
4331 processSync(mapper);
4332
4333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4334
4335 // Move into the display area. Should generate a pointer down.
4336 x = 50;
4337 y = 75;
4338 processMove(mapper, x, y);
4339 processSync(mapper);
4340
4341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4343 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4344 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4345 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4346 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4347 ASSERT_EQ(0, motionArgs.flags);
4348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4349 ASSERT_EQ(0, motionArgs.buttonState);
4350 ASSERT_EQ(0, motionArgs.edgeFlags);
4351 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4352 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4355 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4356 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4357 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4358 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4359
4360 // Release. Should generate a pointer up.
4361 processUp(mapper);
4362 processSync(mapper);
4363
4364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4366 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4367 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4368 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4369 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4370 ASSERT_EQ(0, motionArgs.flags);
4371 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4372 ASSERT_EQ(0, motionArgs.buttonState);
4373 ASSERT_EQ(0, motionArgs.edgeFlags);
4374 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4375 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4378 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4379 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4380 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4381 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4382
4383 // Should not have sent any more keys or motions.
4384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4386}
4387
Santos Cordonfa5cf462017-04-05 10:37:00 -07004388TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004389 addConfigurationProperty("touch.deviceType", "touchScreen");
4390 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4391
4392 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4393 prepareButtons();
4394 prepareAxes(POSITION);
4395 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004396 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004397
4398 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4399
4400 NotifyMotionArgs motionArgs;
4401
4402 // Down.
4403 int32_t x = 100;
4404 int32_t y = 125;
4405 processDown(mapper, x, y);
4406 processSync(mapper);
4407
4408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4410 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4411 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4412 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4413 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4414 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4415 ASSERT_EQ(0, motionArgs.flags);
4416 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4417 ASSERT_EQ(0, motionArgs.buttonState);
4418 ASSERT_EQ(0, motionArgs.edgeFlags);
4419 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4420 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4423 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4424 1, 0, 0, 0, 0, 0, 0, 0));
4425 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4426 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4427 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4428
4429 // Move.
4430 x += 50;
4431 y += 75;
4432 processMove(mapper, x, y);
4433 processSync(mapper);
4434
4435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4436 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4437 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4438 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4439 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4440 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4442 ASSERT_EQ(0, motionArgs.flags);
4443 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4444 ASSERT_EQ(0, motionArgs.buttonState);
4445 ASSERT_EQ(0, motionArgs.edgeFlags);
4446 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4447 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4450 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4451 1, 0, 0, 0, 0, 0, 0, 0));
4452 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4453 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4455
4456 // Up.
4457 processUp(mapper);
4458 processSync(mapper);
4459
4460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4462 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4463 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4464 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4465 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4466 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4467 ASSERT_EQ(0, motionArgs.flags);
4468 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4469 ASSERT_EQ(0, motionArgs.buttonState);
4470 ASSERT_EQ(0, motionArgs.edgeFlags);
4471 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4472 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4475 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4476 1, 0, 0, 0, 0, 0, 0, 0));
4477 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4478 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4480
4481 // Should not have sent any more keys or motions.
4482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4484}
4485
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487 addConfigurationProperty("touch.deviceType", "touchScreen");
4488 prepareDisplay(DISPLAY_ORIENTATION_0);
4489 prepareButtons();
4490 prepareAxes(POSITION);
4491 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004492 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493
4494 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4495
4496 NotifyMotionArgs motionArgs;
4497
4498 // Down.
4499 int32_t x = 100;
4500 int32_t y = 125;
4501 processDown(mapper, x, y);
4502 processSync(mapper);
4503
4504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4505 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4506 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4507 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4508 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4510 ASSERT_EQ(0, motionArgs.flags);
4511 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4512 ASSERT_EQ(0, motionArgs.buttonState);
4513 ASSERT_EQ(0, motionArgs.edgeFlags);
4514 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4515 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4518 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4519 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4520 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4522
4523 // Move.
4524 x += 50;
4525 y += 75;
4526 processMove(mapper, x, y);
4527 processSync(mapper);
4528
4529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4530 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4531 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4532 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4533 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4535 ASSERT_EQ(0, motionArgs.flags);
4536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4537 ASSERT_EQ(0, motionArgs.buttonState);
4538 ASSERT_EQ(0, motionArgs.edgeFlags);
4539 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4540 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4543 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4544 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4545 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4546 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4547
4548 // Up.
4549 processUp(mapper);
4550 processSync(mapper);
4551
4552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4553 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4554 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4555 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4556 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4557 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4558 ASSERT_EQ(0, motionArgs.flags);
4559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4560 ASSERT_EQ(0, motionArgs.buttonState);
4561 ASSERT_EQ(0, motionArgs.edgeFlags);
4562 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4563 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4566 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4567 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4568 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4570
4571 // Should not have sent any more keys or motions.
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4574}
4575
4576TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577 addConfigurationProperty("touch.deviceType", "touchScreen");
4578 prepareButtons();
4579 prepareAxes(POSITION);
4580 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004581 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582
4583 NotifyMotionArgs args;
4584
4585 // Rotation 90.
4586 prepareDisplay(DISPLAY_ORIENTATION_90);
4587 processDown(mapper, toRawX(50), toRawY(75));
4588 processSync(mapper);
4589
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4591 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4592 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4593
4594 processUp(mapper);
4595 processSync(mapper);
4596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4597}
4598
4599TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600 addConfigurationProperty("touch.deviceType", "touchScreen");
4601 prepareButtons();
4602 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004603 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604
4605 NotifyMotionArgs args;
4606
4607 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004608 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609 prepareDisplay(DISPLAY_ORIENTATION_0);
4610 processDown(mapper, toRawX(50), toRawY(75));
4611 processSync(mapper);
4612
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4614 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4615 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4616
4617 processUp(mapper);
4618 processSync(mapper);
4619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4620
4621 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004622 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623 prepareDisplay(DISPLAY_ORIENTATION_90);
4624 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4625 processSync(mapper);
4626
4627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4628 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4629 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4630
4631 processUp(mapper);
4632 processSync(mapper);
4633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4634
4635 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004636 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 prepareDisplay(DISPLAY_ORIENTATION_180);
4638 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4639 processSync(mapper);
4640
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4642 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4643 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4644
4645 processUp(mapper);
4646 processSync(mapper);
4647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4648
4649 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004650 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 prepareDisplay(DISPLAY_ORIENTATION_270);
4652 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4653 processSync(mapper);
4654
4655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4656 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4657 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4658
4659 processUp(mapper);
4660 processSync(mapper);
4661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4662}
4663
4664TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 addConfigurationProperty("touch.deviceType", "touchScreen");
4666 prepareDisplay(DISPLAY_ORIENTATION_0);
4667 prepareButtons();
4668 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004669 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670
4671 // These calculations are based on the input device calibration documentation.
4672 int32_t rawX = 100;
4673 int32_t rawY = 200;
4674 int32_t rawPressure = 10;
4675 int32_t rawToolMajor = 12;
4676 int32_t rawDistance = 2;
4677 int32_t rawTiltX = 30;
4678 int32_t rawTiltY = 110;
4679
4680 float x = toDisplayX(rawX);
4681 float y = toDisplayY(rawY);
4682 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4683 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4684 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4685 float distance = float(rawDistance);
4686
4687 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4688 float tiltScale = M_PI / 180;
4689 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4690 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4691 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4692 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4693
4694 processDown(mapper, rawX, rawY);
4695 processPressure(mapper, rawPressure);
4696 processToolMajor(mapper, rawToolMajor);
4697 processDistance(mapper, rawDistance);
4698 processTilt(mapper, rawTiltX, rawTiltY);
4699 processSync(mapper);
4700
4701 NotifyMotionArgs args;
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4704 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4705 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4706}
4707
Jason Gerecke489fda82012-09-07 17:19:40 -07004708TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004709 addConfigurationProperty("touch.deviceType", "touchScreen");
4710 prepareDisplay(DISPLAY_ORIENTATION_0);
4711 prepareLocationCalibration();
4712 prepareButtons();
4713 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004714 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004715
4716 int32_t rawX = 100;
4717 int32_t rawY = 200;
4718
4719 float x = toDisplayX(toCookedX(rawX, rawY));
4720 float y = toDisplayY(toCookedY(rawX, rawY));
4721
4722 processDown(mapper, rawX, rawY);
4723 processSync(mapper);
4724
4725 NotifyMotionArgs args;
4726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4728 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4729}
4730
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 addConfigurationProperty("touch.deviceType", "touchScreen");
4733 prepareDisplay(DISPLAY_ORIENTATION_0);
4734 prepareButtons();
4735 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004736 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737
4738 NotifyMotionArgs motionArgs;
4739 NotifyKeyArgs keyArgs;
4740
4741 processDown(mapper, 100, 200);
4742 processSync(mapper);
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4744 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4745 ASSERT_EQ(0, motionArgs.buttonState);
4746
4747 // press BTN_LEFT, release BTN_LEFT
4748 processKey(mapper, BTN_LEFT, 1);
4749 processSync(mapper);
4750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4752 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4753
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4755 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4756 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4757
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758 processKey(mapper, BTN_LEFT, 0);
4759 processSync(mapper);
4760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004761 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004763
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004766 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767
4768 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4769 processKey(mapper, BTN_RIGHT, 1);
4770 processKey(mapper, BTN_MIDDLE, 1);
4771 processSync(mapper);
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4774 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4775 motionArgs.buttonState);
4776
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4778 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4779 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4780
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4782 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4783 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4784 motionArgs.buttonState);
4785
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 processKey(mapper, BTN_RIGHT, 0);
4787 processSync(mapper);
4788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004791
4792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795
4796 processKey(mapper, BTN_MIDDLE, 0);
4797 processSync(mapper);
4798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004801
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004804 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805
4806 // press BTN_BACK, release BTN_BACK
4807 processKey(mapper, BTN_BACK, 1);
4808 processSync(mapper);
4809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4810 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4811 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004812
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004815 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4816
4817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4818 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4819 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820
4821 processKey(mapper, BTN_BACK, 0);
4822 processSync(mapper);
4823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004824 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004826
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004829 ASSERT_EQ(0, motionArgs.buttonState);
4830
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4832 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4833 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4834
4835 // press BTN_SIDE, release BTN_SIDE
4836 processKey(mapper, BTN_SIDE, 1);
4837 processSync(mapper);
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4839 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4840 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004841
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004844 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4845
4846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4847 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4848 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849
4850 processKey(mapper, BTN_SIDE, 0);
4851 processSync(mapper);
4852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004853 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004855
4856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004858 ASSERT_EQ(0, motionArgs.buttonState);
4859
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4861 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4862 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4863
4864 // press BTN_FORWARD, release BTN_FORWARD
4865 processKey(mapper, BTN_FORWARD, 1);
4866 processSync(mapper);
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4868 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4869 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004870
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004873 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4874
4875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4876 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4877 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878
4879 processKey(mapper, BTN_FORWARD, 0);
4880 processSync(mapper);
4881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004882 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004884
4885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004887 ASSERT_EQ(0, motionArgs.buttonState);
4888
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4890 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4891 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4892
4893 // press BTN_EXTRA, release BTN_EXTRA
4894 processKey(mapper, BTN_EXTRA, 1);
4895 processSync(mapper);
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4897 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4898 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004899
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004902 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4903
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4905 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4906 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907
4908 processKey(mapper, BTN_EXTRA, 0);
4909 processSync(mapper);
4910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004913
4914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004916 ASSERT_EQ(0, motionArgs.buttonState);
4917
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4919 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4920 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4921
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4923
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 // press BTN_STYLUS, release BTN_STYLUS
4925 processKey(mapper, BTN_STYLUS, 1);
4926 processSync(mapper);
4927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004929 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4930
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4932 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4933 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934
4935 processKey(mapper, BTN_STYLUS, 0);
4936 processSync(mapper);
4937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004938 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004940
4941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004943 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944
4945 // press BTN_STYLUS2, release BTN_STYLUS2
4946 processKey(mapper, BTN_STYLUS2, 1);
4947 processSync(mapper);
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004950 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4951
4952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4953 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4954 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955
4956 processKey(mapper, BTN_STYLUS2, 0);
4957 processSync(mapper);
4958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004961
4962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004964 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965
4966 // release touch
4967 processUp(mapper);
4968 processSync(mapper);
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4970 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4971 ASSERT_EQ(0, motionArgs.buttonState);
4972}
4973
4974TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 addConfigurationProperty("touch.deviceType", "touchScreen");
4976 prepareDisplay(DISPLAY_ORIENTATION_0);
4977 prepareButtons();
4978 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004979 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980
4981 NotifyMotionArgs motionArgs;
4982
4983 // default tool type is finger
4984 processDown(mapper, 100, 200);
4985 processSync(mapper);
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4989
4990 // eraser
4991 processKey(mapper, BTN_TOOL_RUBBER, 1);
4992 processSync(mapper);
4993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4996
4997 // stylus
4998 processKey(mapper, BTN_TOOL_RUBBER, 0);
4999 processKey(mapper, BTN_TOOL_PEN, 1);
5000 processSync(mapper);
5001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5004
5005 // brush
5006 processKey(mapper, BTN_TOOL_PEN, 0);
5007 processKey(mapper, BTN_TOOL_BRUSH, 1);
5008 processSync(mapper);
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5012
5013 // pencil
5014 processKey(mapper, BTN_TOOL_BRUSH, 0);
5015 processKey(mapper, BTN_TOOL_PENCIL, 1);
5016 processSync(mapper);
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5020
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005021 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 processKey(mapper, BTN_TOOL_PENCIL, 0);
5023 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5024 processSync(mapper);
5025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5028
5029 // mouse
5030 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5031 processKey(mapper, BTN_TOOL_MOUSE, 1);
5032 processSync(mapper);
5033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5036
5037 // lens
5038 processKey(mapper, BTN_TOOL_MOUSE, 0);
5039 processKey(mapper, BTN_TOOL_LENS, 1);
5040 processSync(mapper);
5041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5044
5045 // double-tap
5046 processKey(mapper, BTN_TOOL_LENS, 0);
5047 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5048 processSync(mapper);
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5052
5053 // triple-tap
5054 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5055 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5056 processSync(mapper);
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5060
5061 // quad-tap
5062 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5063 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5064 processSync(mapper);
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5068
5069 // finger
5070 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5071 processKey(mapper, BTN_TOOL_FINGER, 1);
5072 processSync(mapper);
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5076
5077 // stylus trumps finger
5078 processKey(mapper, BTN_TOOL_PEN, 1);
5079 processSync(mapper);
5080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5083
5084 // eraser trumps stylus
5085 processKey(mapper, BTN_TOOL_RUBBER, 1);
5086 processSync(mapper);
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5090
5091 // mouse trumps eraser
5092 processKey(mapper, BTN_TOOL_MOUSE, 1);
5093 processSync(mapper);
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5097
5098 // back to default tool type
5099 processKey(mapper, BTN_TOOL_MOUSE, 0);
5100 processKey(mapper, BTN_TOOL_RUBBER, 0);
5101 processKey(mapper, BTN_TOOL_PEN, 0);
5102 processKey(mapper, BTN_TOOL_FINGER, 0);
5103 processSync(mapper);
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5107}
5108
5109TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 addConfigurationProperty("touch.deviceType", "touchScreen");
5111 prepareDisplay(DISPLAY_ORIENTATION_0);
5112 prepareButtons();
5113 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005114 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005115 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116
5117 NotifyMotionArgs motionArgs;
5118
5119 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5120 processKey(mapper, BTN_TOOL_FINGER, 1);
5121 processMove(mapper, 100, 200);
5122 processSync(mapper);
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5124 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5126 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5127
5128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5129 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5131 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5132
5133 // move a little
5134 processMove(mapper, 150, 250);
5135 processSync(mapper);
5136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5137 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5139 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5140
5141 // down when BTN_TOUCH is pressed, pressure defaults to 1
5142 processKey(mapper, BTN_TOUCH, 1);
5143 processSync(mapper);
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5145 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5147 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5148
5149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5152 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5153
5154 // up when BTN_TOUCH is released, hover restored
5155 processKey(mapper, BTN_TOUCH, 0);
5156 processSync(mapper);
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5158 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5160 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5161
5162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5163 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5165 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5166
5167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5168 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5170 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5171
5172 // exit hover when pointer goes away
5173 processKey(mapper, BTN_TOOL_FINGER, 0);
5174 processSync(mapper);
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5176 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5178 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5179}
5180
5181TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182 addConfigurationProperty("touch.deviceType", "touchScreen");
5183 prepareDisplay(DISPLAY_ORIENTATION_0);
5184 prepareButtons();
5185 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005186 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005187
5188 NotifyMotionArgs motionArgs;
5189
5190 // initially hovering because pressure is 0
5191 processDown(mapper, 100, 200);
5192 processPressure(mapper, 0);
5193 processSync(mapper);
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5195 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5197 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5198
5199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5200 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5202 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5203
5204 // move a little
5205 processMove(mapper, 150, 250);
5206 processSync(mapper);
5207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5208 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5210 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5211
5212 // down when pressure is non-zero
5213 processPressure(mapper, RAW_PRESSURE_MAX);
5214 processSync(mapper);
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5216 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5218 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5219
5220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5221 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5223 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5224
5225 // up when pressure becomes 0, hover restored
5226 processPressure(mapper, 0);
5227 processSync(mapper);
5228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5229 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5231 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5232
5233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5234 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5236 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5237
5238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5239 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5241 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5242
5243 // exit hover when pointer goes away
5244 processUp(mapper);
5245 processSync(mapper);
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5247 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5249 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5250}
5251
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252// --- MultiTouchInputMapperTest ---
5253
5254class MultiTouchInputMapperTest : public TouchInputMapperTest {
5255protected:
5256 void prepareAxes(int axes);
5257
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005258 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5259 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5260 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5261 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5262 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5263 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5264 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5265 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5266 void processId(MultiTouchInputMapper& mapper, int32_t id);
5267 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5268 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5269 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5270 void processMTSync(MultiTouchInputMapper& mapper);
5271 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272};
5273
5274void MultiTouchInputMapperTest::prepareAxes(int axes) {
5275 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005276 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5277 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005278 }
5279 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005280 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5281 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005283 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5284 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005285 }
5286 }
5287 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005288 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5289 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005291 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5292 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293 }
5294 }
5295 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5297 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298 }
5299 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005300 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5301 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302 }
5303 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005304 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5305 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306 }
5307 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005308 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5309 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310 }
5311 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005312 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5313 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314 }
5315 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005316 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317 }
5318}
5319
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005320void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5321 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005322 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5323 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324}
5325
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005326void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5327 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005328 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329}
5330
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005331void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5332 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005333 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334}
5335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005336void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005337 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338}
5339
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005340void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005341 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342}
5343
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5345 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005346 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347}
5348
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005349void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005350 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351}
5352
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005353void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005354 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355}
5356
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005357void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005358 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359}
5360
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005361void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005362 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363}
5364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005365void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005366 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005367}
5368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005369void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5370 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005371 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372}
5373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005375 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376}
5377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005378void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005379 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380}
5381
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383 addConfigurationProperty("touch.deviceType", "touchScreen");
5384 prepareDisplay(DISPLAY_ORIENTATION_0);
5385 prepareAxes(POSITION);
5386 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005387 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388
5389 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5390
5391 NotifyMotionArgs motionArgs;
5392
5393 // Two fingers down at once.
5394 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5395 processPosition(mapper, x1, y1);
5396 processMTSync(mapper);
5397 processPosition(mapper, x2, y2);
5398 processMTSync(mapper);
5399 processSync(mapper);
5400
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5403 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5404 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5405 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5406 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5407 ASSERT_EQ(0, motionArgs.flags);
5408 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5409 ASSERT_EQ(0, motionArgs.buttonState);
5410 ASSERT_EQ(0, motionArgs.edgeFlags);
5411 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5412 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5415 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5416 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5417 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5419
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5421 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5422 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5423 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5424 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5425 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5426 motionArgs.action);
5427 ASSERT_EQ(0, motionArgs.flags);
5428 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5429 ASSERT_EQ(0, motionArgs.buttonState);
5430 ASSERT_EQ(0, motionArgs.edgeFlags);
5431 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5432 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5434 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5437 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5439 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5440 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5441 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5442 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5443
5444 // Move.
5445 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5446 processPosition(mapper, x1, y1);
5447 processMTSync(mapper);
5448 processPosition(mapper, x2, y2);
5449 processMTSync(mapper);
5450 processSync(mapper);
5451
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5453 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5454 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5455 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5456 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5458 ASSERT_EQ(0, motionArgs.flags);
5459 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5460 ASSERT_EQ(0, motionArgs.buttonState);
5461 ASSERT_EQ(0, motionArgs.edgeFlags);
5462 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5463 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5465 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5468 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5470 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5471 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5472 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5473 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5474
5475 // First finger up.
5476 x2 += 15; y2 -= 20;
5477 processPosition(mapper, x2, y2);
5478 processMTSync(mapper);
5479 processSync(mapper);
5480
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5483 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5484 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5485 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5486 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5487 motionArgs.action);
5488 ASSERT_EQ(0, motionArgs.flags);
5489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5490 ASSERT_EQ(0, motionArgs.buttonState);
5491 ASSERT_EQ(0, motionArgs.edgeFlags);
5492 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5495 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5498 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5500 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5501 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5502 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5503 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5504
5505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5506 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5507 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5508 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5509 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5511 ASSERT_EQ(0, motionArgs.flags);
5512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5513 ASSERT_EQ(0, motionArgs.buttonState);
5514 ASSERT_EQ(0, motionArgs.edgeFlags);
5515 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5516 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5519 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5520 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5521 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5523
5524 // Move.
5525 x2 += 20; y2 -= 25;
5526 processPosition(mapper, x2, y2);
5527 processMTSync(mapper);
5528 processSync(mapper);
5529
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5531 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5532 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5533 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5534 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5536 ASSERT_EQ(0, motionArgs.flags);
5537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5538 ASSERT_EQ(0, motionArgs.buttonState);
5539 ASSERT_EQ(0, motionArgs.edgeFlags);
5540 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5541 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5544 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5545 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5546 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5547 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5548
5549 // New finger down.
5550 int32_t x3 = 700, y3 = 300;
5551 processPosition(mapper, x2, y2);
5552 processMTSync(mapper);
5553 processPosition(mapper, x3, y3);
5554 processMTSync(mapper);
5555 processSync(mapper);
5556
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5558 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5559 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5560 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5561 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5562 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5563 motionArgs.action);
5564 ASSERT_EQ(0, motionArgs.flags);
5565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5566 ASSERT_EQ(0, motionArgs.buttonState);
5567 ASSERT_EQ(0, motionArgs.edgeFlags);
5568 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5571 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5574 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5576 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5577 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5578 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5579 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5580
5581 // Second finger up.
5582 x3 += 30; y3 -= 20;
5583 processPosition(mapper, x3, y3);
5584 processMTSync(mapper);
5585 processSync(mapper);
5586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5588 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5589 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5590 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5591 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5592 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5593 motionArgs.action);
5594 ASSERT_EQ(0, motionArgs.flags);
5595 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5596 ASSERT_EQ(0, motionArgs.buttonState);
5597 ASSERT_EQ(0, motionArgs.edgeFlags);
5598 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5599 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5601 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5604 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5606 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5607 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5608 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5609 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5610
5611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5612 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5613 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5614 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5615 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5617 ASSERT_EQ(0, motionArgs.flags);
5618 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5619 ASSERT_EQ(0, motionArgs.buttonState);
5620 ASSERT_EQ(0, motionArgs.edgeFlags);
5621 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5622 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5625 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5626 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5627 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5629
5630 // Last finger up.
5631 processMTSync(mapper);
5632 processSync(mapper);
5633
5634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5635 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5636 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5637 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5638 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5639 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5640 ASSERT_EQ(0, motionArgs.flags);
5641 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5642 ASSERT_EQ(0, motionArgs.buttonState);
5643 ASSERT_EQ(0, motionArgs.edgeFlags);
5644 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5645 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5648 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5649 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5650 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5651 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5652
5653 // Should not have sent any more keys or motions.
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5656}
5657
5658TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659 addConfigurationProperty("touch.deviceType", "touchScreen");
5660 prepareDisplay(DISPLAY_ORIENTATION_0);
5661 prepareAxes(POSITION | ID);
5662 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005663 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664
5665 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5666
5667 NotifyMotionArgs motionArgs;
5668
5669 // Two fingers down at once.
5670 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5671 processPosition(mapper, x1, y1);
5672 processId(mapper, 1);
5673 processMTSync(mapper);
5674 processPosition(mapper, x2, y2);
5675 processId(mapper, 2);
5676 processMTSync(mapper);
5677 processSync(mapper);
5678
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5680 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5681 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5682 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5683 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5685 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5686
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5688 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5689 motionArgs.action);
5690 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5693 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5696 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5698 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5699
5700 // Move.
5701 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5702 processPosition(mapper, x1, y1);
5703 processId(mapper, 1);
5704 processMTSync(mapper);
5705 processPosition(mapper, x2, y2);
5706 processId(mapper, 2);
5707 processMTSync(mapper);
5708 processSync(mapper);
5709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5712 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5713 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5715 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5718 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5720 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5721
5722 // First finger up.
5723 x2 += 15; y2 -= 20;
5724 processPosition(mapper, x2, y2);
5725 processId(mapper, 2);
5726 processMTSync(mapper);
5727 processSync(mapper);
5728
5729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5730 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5731 motionArgs.action);
5732 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5733 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5735 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5738 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5740 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5741
5742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5744 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5745 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5748 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5749
5750 // Move.
5751 x2 += 20; y2 -= 25;
5752 processPosition(mapper, x2, y2);
5753 processId(mapper, 2);
5754 processMTSync(mapper);
5755 processSync(mapper);
5756
5757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5759 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5760 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5763 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5764
5765 // New finger down.
5766 int32_t x3 = 700, y3 = 300;
5767 processPosition(mapper, x2, y2);
5768 processId(mapper, 2);
5769 processMTSync(mapper);
5770 processPosition(mapper, x3, y3);
5771 processId(mapper, 3);
5772 processMTSync(mapper);
5773 processSync(mapper);
5774
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5776 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5777 motionArgs.action);
5778 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5781 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5784 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5786 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5787
5788 // Second finger up.
5789 x3 += 30; y3 -= 20;
5790 processPosition(mapper, x3, y3);
5791 processId(mapper, 3);
5792 processMTSync(mapper);
5793 processSync(mapper);
5794
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5797 motionArgs.action);
5798 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5799 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5801 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5804 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5806 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5807
5808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5810 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5811 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5814 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5815
5816 // Last finger up.
5817 processMTSync(mapper);
5818 processSync(mapper);
5819
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5821 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5822 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5823 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5826 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5827
5828 // Should not have sent any more keys or motions.
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5831}
5832
5833TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834 addConfigurationProperty("touch.deviceType", "touchScreen");
5835 prepareDisplay(DISPLAY_ORIENTATION_0);
5836 prepareAxes(POSITION | ID | SLOT);
5837 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005838 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839
5840 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5841
5842 NotifyMotionArgs motionArgs;
5843
5844 // Two fingers down at once.
5845 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5846 processPosition(mapper, x1, y1);
5847 processId(mapper, 1);
5848 processSlot(mapper, 1);
5849 processPosition(mapper, x2, y2);
5850 processId(mapper, 2);
5851 processSync(mapper);
5852
5853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5854 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5855 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5856 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5859 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5863 motionArgs.action);
5864 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5865 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5867 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5870 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5872 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5873
5874 // Move.
5875 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5876 processSlot(mapper, 0);
5877 processPosition(mapper, x1, y1);
5878 processSlot(mapper, 1);
5879 processPosition(mapper, x2, y2);
5880 processSync(mapper);
5881
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5884 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5885 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5887 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5890 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5892 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5893
5894 // First finger up.
5895 x2 += 15; y2 -= 20;
5896 processSlot(mapper, 0);
5897 processId(mapper, -1);
5898 processSlot(mapper, 1);
5899 processPosition(mapper, x2, y2);
5900 processSync(mapper);
5901
5902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5903 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5904 motionArgs.action);
5905 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5906 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5908 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5911 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5913 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5914
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5917 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5918 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5921 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5922
5923 // Move.
5924 x2 += 20; y2 -= 25;
5925 processPosition(mapper, x2, y2);
5926 processSync(mapper);
5927
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5930 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5931 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5934 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5935
5936 // New finger down.
5937 int32_t x3 = 700, y3 = 300;
5938 processPosition(mapper, x2, y2);
5939 processSlot(mapper, 0);
5940 processId(mapper, 3);
5941 processPosition(mapper, x3, y3);
5942 processSync(mapper);
5943
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5945 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5946 motionArgs.action);
5947 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5948 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5949 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5950 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5951 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5953 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5955 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5956
5957 // Second finger up.
5958 x3 += 30; y3 -= 20;
5959 processSlot(mapper, 1);
5960 processId(mapper, -1);
5961 processSlot(mapper, 0);
5962 processPosition(mapper, x3, y3);
5963 processSync(mapper);
5964
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5966 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5967 motionArgs.action);
5968 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5969 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5971 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5972 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5974 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5976 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5977
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5980 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5981 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5984 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5985
5986 // Last finger up.
5987 processId(mapper, -1);
5988 processSync(mapper);
5989
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5991 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5992 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5993 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5996 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5997
5998 // Should not have sent any more keys or motions.
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6001}
6002
6003TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004 addConfigurationProperty("touch.deviceType", "touchScreen");
6005 prepareDisplay(DISPLAY_ORIENTATION_0);
6006 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006007 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008
6009 // These calculations are based on the input device calibration documentation.
6010 int32_t rawX = 100;
6011 int32_t rawY = 200;
6012 int32_t rawTouchMajor = 7;
6013 int32_t rawTouchMinor = 6;
6014 int32_t rawToolMajor = 9;
6015 int32_t rawToolMinor = 8;
6016 int32_t rawPressure = 11;
6017 int32_t rawDistance = 0;
6018 int32_t rawOrientation = 3;
6019 int32_t id = 5;
6020
6021 float x = toDisplayX(rawX);
6022 float y = toDisplayY(rawY);
6023 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6024 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6025 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6026 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6027 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6028 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6029 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6030 float distance = float(rawDistance);
6031
6032 processPosition(mapper, rawX, rawY);
6033 processTouchMajor(mapper, rawTouchMajor);
6034 processTouchMinor(mapper, rawTouchMinor);
6035 processToolMajor(mapper, rawToolMajor);
6036 processToolMinor(mapper, rawToolMinor);
6037 processPressure(mapper, rawPressure);
6038 processOrientation(mapper, rawOrientation);
6039 processDistance(mapper, rawDistance);
6040 processId(mapper, id);
6041 processMTSync(mapper);
6042 processSync(mapper);
6043
6044 NotifyMotionArgs args;
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6046 ASSERT_EQ(0, args.pointerProperties[0].id);
6047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6048 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6049 orientation, distance));
6050}
6051
6052TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 addConfigurationProperty("touch.deviceType", "touchScreen");
6054 prepareDisplay(DISPLAY_ORIENTATION_0);
6055 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6056 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006057 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058
6059 // These calculations are based on the input device calibration documentation.
6060 int32_t rawX = 100;
6061 int32_t rawY = 200;
6062 int32_t rawTouchMajor = 140;
6063 int32_t rawTouchMinor = 120;
6064 int32_t rawToolMajor = 180;
6065 int32_t rawToolMinor = 160;
6066
6067 float x = toDisplayX(rawX);
6068 float y = toDisplayY(rawY);
6069 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6070 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6071 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6072 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6073 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6074
6075 processPosition(mapper, rawX, rawY);
6076 processTouchMajor(mapper, rawTouchMajor);
6077 processTouchMinor(mapper, rawTouchMinor);
6078 processToolMajor(mapper, rawToolMajor);
6079 processToolMinor(mapper, rawToolMinor);
6080 processMTSync(mapper);
6081 processSync(mapper);
6082
6083 NotifyMotionArgs args;
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6086 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6087}
6088
6089TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090 addConfigurationProperty("touch.deviceType", "touchScreen");
6091 prepareDisplay(DISPLAY_ORIENTATION_0);
6092 prepareAxes(POSITION | TOUCH | TOOL);
6093 addConfigurationProperty("touch.size.calibration", "diameter");
6094 addConfigurationProperty("touch.size.scale", "10");
6095 addConfigurationProperty("touch.size.bias", "160");
6096 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006097 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098
6099 // These calculations are based on the input device calibration documentation.
6100 // Note: We only provide a single common touch/tool value because the device is assumed
6101 // not to emit separate values for each pointer (isSummed = 1).
6102 int32_t rawX = 100;
6103 int32_t rawY = 200;
6104 int32_t rawX2 = 150;
6105 int32_t rawY2 = 250;
6106 int32_t rawTouchMajor = 5;
6107 int32_t rawToolMajor = 8;
6108
6109 float x = toDisplayX(rawX);
6110 float y = toDisplayY(rawY);
6111 float x2 = toDisplayX(rawX2);
6112 float y2 = toDisplayY(rawY2);
6113 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6114 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6115 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6116
6117 processPosition(mapper, rawX, rawY);
6118 processTouchMajor(mapper, rawTouchMajor);
6119 processToolMajor(mapper, rawToolMajor);
6120 processMTSync(mapper);
6121 processPosition(mapper, rawX2, rawY2);
6122 processTouchMajor(mapper, rawTouchMajor);
6123 processToolMajor(mapper, rawToolMajor);
6124 processMTSync(mapper);
6125 processSync(mapper);
6126
6127 NotifyMotionArgs args;
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6130
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6132 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6133 args.action);
6134 ASSERT_EQ(size_t(2), args.pointerCount);
6135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6136 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6138 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6139}
6140
6141TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142 addConfigurationProperty("touch.deviceType", "touchScreen");
6143 prepareDisplay(DISPLAY_ORIENTATION_0);
6144 prepareAxes(POSITION | TOUCH | TOOL);
6145 addConfigurationProperty("touch.size.calibration", "area");
6146 addConfigurationProperty("touch.size.scale", "43");
6147 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006148 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149
6150 // These calculations are based on the input device calibration documentation.
6151 int32_t rawX = 100;
6152 int32_t rawY = 200;
6153 int32_t rawTouchMajor = 5;
6154 int32_t rawToolMajor = 8;
6155
6156 float x = toDisplayX(rawX);
6157 float y = toDisplayY(rawY);
6158 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6159 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6160 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6161
6162 processPosition(mapper, rawX, rawY);
6163 processTouchMajor(mapper, rawTouchMajor);
6164 processToolMajor(mapper, rawToolMajor);
6165 processMTSync(mapper);
6166 processSync(mapper);
6167
6168 NotifyMotionArgs args;
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6171 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6172}
6173
6174TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 addConfigurationProperty("touch.deviceType", "touchScreen");
6176 prepareDisplay(DISPLAY_ORIENTATION_0);
6177 prepareAxes(POSITION | PRESSURE);
6178 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6179 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006180 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181
Michael Wrightaa449c92017-12-13 21:21:43 +00006182 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006183 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006184 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6185 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6186 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6187
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188 // These calculations are based on the input device calibration documentation.
6189 int32_t rawX = 100;
6190 int32_t rawY = 200;
6191 int32_t rawPressure = 60;
6192
6193 float x = toDisplayX(rawX);
6194 float y = toDisplayY(rawY);
6195 float pressure = float(rawPressure) * 0.01f;
6196
6197 processPosition(mapper, rawX, rawY);
6198 processPressure(mapper, rawPressure);
6199 processMTSync(mapper);
6200 processSync(mapper);
6201
6202 NotifyMotionArgs args;
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6205 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6206}
6207
6208TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 addConfigurationProperty("touch.deviceType", "touchScreen");
6210 prepareDisplay(DISPLAY_ORIENTATION_0);
6211 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006212 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213
6214 NotifyMotionArgs motionArgs;
6215 NotifyKeyArgs keyArgs;
6216
6217 processId(mapper, 1);
6218 processPosition(mapper, 100, 200);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6222 ASSERT_EQ(0, motionArgs.buttonState);
6223
6224 // press BTN_LEFT, release BTN_LEFT
6225 processKey(mapper, BTN_LEFT, 1);
6226 processSync(mapper);
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6229 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6230
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6233 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6234
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235 processKey(mapper, BTN_LEFT, 0);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006240
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006243 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244
6245 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6246 processKey(mapper, BTN_RIGHT, 1);
6247 processKey(mapper, BTN_MIDDLE, 1);
6248 processSync(mapper);
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6251 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6252 motionArgs.buttonState);
6253
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6256 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6257
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6259 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6260 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6261 motionArgs.buttonState);
6262
Michael Wrightd02c5b62014-02-10 15:10:22 -08006263 processKey(mapper, BTN_RIGHT, 0);
6264 processSync(mapper);
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006268
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272
6273 processKey(mapper, BTN_MIDDLE, 0);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006276 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006278
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006281 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282
6283 // press BTN_BACK, release BTN_BACK
6284 processKey(mapper, BTN_BACK, 1);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6287 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6288 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006289
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006292 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6293
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6296 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006297
6298 processKey(mapper, BTN_BACK, 0);
6299 processSync(mapper);
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006303
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306 ASSERT_EQ(0, motionArgs.buttonState);
6307
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6309 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6310 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6311
6312 // press BTN_SIDE, release BTN_SIDE
6313 processKey(mapper, BTN_SIDE, 1);
6314 processSync(mapper);
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6316 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6317 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006318
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006321 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6322
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6325 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326
6327 processKey(mapper, BTN_SIDE, 0);
6328 processSync(mapper);
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006332
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335 ASSERT_EQ(0, motionArgs.buttonState);
6336
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6338 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6339 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6340
6341 // press BTN_FORWARD, release BTN_FORWARD
6342 processKey(mapper, BTN_FORWARD, 1);
6343 processSync(mapper);
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6345 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6346 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006350 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6351
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6354 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355
6356 processKey(mapper, BTN_FORWARD, 0);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006359 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006361
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006364 ASSERT_EQ(0, motionArgs.buttonState);
6365
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6367 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6368 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6369
6370 // press BTN_EXTRA, release BTN_EXTRA
6371 processKey(mapper, BTN_EXTRA, 1);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6374 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6375 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006379 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6380
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6383 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384
6385 processKey(mapper, BTN_EXTRA, 0);
6386 processSync(mapper);
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006388 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006393 ASSERT_EQ(0, motionArgs.buttonState);
6394
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6396 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6397 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6398
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6400
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 // press BTN_STYLUS, release BTN_STYLUS
6402 processKey(mapper, BTN_STYLUS, 1);
6403 processSync(mapper);
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6407
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6410 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411
6412 processKey(mapper, BTN_STYLUS, 0);
6413 processSync(mapper);
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006420 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421
6422 // press BTN_STYLUS2, release BTN_STYLUS2
6423 processKey(mapper, BTN_STYLUS2, 1);
6424 processSync(mapper);
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6428
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6431 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432
6433 processKey(mapper, BTN_STYLUS2, 0);
6434 processSync(mapper);
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006436 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006441 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442
6443 // release touch
6444 processId(mapper, -1);
6445 processSync(mapper);
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6447 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6448 ASSERT_EQ(0, motionArgs.buttonState);
6449}
6450
6451TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006452 addConfigurationProperty("touch.deviceType", "touchScreen");
6453 prepareDisplay(DISPLAY_ORIENTATION_0);
6454 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006455 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456
6457 NotifyMotionArgs motionArgs;
6458
6459 // default tool type is finger
6460 processId(mapper, 1);
6461 processPosition(mapper, 100, 200);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6464 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6466
6467 // eraser
6468 processKey(mapper, BTN_TOOL_RUBBER, 1);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6473
6474 // stylus
6475 processKey(mapper, BTN_TOOL_RUBBER, 0);
6476 processKey(mapper, BTN_TOOL_PEN, 1);
6477 processSync(mapper);
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6481
6482 // brush
6483 processKey(mapper, BTN_TOOL_PEN, 0);
6484 processKey(mapper, BTN_TOOL_BRUSH, 1);
6485 processSync(mapper);
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6489
6490 // pencil
6491 processKey(mapper, BTN_TOOL_BRUSH, 0);
6492 processKey(mapper, BTN_TOOL_PENCIL, 1);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6497
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006498 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006499 processKey(mapper, BTN_TOOL_PENCIL, 0);
6500 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6501 processSync(mapper);
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6505
6506 // mouse
6507 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6508 processKey(mapper, BTN_TOOL_MOUSE, 1);
6509 processSync(mapper);
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6513
6514 // lens
6515 processKey(mapper, BTN_TOOL_MOUSE, 0);
6516 processKey(mapper, BTN_TOOL_LENS, 1);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6521
6522 // double-tap
6523 processKey(mapper, BTN_TOOL_LENS, 0);
6524 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6529
6530 // triple-tap
6531 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6532 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6537
6538 // quad-tap
6539 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6540 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6541 processSync(mapper);
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6545
6546 // finger
6547 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6548 processKey(mapper, BTN_TOOL_FINGER, 1);
6549 processSync(mapper);
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6553
6554 // stylus trumps finger
6555 processKey(mapper, BTN_TOOL_PEN, 1);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6560
6561 // eraser trumps stylus
6562 processKey(mapper, BTN_TOOL_RUBBER, 1);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6567
6568 // mouse trumps eraser
6569 processKey(mapper, BTN_TOOL_MOUSE, 1);
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6574
6575 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6576 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6577 processSync(mapper);
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6581
6582 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6583 processToolType(mapper, MT_TOOL_PEN);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6588
6589 // back to default tool type
6590 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6591 processKey(mapper, BTN_TOOL_MOUSE, 0);
6592 processKey(mapper, BTN_TOOL_RUBBER, 0);
6593 processKey(mapper, BTN_TOOL_PEN, 0);
6594 processKey(mapper, BTN_TOOL_FINGER, 0);
6595 processSync(mapper);
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6599}
6600
6601TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006602 addConfigurationProperty("touch.deviceType", "touchScreen");
6603 prepareDisplay(DISPLAY_ORIENTATION_0);
6604 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006605 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006606 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006607
6608 NotifyMotionArgs motionArgs;
6609
6610 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6611 processId(mapper, 1);
6612 processPosition(mapper, 100, 200);
6613 processSync(mapper);
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6617 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6618
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6622 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6623
6624 // move a little
6625 processPosition(mapper, 150, 250);
6626 processSync(mapper);
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6628 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6630 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6631
6632 // down when BTN_TOUCH is pressed, pressure defaults to 1
6633 processKey(mapper, BTN_TOUCH, 1);
6634 processSync(mapper);
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6636 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6638 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6639
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6641 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6643 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6644
6645 // up when BTN_TOUCH is released, hover restored
6646 processKey(mapper, BTN_TOUCH, 0);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6649 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6651 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6652
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6654 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6656 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6657
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6661 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6662
6663 // exit hover when pointer goes away
6664 processId(mapper, -1);
6665 processSync(mapper);
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6669 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6670}
6671
6672TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006673 addConfigurationProperty("touch.deviceType", "touchScreen");
6674 prepareDisplay(DISPLAY_ORIENTATION_0);
6675 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006676 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006677
6678 NotifyMotionArgs motionArgs;
6679
6680 // initially hovering because pressure is 0
6681 processId(mapper, 1);
6682 processPosition(mapper, 100, 200);
6683 processPressure(mapper, 0);
6684 processSync(mapper);
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6688 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6689
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6691 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6693 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6694
6695 // move a little
6696 processPosition(mapper, 150, 250);
6697 processSync(mapper);
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6701 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6702
6703 // down when pressure becomes non-zero
6704 processPressure(mapper, RAW_PRESSURE_MAX);
6705 processSync(mapper);
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6707 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6709 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6710
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6714 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6715
6716 // up when pressure becomes 0, hover restored
6717 processPressure(mapper, 0);
6718 processSync(mapper);
6719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6720 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6722 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6723
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6727 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6728
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6732 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6733
6734 // exit hover when pointer goes away
6735 processId(mapper, -1);
6736 processSync(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6738 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6740 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6741}
6742
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006743/**
6744 * Set the input device port <--> display port associations, and check that the
6745 * events are routed to the display that matches the display port.
6746 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6747 */
6748TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006749 const std::string usb2 = "USB2";
6750 const uint8_t hdmi1 = 0;
6751 const uint8_t hdmi2 = 1;
6752 const std::string secondaryUniqueId = "uniqueId2";
6753 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6754
6755 addConfigurationProperty("touch.deviceType", "touchScreen");
6756 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006757 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006758
6759 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6760 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6761
6762 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6763 // for this input device is specified, and the matching viewport is not present,
6764 // the input device should be disabled (at the mapper level).
6765
6766 // Add viewport for display 2 on hdmi2
6767 prepareSecondaryDisplay(type, hdmi2);
6768 // Send a touch event
6769 processPosition(mapper, 100, 100);
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6772
6773 // Add viewport for display 1 on hdmi1
6774 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6775 // Send a touch event again
6776 processPosition(mapper, 100, 100);
6777 processSync(mapper);
6778
6779 NotifyMotionArgs args;
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6781 ASSERT_EQ(DISPLAY_ID, args.displayId);
6782}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006783
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006784TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006785 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006786 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006787 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006788 fakePointerController->setPosition(100, 200);
6789 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006790 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6791
Garfield Tan888a6a42020-01-09 11:39:16 -08006792 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6793 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6794
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006795 prepareDisplay(DISPLAY_ORIENTATION_0);
6796 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006797 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006798
6799 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006800 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006801
6802 NotifyMotionArgs motionArgs;
6803 processPosition(mapper, 100, 100);
6804 processSync(mapper);
6805
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6807 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6808 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6809}
6810
Arthur Hung7c645402019-01-25 17:45:42 +08006811TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6812 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006813 prepareAxes(POSITION | ID | SLOT);
6814 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006815 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006816
6817 // Create the second touch screen device, and enable multi fingers.
6818 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006819 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006820 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006821 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006822 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006823 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006824 std::unique_ptr<InputDevice> device2 =
6825 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006826 identifier);
6827 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6828 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6829 0 /*flat*/, 0 /*fuzz*/);
6830 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6831 0 /*flat*/, 0 /*fuzz*/);
6832 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6833 0 /*flat*/, 0 /*fuzz*/);
6834 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6835 0 /*flat*/, 0 /*fuzz*/);
6836 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6837 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6838 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006839
6840 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006841 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006842 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6843 device2->reset(ARBITRARY_TIME);
6844
6845 // Setup PointerController.
6846 sp<FakePointerController> fakePointerController = new FakePointerController();
6847 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6848 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6849
6850 // Setup policy for associated displays and show touches.
6851 const uint8_t hdmi1 = 0;
6852 const uint8_t hdmi2 = 1;
6853 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6854 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6855 mFakePolicy->setShowTouches(true);
6856
6857 // Create displays.
6858 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6859 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6860
6861 // Default device will reconfigure above, need additional reconfiguration for another device.
6862 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6863 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6864
6865 // Two fingers down at default display.
6866 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6867 processPosition(mapper, x1, y1);
6868 processId(mapper, 1);
6869 processSlot(mapper, 1);
6870 processPosition(mapper, x2, y2);
6871 processId(mapper, 2);
6872 processSync(mapper);
6873
6874 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6875 fakePointerController->getSpots().find(DISPLAY_ID);
6876 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6877 ASSERT_EQ(size_t(2), iter->second.size());
6878
6879 // Two fingers down at second display.
6880 processPosition(mapper2, x1, y1);
6881 processId(mapper2, 1);
6882 processSlot(mapper2, 1);
6883 processPosition(mapper2, x2, y2);
6884 processId(mapper2, 2);
6885 processSync(mapper2);
6886
6887 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6888 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6889 ASSERT_EQ(size_t(2), iter->second.size());
6890}
6891
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006892TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006893 prepareAxes(POSITION);
6894 addConfigurationProperty("touch.deviceType", "touchScreen");
6895 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006896 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006897
6898 NotifyMotionArgs motionArgs;
6899 // Unrotated video frame
6900 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6901 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006902 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006903 processPosition(mapper, 100, 200);
6904 processSync(mapper);
6905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6906 ASSERT_EQ(frames, motionArgs.videoFrames);
6907
6908 // Subsequent touch events should not have any videoframes
6909 // This is implemented separately in FakeEventHub,
6910 // but that should match the behaviour of TouchVideoDevice.
6911 processPosition(mapper, 200, 200);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6914 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6915}
6916
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006917TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006918 prepareAxes(POSITION);
6919 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006921 // Unrotated video frame
6922 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6923 NotifyMotionArgs motionArgs;
6924
6925 // Test all 4 orientations
6926 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6927 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6928 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6929 clearViewports();
6930 prepareDisplay(orientation);
6931 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006932 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006933 processPosition(mapper, 100, 200);
6934 processSync(mapper);
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6936 frames[0].rotate(orientation);
6937 ASSERT_EQ(frames, motionArgs.videoFrames);
6938 }
6939}
6940
6941TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006942 prepareAxes(POSITION);
6943 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006944 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006945 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6946 // so mix these.
6947 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6948 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6949 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6950 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6951 NotifyMotionArgs motionArgs;
6952
6953 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006954 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006955 processPosition(mapper, 100, 200);
6956 processSync(mapper);
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6958 std::for_each(frames.begin(), frames.end(),
6959 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6960 ASSERT_EQ(frames, motionArgs.videoFrames);
6961}
6962
Arthur Hung9da14732019-09-02 16:16:58 +08006963/**
6964 * If we had defined port associations, but the viewport is not ready, the touch device would be
6965 * expected to be disabled, and it should be enabled after the viewport has found.
6966 */
6967TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006968 constexpr uint8_t hdmi2 = 1;
6969 const std::string secondaryUniqueId = "uniqueId2";
6970 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6971
6972 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6973
6974 addConfigurationProperty("touch.deviceType", "touchScreen");
6975 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006976 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006977
6978 ASSERT_EQ(mDevice->isEnabled(), false);
6979
6980 // Add display on hdmi2, the device should be enabled and can receive touch event.
6981 prepareSecondaryDisplay(type, hdmi2);
6982 ASSERT_EQ(mDevice->isEnabled(), true);
6983
6984 // Send a touch event.
6985 processPosition(mapper, 100, 100);
6986 processSync(mapper);
6987
6988 NotifyMotionArgs args;
6989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6990 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6991}
6992
Arthur Hung6cd19a42019-08-30 19:04:12 +08006993
Arthur Hung6cd19a42019-08-30 19:04:12 +08006994
Arthur Hung421eb1c2020-01-16 00:09:42 +08006995TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006996 addConfigurationProperty("touch.deviceType", "touchScreen");
6997 prepareDisplay(DISPLAY_ORIENTATION_0);
6998 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006999 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007000
7001 NotifyMotionArgs motionArgs;
7002
7003 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7004 // finger down
7005 processId(mapper, 1);
7006 processPosition(mapper, x1, y1);
7007 processSync(mapper);
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7009 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7011
7012 // finger move
7013 processId(mapper, 1);
7014 processPosition(mapper, x2, y2);
7015 processSync(mapper);
7016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7019
7020 // finger up.
7021 processId(mapper, -1);
7022 processSync(mapper);
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7024 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7025 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7026
7027 // new finger down
7028 processId(mapper, 1);
7029 processPosition(mapper, x3, y3);
7030 processSync(mapper);
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7034}
7035
7036/**
7037 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
7038 * UP events should be ignored.
7039 */
7040TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007041 addConfigurationProperty("touch.deviceType", "touchScreen");
7042 prepareDisplay(DISPLAY_ORIENTATION_0);
7043 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007044 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007045
7046 NotifyMotionArgs motionArgs;
7047
7048 // default tool type is finger
7049 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7050 processId(mapper, 1);
7051 processPosition(mapper, x1, y1);
7052 processSync(mapper);
7053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7054 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7056
7057 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7058 processToolType(mapper, MT_TOOL_PALM);
7059 processSync(mapper);
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7061 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7062
7063 // Ignore the following MOVE and UP events if had detect a palm event.
7064 processId(mapper, 1);
7065 processPosition(mapper, x2, y2);
7066 processSync(mapper);
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7068
7069 // finger up.
7070 processId(mapper, -1);
7071 processSync(mapper);
7072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7073
7074 // new finger down
7075 processToolType(mapper, MT_TOOL_FINGER);
7076 processId(mapper, 1);
7077 processPosition(mapper, x3, y3);
7078 processSync(mapper);
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7080 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7082}
7083
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007084// --- MultiTouchInputMapperTest_ExternalDevice ---
7085
7086class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7087protected:
7088 virtual void SetUp() override {
7089 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7090 }
7091};
7092
7093/**
7094 * Expect fallback to internal viewport if device is external and external viewport is not present.
7095 */
7096TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7097 prepareAxes(POSITION);
7098 addConfigurationProperty("touch.deviceType", "touchScreen");
7099 prepareDisplay(DISPLAY_ORIENTATION_0);
7100 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7101
7102 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7103
7104 NotifyMotionArgs motionArgs;
7105
7106 // Expect the event to be sent to the internal viewport,
7107 // because an external viewport is not present.
7108 processPosition(mapper, 100, 100);
7109 processSync(mapper);
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7111 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7112
7113 // Expect the event to be sent to the external viewport if it is present.
7114 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
7115 processPosition(mapper, 100, 100);
7116 processSync(mapper);
7117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7118 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7119}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007120
7121/**
7122 * Test touch should not work if outside of surface.
7123 */
7124class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7125protected:
7126 void halfDisplayToCenterHorizontal(int32_t orientation) {
7127 std::optional<DisplayViewport> internalViewport =
7128 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
7129
7130 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7131 internalViewport->orientation = orientation;
7132 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7133 internalViewport->logicalLeft = 0;
7134 internalViewport->logicalTop = 0;
7135 internalViewport->logicalRight = DISPLAY_HEIGHT;
7136 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7137
7138 internalViewport->physicalLeft = 0;
7139 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7140 internalViewport->physicalRight = DISPLAY_HEIGHT;
7141 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7142
7143 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7144 internalViewport->deviceHeight = DISPLAY_WIDTH;
7145 } else {
7146 internalViewport->logicalLeft = 0;
7147 internalViewport->logicalTop = 0;
7148 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7149 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7150
7151 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7152 internalViewport->physicalTop = 0;
7153 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7154 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7155
7156 internalViewport->deviceWidth = DISPLAY_WIDTH;
7157 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7158 }
7159
7160 mFakePolicy->updateViewport(internalViewport.value());
7161 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7162 }
7163
7164 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7165 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7166 int32_t yExpected) {
7167 // touch on outside area should not work.
7168 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7169 processSync(mapper);
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7171
7172 // touch on inside area should receive the event.
7173 NotifyMotionArgs args;
7174 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7175 processSync(mapper);
7176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7177 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7178 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7179
7180 // Reset.
7181 mapper.reset(ARBITRARY_TIME);
7182 }
7183};
7184
7185TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7186 addConfigurationProperty("touch.deviceType", "touchScreen");
7187 prepareDisplay(DISPLAY_ORIENTATION_0);
7188 prepareAxes(POSITION);
7189 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7190
7191 // Touch on center of normal display should work.
7192 const int32_t x = DISPLAY_WIDTH / 4;
7193 const int32_t y = DISPLAY_HEIGHT / 2;
7194 processPosition(mapper, toRawX(x), toRawY(y));
7195 processSync(mapper);
7196 NotifyMotionArgs args;
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7199 0.0f, 0.0f, 0.0f, 0.0f));
7200 // Reset.
7201 mapper.reset(ARBITRARY_TIME);
7202
7203 // Let physical display be different to device, and make surface and physical could be 1:1.
7204 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7205
7206 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7207 const int32_t yExpected = y;
7208 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7209}
7210
7211TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7212 addConfigurationProperty("touch.deviceType", "touchScreen");
7213 prepareDisplay(DISPLAY_ORIENTATION_0);
7214 prepareAxes(POSITION);
7215 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7216
7217 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7218 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7219
7220 const int32_t x = DISPLAY_WIDTH / 4;
7221 const int32_t y = DISPLAY_HEIGHT / 2;
7222
7223 // expect x/y = swap x/y then reverse y.
7224 const int32_t xExpected = y;
7225 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7226 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7227}
7228
7229TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7230 addConfigurationProperty("touch.deviceType", "touchScreen");
7231 prepareDisplay(DISPLAY_ORIENTATION_0);
7232 prepareAxes(POSITION);
7233 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7234
7235 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7236 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7237
7238 const int32_t x = DISPLAY_WIDTH / 4;
7239 const int32_t y = DISPLAY_HEIGHT / 2;
7240
7241 // expect x/y = swap x/y then reverse x.
7242 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7243 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7244 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7245}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246} // namespace android