blob: b7f99422c403540abf384da43f777862d4bf033a [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
19#include <InputMapper.h>
20#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080021#include <InputReaderBase.h>
22#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070023#include <KeyboardInputMapper.h>
24#include <MultiTouchInputMapper.h>
25#include <SingleTouchInputMapper.h>
26#include <SwitchInputMapper.h>
27#include <TestInputListener.h>
28#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080029#include <UinputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070031#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080033#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <math.h>
35
36namespace android {
37
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038using std::chrono_literals::operator""ms;
39
40// Timeout for waiting for an expected event
41static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
42
Michael Wrightd02c5b62014-02-10 15:10:22 -080043// An arbitrary time value.
44static const nsecs_t ARBITRARY_TIME = 1234;
45
46// Arbitrary display properties.
47static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070048static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080049static const int32_t DISPLAY_WIDTH = 480;
50static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070051static const int32_t VIRTUAL_DISPLAY_ID = 1;
52static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
53static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070054static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070055static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Error tolerance for floating point assertions.
58static const float EPSILON = 0.001f;
59
60template<typename T>
61static inline T min(T a, T b) {
62 return a < b ? a : b;
63}
64
65static inline float avg(float x, float y) {
66 return (x + y) / 2;
67}
68
69
70// --- FakePointerController ---
71
72class FakePointerController : public PointerControllerInterface {
73 bool mHaveBounds;
74 float mMinX, mMinY, mMaxX, mMaxY;
75 float mX, mY;
76 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080077 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
79protected:
80 virtual ~FakePointerController() { }
81
82public:
83 FakePointerController() :
84 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080085 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080086 }
87
88 void setBounds(float minX, float minY, float maxX, float maxY) {
89 mHaveBounds = true;
90 mMinX = minX;
91 mMinY = minY;
92 mMaxX = maxX;
93 mMaxY = maxY;
94 }
95
96 virtual void setPosition(float x, float y) {
97 mX = x;
98 mY = y;
99 }
100
101 virtual void setButtonState(int32_t buttonState) {
102 mButtonState = buttonState;
103 }
104
105 virtual int32_t getButtonState() const {
106 return mButtonState;
107 }
108
109 virtual void getPosition(float* outX, float* outY) const {
110 *outX = mX;
111 *outY = mY;
112 }
113
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800114 virtual int32_t getDisplayId() const {
115 return mDisplayId;
116 }
117
Garfield Tan888a6a42020-01-09 11:39:16 -0800118 virtual void setDisplayViewport(const DisplayViewport& viewport) {
119 mDisplayId = viewport.displayId;
120 }
121
Arthur Hung7c645402019-01-25 17:45:42 +0800122 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
123 return mSpotsByDisplay;
124 }
125
Michael Wrightd02c5b62014-02-10 15:10:22 -0800126private:
127 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
128 *outMinX = mMinX;
129 *outMinY = mMinY;
130 *outMaxX = mMaxX;
131 *outMaxY = mMaxY;
132 return mHaveBounds;
133 }
134
135 virtual void move(float deltaX, float deltaY) {
136 mX += deltaX;
137 if (mX < mMinX) mX = mMinX;
138 if (mX > mMaxX) mX = mMaxX;
139 mY += deltaY;
140 if (mY < mMinY) mY = mMinY;
141 if (mY > mMaxY) mY = mMaxY;
142 }
143
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100144 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 }
146
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100147 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148 }
149
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100150 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151 }
152
Arthur Hung7c645402019-01-25 17:45:42 +0800153 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
154 int32_t displayId) {
155 std::vector<int32_t> newSpots;
156 // Add spots for fingers that are down.
157 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
158 uint32_t id = idBits.clearFirstMarkedBit();
159 newSpots.push_back(id);
160 }
161
162 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 }
164
165 virtual void clearSpots() {
166 }
Arthur Hung7c645402019-01-25 17:45:42 +0800167
168 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169};
170
171
172// --- FakeInputReaderPolicy ---
173
174class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700175 std::mutex mLock;
176 std::condition_variable mDevicesChangedCondition;
177
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 InputReaderConfiguration mConfig;
179 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700180 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
181 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100182 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700183 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
185protected:
186 virtual ~FakeInputReaderPolicy() { }
187
188public:
189 FakeInputReaderPolicy() {
190 }
191
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700192 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800193 waitForInputDevices([](bool devicesChanged) {
194 if (!devicesChanged) {
195 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
196 }
197 });
198 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700199
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800200 void assertInputDevicesNotChanged() {
201 waitForInputDevices([](bool devicesChanged) {
202 if (devicesChanged) {
203 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
204 }
205 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700206 }
207
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700208 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100209 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100210 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700211 }
212
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700213 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
214 return mConfig.getDisplayViewportByUniqueId(uniqueId);
215 }
216 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
217 return mConfig.getDisplayViewportByType(type);
218 }
219
220 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
221 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700222 }
223
224 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700225 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
226 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700227 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700228 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700229 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100230 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 }
232
Arthur Hung6cd19a42019-08-30 19:04:12 +0800233 bool updateViewport(const DisplayViewport& viewport) {
234 size_t count = mViewports.size();
235 for (size_t i = 0; i < count; i++) {
236 const DisplayViewport& currentViewport = mViewports[i];
237 if (currentViewport.displayId == viewport.displayId) {
238 mViewports[i] = viewport;
239 mConfig.setDisplayViewports(mViewports);
240 return true;
241 }
242 }
243 // no viewport found.
244 return false;
245 }
246
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100247 void addExcludedDeviceName(const std::string& deviceName) {
248 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 }
250
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700251 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
252 mConfig.portAssociations.insert({inputPort, displayPort});
253 }
254
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000255 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700256
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000257 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
260 mPointerControllers.add(deviceId, controller);
261 }
262
263 const InputReaderConfiguration* getReaderConfiguration() const {
264 return &mConfig;
265 }
266
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800267 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 return mInputDevices;
269 }
270
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100271 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700272 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700273 return transform;
274 }
275
276 void setTouchAffineTransformation(const TouchAffineTransformation t) {
277 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800278 }
279
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800280 void setPointerCapture(bool enabled) {
281 mConfig.pointerCapture = enabled;
282 }
283
Arthur Hung7c645402019-01-25 17:45:42 +0800284 void setShowTouches(bool enabled) {
285 mConfig.showTouches = enabled;
286 }
287
Garfield Tan888a6a42020-01-09 11:39:16 -0800288 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
289 mConfig.defaultPointerDisplayId = pointerDisplayId;
290 }
291
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700293 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700294 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
295 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700296 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
297 || orientation == DISPLAY_ORIENTATION_270);
298 DisplayViewport v;
299 v.displayId = displayId;
300 v.orientation = orientation;
301 v.logicalLeft = 0;
302 v.logicalTop = 0;
303 v.logicalRight = isRotated ? height : width;
304 v.logicalBottom = isRotated ? width : height;
305 v.physicalLeft = 0;
306 v.physicalTop = 0;
307 v.physicalRight = isRotated ? height : width;
308 v.physicalBottom = isRotated ? width : height;
309 v.deviceWidth = isRotated ? height : width;
310 v.deviceHeight = isRotated ? width : height;
311 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700312 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100313 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700314 return v;
315 }
316
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
318 *outConfig = mConfig;
319 }
320
321 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
322 return mPointerControllers.valueFor(deviceId);
323 }
324
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800325 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700326 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700328 mInputDevicesChanged = true;
329 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 }
331
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100332 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700333 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100336 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
337 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800339
340 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
341 std::unique_lock<std::mutex> lock(mLock);
342 base::ScopedLockAssertion assumeLocked(mLock);
343
344 const bool devicesChanged =
345 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
346 return mInputDevicesChanged;
347 });
348 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
349 mInputDevicesChanged = false;
350 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351};
352
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353// --- FakeEventHub ---
354
355class FakeEventHub : public EventHubInterface {
356 struct KeyInfo {
357 int32_t keyCode;
358 uint32_t flags;
359 };
360
361 struct Device {
362 InputDeviceIdentifier identifier;
363 uint32_t classes;
364 PropertyMap configuration;
365 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
366 KeyedVector<int, bool> relativeAxes;
367 KeyedVector<int32_t, int32_t> keyCodeStates;
368 KeyedVector<int32_t, int32_t> scanCodeStates;
369 KeyedVector<int32_t, int32_t> switchStates;
370 KeyedVector<int32_t, int32_t> absoluteAxisValue;
371 KeyedVector<int32_t, KeyInfo> keysByScanCode;
372 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
373 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800374 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700375 bool enabled;
376
377 status_t enable() {
378 enabled = true;
379 return OK;
380 }
381
382 status_t disable() {
383 enabled = false;
384 return OK;
385 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700387 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700388 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 }
390 };
391
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700392 std::mutex mLock;
393 std::condition_variable mEventsCondition;
394
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100396 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700397 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600398 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700400public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 virtual ~FakeEventHub() {
402 for (size_t i = 0; i < mDevices.size(); i++) {
403 delete mDevices.valueAt(i);
404 }
405 }
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 FakeEventHub() { }
408
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100409 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 Device* device = new Device(classes);
411 device->identifier.name = name;
412 mDevices.add(deviceId, device);
413
414 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
415 }
416
417 void removeDevice(int32_t deviceId) {
418 delete mDevices.valueFor(deviceId);
419 mDevices.removeItem(deviceId);
420
421 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
422 }
423
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700424 bool isDeviceEnabled(int32_t deviceId) {
425 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700426 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700427 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
428 return false;
429 }
430 return device->enabled;
431 }
432
433 status_t enableDevice(int32_t deviceId) {
434 status_t result;
435 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700436 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
438 return BAD_VALUE;
439 }
440 if (device->enabled) {
441 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
442 return OK;
443 }
444 result = device->enable();
445 return result;
446 }
447
448 status_t disableDevice(int32_t deviceId) {
449 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700450 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700451 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
452 return BAD_VALUE;
453 }
454 if (!device->enabled) {
455 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
456 return OK;
457 }
458 return device->disable();
459 }
460
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 void finishDeviceScan() {
462 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
463 }
464
465 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
466 Device* device = getDevice(deviceId);
467 device->configuration.addProperty(key, value);
468 }
469
470 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
471 Device* device = getDevice(deviceId);
472 device->configuration.addAll(configuration);
473 }
474
475 void addAbsoluteAxis(int32_t deviceId, int axis,
476 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
477 Device* device = getDevice(deviceId);
478
479 RawAbsoluteAxisInfo info;
480 info.valid = true;
481 info.minValue = minValue;
482 info.maxValue = maxValue;
483 info.flat = flat;
484 info.fuzz = fuzz;
485 info.resolution = resolution;
486 device->absoluteAxes.add(axis, info);
487 }
488
489 void addRelativeAxis(int32_t deviceId, int32_t axis) {
490 Device* device = getDevice(deviceId);
491 device->relativeAxes.add(axis, true);
492 }
493
494 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
495 Device* device = getDevice(deviceId);
496 device->keyCodeStates.replaceValueFor(keyCode, state);
497 }
498
499 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
500 Device* device = getDevice(deviceId);
501 device->scanCodeStates.replaceValueFor(scanCode, state);
502 }
503
504 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
505 Device* device = getDevice(deviceId);
506 device->switchStates.replaceValueFor(switchCode, state);
507 }
508
509 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
510 Device* device = getDevice(deviceId);
511 device->absoluteAxisValue.replaceValueFor(axis, value);
512 }
513
514 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
515 int32_t keyCode, uint32_t flags) {
516 Device* device = getDevice(deviceId);
517 KeyInfo info;
518 info.keyCode = keyCode;
519 info.flags = flags;
520 if (scanCode) {
521 device->keysByScanCode.add(scanCode, info);
522 }
523 if (usageCode) {
524 device->keysByUsageCode.add(usageCode, info);
525 }
526 }
527
528 void addLed(int32_t deviceId, int32_t led, bool initialState) {
529 Device* device = getDevice(deviceId);
530 device->leds.add(led, initialState);
531 }
532
533 bool getLedState(int32_t deviceId, int32_t led) {
534 Device* device = getDevice(deviceId);
535 return device->leds.valueFor(led);
536 }
537
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100538 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 return mExcludedDevices;
540 }
541
542 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
543 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800544 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 }
546
547 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
548 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700549 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 RawEvent event;
551 event.when = when;
552 event.deviceId = deviceId;
553 event.type = type;
554 event.code = code;
555 event.value = value;
556 mEvents.push_back(event);
557
558 if (type == EV_ABS) {
559 setAbsoluteAxisValue(deviceId, code, value);
560 }
561 }
562
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600563 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
564 std::vector<TouchVideoFrame>> videoFrames) {
565 mVideoFrames = std::move(videoFrames);
566 }
567
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700569 std::unique_lock<std::mutex> lock(mLock);
570 base::ScopedLockAssertion assumeLocked(mLock);
571 const bool queueIsEmpty =
572 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
573 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
574 if (!queueIsEmpty) {
575 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
576 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
579private:
580 Device* getDevice(int32_t deviceId) const {
581 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100582 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 }
584
585 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
586 Device* device = getDevice(deviceId);
587 return device ? device->classes : 0;
588 }
589
590 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
591 Device* device = getDevice(deviceId);
592 return device ? device->identifier : InputDeviceIdentifier();
593 }
594
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100595 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 return 0;
597 }
598
599 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
600 Device* device = getDevice(deviceId);
601 if (device) {
602 *outConfiguration = device->configuration;
603 }
604 }
605
606 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
607 RawAbsoluteAxisInfo* outAxisInfo) const {
608 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800609 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 ssize_t index = device->absoluteAxes.indexOfKey(axis);
611 if (index >= 0) {
612 *outAxisInfo = device->absoluteAxes.valueAt(index);
613 return OK;
614 }
615 }
616 outAxisInfo->clear();
617 return -1;
618 }
619
620 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
621 Device* device = getDevice(deviceId);
622 if (device) {
623 return device->relativeAxes.indexOfKey(axis) >= 0;
624 }
625 return false;
626 }
627
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100628 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 return false;
630 }
631
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700632 virtual status_t mapKey(int32_t deviceId,
633 int32_t scanCode, int32_t usageCode, int32_t metaState,
634 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 Device* device = getDevice(deviceId);
636 if (device) {
637 const KeyInfo* key = getKey(device, scanCode, usageCode);
638 if (key) {
639 if (outKeycode) {
640 *outKeycode = key->keyCode;
641 }
642 if (outFlags) {
643 *outFlags = key->flags;
644 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700645 if (outMetaState) {
646 *outMetaState = metaState;
647 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 return OK;
649 }
650 }
651 return NAME_NOT_FOUND;
652 }
653
654 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
655 if (usageCode) {
656 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
657 if (index >= 0) {
658 return &device->keysByUsageCode.valueAt(index);
659 }
660 }
661 if (scanCode) {
662 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
663 if (index >= 0) {
664 return &device->keysByScanCode.valueAt(index);
665 }
666 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700667 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 }
669
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100670 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 return NAME_NOT_FOUND;
672 }
673
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100674 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 mExcludedDevices = devices;
676 }
677
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100678 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700679 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 if (mEvents.empty()) {
681 return 0;
682 }
683
684 *buffer = *mEvents.begin();
685 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700686 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 return 1;
688 }
689
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800690 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600691 auto it = mVideoFrames.find(deviceId);
692 if (it != mVideoFrames.end()) {
693 std::vector<TouchVideoFrame> frames = std::move(it->second);
694 mVideoFrames.erase(deviceId);
695 return frames;
696 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800697 return {};
698 }
699
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
701 Device* device = getDevice(deviceId);
702 if (device) {
703 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
704 if (index >= 0) {
705 return device->scanCodeStates.valueAt(index);
706 }
707 }
708 return AKEY_STATE_UNKNOWN;
709 }
710
711 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
712 Device* device = getDevice(deviceId);
713 if (device) {
714 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
715 if (index >= 0) {
716 return device->keyCodeStates.valueAt(index);
717 }
718 }
719 return AKEY_STATE_UNKNOWN;
720 }
721
722 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
723 Device* device = getDevice(deviceId);
724 if (device) {
725 ssize_t index = device->switchStates.indexOfKey(sw);
726 if (index >= 0) {
727 return device->switchStates.valueAt(index);
728 }
729 }
730 return AKEY_STATE_UNKNOWN;
731 }
732
733 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
734 int32_t* outValue) const {
735 Device* device = getDevice(deviceId);
736 if (device) {
737 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
738 if (index >= 0) {
739 *outValue = device->absoluteAxisValue.valueAt(index);
740 return OK;
741 }
742 }
743 *outValue = 0;
744 return -1;
745 }
746
747 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
748 uint8_t* outFlags) const {
749 bool result = false;
750 Device* device = getDevice(deviceId);
751 if (device) {
752 for (size_t i = 0; i < numCodes; i++) {
753 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
754 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
755 outFlags[i] = 1;
756 result = true;
757 }
758 }
759 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
760 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
761 outFlags[i] = 1;
762 result = true;
763 }
764 }
765 }
766 }
767 return result;
768 }
769
770 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
771 Device* device = getDevice(deviceId);
772 if (device) {
773 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
774 return index >= 0;
775 }
776 return false;
777 }
778
779 virtual bool hasLed(int32_t deviceId, int32_t led) const {
780 Device* device = getDevice(deviceId);
781 return device && device->leds.indexOfKey(led) >= 0;
782 }
783
784 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
785 Device* device = getDevice(deviceId);
786 if (device) {
787 ssize_t index = device->leds.indexOfKey(led);
788 if (index >= 0) {
789 device->leds.replaceValueAt(led, on);
790 } else {
791 ADD_FAILURE()
792 << "Attempted to set the state of an LED that the EventHub declared "
793 "was not present. led=" << led;
794 }
795 }
796 }
797
798 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800799 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 outVirtualKeys.clear();
801
802 Device* device = getDevice(deviceId);
803 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800804 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 }
806 }
807
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100808 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700809 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100812 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 return false;
814 }
815
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100816 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 }
818
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100819 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 }
821
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100822 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 return false;
824 }
825
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800826 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 }
828
829 virtual void monitor() {
830 }
831
832 virtual void requestReopenDevices() {
833 }
834
835 virtual void wake() {
836 }
837};
838
839
840// --- FakeInputReaderContext ---
841
842class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700843 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 sp<InputReaderPolicyInterface> mPolicy;
845 sp<InputListenerInterface> mListener;
846 int32_t mGlobalMetaState;
847 bool mUpdateGlobalMetaStateWasCalled;
848 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800849 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850
851public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700852 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
853 const sp<InputReaderPolicyInterface>& policy,
854 const sp<InputListenerInterface>& listener)
855 : mEventHub(eventHub),
856 mPolicy(policy),
857 mListener(listener),
858 mGlobalMetaState(0),
859 mNextSequenceNum(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
861 virtual ~FakeInputReaderContext() { }
862
863 void assertUpdateGlobalMetaStateWasCalled() {
864 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
865 << "Expected updateGlobalMetaState() to have been called.";
866 mUpdateGlobalMetaStateWasCalled = false;
867 }
868
869 void setGlobalMetaState(int32_t state) {
870 mGlobalMetaState = state;
871 }
872
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800873 uint32_t getGeneration() {
874 return mGeneration;
875 }
876
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877private:
878 virtual void updateGlobalMetaState() {
879 mUpdateGlobalMetaStateWasCalled = true;
880 }
881
882 virtual int32_t getGlobalMetaState() {
883 return mGlobalMetaState;
884 }
885
886 virtual EventHubInterface* getEventHub() {
887 return mEventHub.get();
888 }
889
890 virtual InputReaderPolicyInterface* getPolicy() {
891 return mPolicy.get();
892 }
893
894 virtual InputListenerInterface* getListener() {
895 return mListener.get();
896 }
897
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100898 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 }
900
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800901 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902
903 virtual void fadePointer() {
904 }
905
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100906 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907 }
908
909 virtual int32_t bumpGeneration() {
910 return ++mGeneration;
911 }
Michael Wright842500e2015-03-13 17:32:02 -0700912
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800913 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700914
915 }
916
917 virtual void dispatchExternalStylusState(const StylusState&) {
918
919 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800920
921 virtual uint32_t getNextSequenceNum() {
922 return mNextSequenceNum++;
923 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924};
925
926
927// --- FakeInputMapper ---
928
929class FakeInputMapper : public InputMapper {
930 uint32_t mSources;
931 int32_t mKeyboardType;
932 int32_t mMetaState;
933 KeyedVector<int32_t, int32_t> mKeyCodeStates;
934 KeyedVector<int32_t, int32_t> mScanCodeStates;
935 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800936 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700938 std::mutex mLock;
939 std::condition_variable mStateChangedCondition;
940 bool mConfigureWasCalled GUARDED_BY(mLock);
941 bool mResetWasCalled GUARDED_BY(mLock);
942 bool mProcessWasCalled GUARDED_BY(mLock);
943 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944
Arthur Hungc23540e2018-11-29 20:42:11 +0800945 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800947 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
948 : InputMapper(deviceContext),
949 mSources(sources),
950 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800952 mConfigureWasCalled(false),
953 mResetWasCalled(false),
954 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955
956 virtual ~FakeInputMapper() { }
957
958 void setKeyboardType(int32_t keyboardType) {
959 mKeyboardType = keyboardType;
960 }
961
962 void setMetaState(int32_t metaState) {
963 mMetaState = metaState;
964 }
965
966 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700967 std::unique_lock<std::mutex> lock(mLock);
968 base::ScopedLockAssertion assumeLocked(mLock);
969 const bool configureCalled =
970 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
971 return mConfigureWasCalled;
972 });
973 if (!configureCalled) {
974 FAIL() << "Expected configure() to have been called.";
975 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976 mConfigureWasCalled = false;
977 }
978
979 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700980 std::unique_lock<std::mutex> lock(mLock);
981 base::ScopedLockAssertion assumeLocked(mLock);
982 const bool resetCalled =
983 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
984 return mResetWasCalled;
985 });
986 if (!resetCalled) {
987 FAIL() << "Expected reset() to have been called.";
988 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 mResetWasCalled = false;
990 }
991
Yi Kong9b14ac62018-07-17 13:48:38 -0700992 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700993 std::unique_lock<std::mutex> lock(mLock);
994 base::ScopedLockAssertion assumeLocked(mLock);
995 const bool processCalled =
996 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
997 return mProcessWasCalled;
998 });
999 if (!processCalled) {
1000 FAIL() << "Expected process() to have been called.";
1001 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002 if (outLastEvent) {
1003 *outLastEvent = mLastEvent;
1004 }
1005 mProcessWasCalled = false;
1006 }
1007
1008 void setKeyCodeState(int32_t keyCode, int32_t state) {
1009 mKeyCodeStates.replaceValueFor(keyCode, state);
1010 }
1011
1012 void setScanCodeState(int32_t scanCode, int32_t state) {
1013 mScanCodeStates.replaceValueFor(scanCode, state);
1014 }
1015
1016 void setSwitchState(int32_t switchCode, int32_t state) {
1017 mSwitchStates.replaceValueFor(switchCode, state);
1018 }
1019
1020 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001021 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022 }
1023
1024private:
1025 virtual uint32_t getSources() {
1026 return mSources;
1027 }
1028
1029 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1030 InputMapper::populateDeviceInfo(deviceInfo);
1031
1032 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1033 deviceInfo->setKeyboardType(mKeyboardType);
1034 }
1035 }
1036
Arthur Hungc23540e2018-11-29 20:42:11 +08001037 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001038 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001040
1041 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001042 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001043 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1044 mViewport = config->getDisplayViewportByPort(*displayPort);
1045 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001046
1047 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048 }
1049
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001050 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001051 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001053 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001054 }
1055
1056 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001057 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058 mLastEvent = *rawEvent;
1059 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001060 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061 }
1062
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001063 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001064 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1065 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1066 }
1067
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001068 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1070 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1071 }
1072
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001073 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1075 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1076 }
1077
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001078 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079 const int32_t* keyCodes, uint8_t* outFlags) {
1080 bool result = false;
1081 for (size_t i = 0; i < numCodes; i++) {
1082 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1083 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1084 outFlags[i] = 1;
1085 result = true;
1086 }
1087 }
1088 }
1089 return result;
1090 }
1091
1092 virtual int32_t getMetaState() {
1093 return mMetaState;
1094 }
1095
1096 virtual void fadePointer() {
1097 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001098
1099 virtual std::optional<int32_t> getAssociatedDisplay() {
1100 if (mViewport) {
1101 return std::make_optional(mViewport->displayId);
1102 }
1103 return std::nullopt;
1104 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105};
1106
1107
1108// --- InstrumentedInputReader ---
1109
1110class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001111 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112
1113public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001114 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1115 const sp<InputReaderPolicyInterface>& policy,
1116 const sp<InputListenerInterface>& listener)
1117 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001119 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001121 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001122
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001123 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, int32_t controllerNumber,
1124 const std::string& name, uint32_t classes,
1125 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126 InputDeviceIdentifier identifier;
1127 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001128 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 int32_t generation = deviceId + 1;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001130 return std::make_shared<InputDevice>(&mContext, deviceId, generation, controllerNumber,
1131 identifier, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 }
1133
Prabir Pradhan28efc192019-11-05 01:10:04 +00001134 // Make the protected loopOnce method accessible to tests.
1135 using InputReader::loopOnce;
1136
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137protected:
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001138 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t deviceId,
1139 int32_t controllerNumber,
1140 const InputDeviceIdentifier& identifier,
1141 uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001143 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001144 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001145 return device;
1146 }
1147 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1148 }
1149
1150 friend class InputReaderTest;
1151};
1152
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001153// --- InputReaderPolicyTest ---
1154class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001155protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001156 sp<FakeInputReaderPolicy> mFakePolicy;
1157
Prabir Pradhan28efc192019-11-05 01:10:04 +00001158 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1159 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001160};
1161
1162/**
1163 * Check that empty set of viewports is an acceptable configuration.
1164 * Also try to get internal viewport two different ways - by type and by uniqueId.
1165 *
1166 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1167 * Such configuration is not currently allowed.
1168 */
1169TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001170 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001171
1172 // We didn't add any viewports yet, so there shouldn't be any.
1173 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001174 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001175 ASSERT_FALSE(internalViewport);
1176
1177 // Add an internal viewport, then clear it
1178 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001179 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001180
1181 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001182 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001183 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001184 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185
1186 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001187 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001188 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001189 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001190
1191 mFakePolicy->clearViewports();
1192 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001193 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001194 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001195 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001196 ASSERT_FALSE(internalViewport);
1197}
1198
1199TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1200 const std::string internalUniqueId = "local:0";
1201 const std::string externalUniqueId = "local:1";
1202 const std::string virtualUniqueId1 = "virtual:2";
1203 const std::string virtualUniqueId2 = "virtual:3";
1204 constexpr int32_t virtualDisplayId1 = 2;
1205 constexpr int32_t virtualDisplayId2 = 3;
1206
1207 // Add an internal viewport
1208 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001209 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210 // Add an external viewport
1211 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001212 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213 // Add an virtual viewport
1214 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001215 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001216 // Add another virtual viewport
1217 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001218 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001219
1220 // Check matching by type for internal
1221 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001222 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001223 ASSERT_TRUE(internalViewport);
1224 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1225
1226 // Check matching by type for external
1227 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001228 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001229 ASSERT_TRUE(externalViewport);
1230 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1231
1232 // Check matching by uniqueId for virtual viewport #1
1233 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001234 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001235 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001236 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1238 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1239
1240 // Check matching by uniqueId for virtual viewport #2
1241 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001242 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001243 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001244 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001245 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1246 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1247}
1248
1249
1250/**
1251 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1252 * that lookup works by checking display id.
1253 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1254 */
1255TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1256 const std::string uniqueId1 = "uniqueId1";
1257 const std::string uniqueId2 = "uniqueId2";
1258 constexpr int32_t displayId1 = 2;
1259 constexpr int32_t displayId2 = 3;
1260
1261 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1262 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1263 for (const ViewportType& type : types) {
1264 mFakePolicy->clearViewports();
1265 // Add a viewport
1266 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001267 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001268 // Add another viewport
1269 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001270 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001271
1272 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001273 std::optional<DisplayViewport> viewport1 =
1274 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001275 ASSERT_TRUE(viewport1);
1276 ASSERT_EQ(displayId1, viewport1->displayId);
1277 ASSERT_EQ(type, viewport1->type);
1278
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001279 std::optional<DisplayViewport> viewport2 =
1280 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001281 ASSERT_TRUE(viewport2);
1282 ASSERT_EQ(displayId2, viewport2->displayId);
1283 ASSERT_EQ(type, viewport2->type);
1284
1285 // When there are multiple viewports of the same kind, and uniqueId is not specified
1286 // in the call to getDisplayViewport, then that situation is not supported.
1287 // The viewports can be stored in any order, so we cannot rely on the order, since that
1288 // is just implementation detail.
1289 // However, we can check that it still returns *a* viewport, we just cannot assert
1290 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001291 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001292 ASSERT_TRUE(someViewport);
1293 }
1294}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001296/**
1297 * Check getDisplayViewportByPort
1298 */
1299TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1300 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1301 const std::string uniqueId1 = "uniqueId1";
1302 const std::string uniqueId2 = "uniqueId2";
1303 constexpr int32_t displayId1 = 1;
1304 constexpr int32_t displayId2 = 2;
1305 const uint8_t hdmi1 = 0;
1306 const uint8_t hdmi2 = 1;
1307 const uint8_t hdmi3 = 2;
1308
1309 mFakePolicy->clearViewports();
1310 // Add a viewport that's associated with some display port that's not of interest.
1311 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1312 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1313 // Add another viewport, connected to HDMI1 port
1314 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1315 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1316
1317 // Check that correct display viewport was returned by comparing the display ports.
1318 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1319 ASSERT_TRUE(hdmi1Viewport);
1320 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1321 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1322
1323 // Check that we can still get the same viewport using the uniqueId
1324 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1325 ASSERT_TRUE(hdmi1Viewport);
1326 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1327 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1328 ASSERT_EQ(type, hdmi1Viewport->type);
1329
1330 // Check that we cannot find a port with "HDMI2", because we never added one
1331 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1332 ASSERT_FALSE(hdmi2Viewport);
1333}
1334
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335// --- InputReaderTest ---
1336
1337class InputReaderTest : public testing::Test {
1338protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001339 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001341 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001342 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001343
Prabir Pradhan28efc192019-11-05 01:10:04 +00001344 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001345 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001346 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001347 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001348
Prabir Pradhan28efc192019-11-05 01:10:04 +00001349 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1350 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001351 }
1352
Prabir Pradhan28efc192019-11-05 01:10:04 +00001353 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001354 mFakeListener.clear();
1355 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001356 }
1357
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001358 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001359 const PropertyMap* configuration) {
1360 mFakeEventHub->addDevice(deviceId, name, classes);
1361
1362 if (configuration) {
1363 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1364 }
1365 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001366 mReader->loopOnce();
1367 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001368 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1369 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001370 }
1371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001372 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001373 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001374 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001375 }
1376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001377 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001378 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001379 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001380 }
1381
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001382 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
1383 const std::string& name, uint32_t classes,
1384 uint32_t sources,
1385 const PropertyMap* configuration) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001386 std::shared_ptr<InputDevice> device =
1387 mReader->newDevice(deviceId, controllerNumber, name, classes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001388 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001389 mReader->setNextDevice(device);
1390 addDevice(deviceId, name, classes, configuration);
1391 return mapper;
1392 }
1393};
1394
1395TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001396 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001397 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001398 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001399 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001400
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001401 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001402 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001403 ASSERT_EQ(1U, inputDevices.size());
1404 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001405 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001406 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1407 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1408 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1409
1410 // Should also have received a notification describing the new input devices.
1411 inputDevices = mFakePolicy->getInputDevices();
1412 ASSERT_EQ(1U, inputDevices.size());
1413 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001414 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001415 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1416 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1417 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1418}
1419
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001420TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1421 constexpr int32_t deviceId = 1;
1422 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001423 std::shared_ptr<InputDevice> device =
1424 mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001425 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001426 device->addMapper<FakeInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001427 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001428 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001429
Yi Kong9b14ac62018-07-17 13:48:38 -07001430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001431
1432 NotifyDeviceResetArgs resetArgs;
1433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001434 ASSERT_EQ(deviceId, resetArgs.deviceId);
1435
1436 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001437 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001438 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001439
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001441 ASSERT_EQ(deviceId, resetArgs.deviceId);
1442 ASSERT_EQ(device->isEnabled(), false);
1443
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001444 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001445 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001448 ASSERT_EQ(device->isEnabled(), false);
1449
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001450 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001451 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001453 ASSERT_EQ(deviceId, resetArgs.deviceId);
1454 ASSERT_EQ(device->isEnabled(), true);
1455}
1456
Michael Wrightd02c5b62014-02-10 15:10:22 -08001457TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001458 FakeInputMapper& mapper =
1459 addDeviceWithFakeInputMapper(1, 0, "fake", INPUT_DEVICE_CLASS_KEYBOARD,
1460 AINPUT_SOURCE_KEYBOARD, nullptr);
1461 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001462
1463 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1464 AINPUT_SOURCE_ANY, AKEYCODE_A))
1465 << "Should return unknown when the device id is >= 0 but unknown.";
1466
1467 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1468 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1469 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1470
1471 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1472 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1473 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1474
1475 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1476 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1477 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1478
1479 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1480 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1481 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1482}
1483
1484TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001485 FakeInputMapper& mapper =
1486 addDeviceWithFakeInputMapper(1, 0, "fake", INPUT_DEVICE_CLASS_KEYBOARD,
1487 AINPUT_SOURCE_KEYBOARD, nullptr);
1488 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001489
1490 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1491 AINPUT_SOURCE_ANY, KEY_A))
1492 << "Should return unknown when the device id is >= 0 but unknown.";
1493
1494 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1495 AINPUT_SOURCE_TRACKBALL, KEY_A))
1496 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1497
1498 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1499 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1500 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1501
1502 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1503 AINPUT_SOURCE_TRACKBALL, KEY_A))
1504 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1505
1506 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1507 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1508 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1509}
1510
1511TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001512 FakeInputMapper& mapper =
1513 addDeviceWithFakeInputMapper(1, 0, "fake", INPUT_DEVICE_CLASS_KEYBOARD,
1514 AINPUT_SOURCE_KEYBOARD, nullptr);
1515 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516
1517 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1518 AINPUT_SOURCE_ANY, SW_LID))
1519 << "Should return unknown when the device id is >= 0 but unknown.";
1520
1521 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1522 AINPUT_SOURCE_TRACKBALL, SW_LID))
1523 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1524
1525 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1526 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1527 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1528
1529 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1530 AINPUT_SOURCE_TRACKBALL, SW_LID))
1531 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1532
1533 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1534 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1535 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1536}
1537
1538TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001539 FakeInputMapper& mapper =
1540 addDeviceWithFakeInputMapper(1, 0, "fake", INPUT_DEVICE_CLASS_KEYBOARD,
1541 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001543 mapper.addSupportedKeyCode(AKEYCODE_A);
1544 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001545
1546 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1547 uint8_t flags[4] = { 0, 0, 0, 1 };
1548
1549 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1550 << "Should return false when device id is >= 0 but unknown.";
1551 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1552
1553 flags[3] = 1;
1554 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1555 << "Should return false when device id is valid but the sources are not supported by the device.";
1556 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1557
1558 flags[3] = 1;
1559 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1560 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1561 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1562
1563 flags[3] = 1;
1564 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1565 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1566 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1567
1568 flags[3] = 1;
1569 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1570 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1571 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1572}
1573
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001574TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001575 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001576
1577 NotifyConfigurationChangedArgs args;
1578
1579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1580 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1581}
1582
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001583TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001584 FakeInputMapper& mapper =
1585 addDeviceWithFakeInputMapper(1, 0, "fake", INPUT_DEVICE_CLASS_KEYBOARD,
1586 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587
1588 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001589 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1591
1592 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001593 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 ASSERT_EQ(0, event.when);
1595 ASSERT_EQ(1, event.deviceId);
1596 ASSERT_EQ(EV_KEY, event.type);
1597 ASSERT_EQ(KEY_A, event.code);
1598 ASSERT_EQ(1, event.value);
1599}
1600
Prabir Pradhan42611e02018-11-27 14:04:02 -08001601TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1602 constexpr int32_t deviceId = 1;
1603 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001604 std::shared_ptr<InputDevice> device =
1605 mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001606 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001607 device->addMapper<FakeInputMapper>(AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001608 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001609 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001610
1611 NotifyDeviceResetArgs resetArgs;
1612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1613 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001615 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001616 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001618 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1619 prevSequenceNum = resetArgs.sequenceNum;
1620
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001621 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001622 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001624 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1625 prevSequenceNum = resetArgs.sequenceNum;
1626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001630 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1631 prevSequenceNum = resetArgs.sequenceNum;
1632}
1633
Arthur Hungc23540e2018-11-29 20:42:11 +08001634TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1635 constexpr int32_t deviceId = 1;
1636 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1637 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001638 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, 0 /*controllerNumber*/,
1639 "fake", deviceClass, DEVICE_LOCATION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001640 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001641 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001642
1643 const uint8_t hdmi1 = 1;
1644
1645 // Associated touch screen with second display.
1646 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1647
1648 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001649 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001650 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1651 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1652 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1653 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1654 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001655 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001656
1657 // Add the device, and make sure all of the callbacks are triggered.
1658 // The device is added after the input port associations are processed since
1659 // we do not yet support dynamic device-to-display associations.
1660 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001663 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001664
Arthur Hung2c9a3342019-07-23 14:18:59 +08001665 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001666 ASSERT_EQ(deviceId, device->getId());
1667 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1668 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001669
1670 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001671 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001672 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001673 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001674}
1675
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001676// --- InputReaderIntegrationTest ---
1677
1678// These tests create and interact with the InputReader only through its interface.
1679// The InputReader is started during SetUp(), which starts its processing in its own
1680// thread. The tests use linux uinput to emulate input devices.
1681// NOTE: Interacting with the physical device while these tests are running may cause
1682// the tests to fail.
1683class InputReaderIntegrationTest : public testing::Test {
1684protected:
1685 sp<TestInputListener> mTestListener;
1686 sp<FakeInputReaderPolicy> mFakePolicy;
1687 sp<InputReaderInterface> mReader;
1688
1689 virtual void SetUp() override {
1690 mFakePolicy = new FakeInputReaderPolicy();
1691 mTestListener = new TestInputListener();
1692
1693 mReader = createInputReader(mFakePolicy, mTestListener);
1694 ASSERT_EQ(mReader->start(), OK);
1695
1696 // Since this test is run on a real device, all the input devices connected
1697 // to the test device will show up in mReader. We wait for those input devices to
1698 // show up before beginning the tests.
1699 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1700 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1701 }
1702
1703 virtual void TearDown() override {
1704 ASSERT_EQ(mReader->stop(), OK);
1705 mTestListener.clear();
1706 mFakePolicy.clear();
1707 }
1708};
1709
1710TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1711 // An invalid input device that is only used for this test.
1712 class InvalidUinputDevice : public UinputDevice {
1713 public:
1714 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1715
1716 private:
1717 void configureDevice(int fd, uinput_user_dev* device) override {}
1718 };
1719
1720 const size_t numDevices = mFakePolicy->getInputDevices().size();
1721
1722 // UinputDevice does not set any event or key bits, so InputReader should not
1723 // consider it as a valid device.
1724 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1725 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1726 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1727 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1728
1729 invalidDevice.reset();
1730 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1731 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1732 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1733}
1734
1735TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1736 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1737
1738 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1739 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1740 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1741 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1742
1743 // Find the test device by its name.
1744 std::vector<InputDeviceInfo> inputDevices;
1745 mReader->getInputDevices(inputDevices);
1746 InputDeviceInfo* keyboardInfo = nullptr;
1747 const char* keyboardName = keyboard->getName();
1748 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1749 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1750 keyboardInfo = &inputDevices[i];
1751 break;
1752 }
1753 }
1754 ASSERT_NE(keyboardInfo, nullptr);
1755 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1756 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1757 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1758
1759 keyboard.reset();
1760 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1761 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1762 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1763}
1764
1765TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1766 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1767 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1768
1769 NotifyConfigurationChangedArgs configChangedArgs;
1770 ASSERT_NO_FATAL_FAILURE(
1771 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
1772 uint32_t prevSequenceNum = configChangedArgs.sequenceNum;
1773 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1774
1775 NotifyKeyArgs keyArgs;
1776 keyboard->pressAndReleaseHomeKey();
1777 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1778 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
1779 ASSERT_LT(prevSequenceNum, keyArgs.sequenceNum);
1780 prevSequenceNum = keyArgs.sequenceNum;
1781 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1782 prevTimestamp = keyArgs.eventTime;
1783
1784 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1785 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
1786 ASSERT_LT(prevSequenceNum, keyArgs.sequenceNum);
1787 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1788}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001789
1790// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001791class InputDeviceTest : public testing::Test {
1792protected:
1793 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001794 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001795 static const int32_t DEVICE_ID;
1796 static const int32_t DEVICE_GENERATION;
1797 static const int32_t DEVICE_CONTROLLER_NUMBER;
1798 static const uint32_t DEVICE_CLASSES;
1799
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001800 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001802 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803 FakeInputReaderContext* mFakeContext;
1804
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001805 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001806
Prabir Pradhan28efc192019-11-05 01:10:04 +00001807 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001808 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001810 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1812
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001813 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001814 InputDeviceIdentifier identifier;
1815 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001816 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001817 mDevice =
1818 std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1819 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820 }
1821
Prabir Pradhan28efc192019-11-05 01:10:04 +00001822 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001823 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824 delete mFakeContext;
1825 mFakeListener.clear();
1826 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001827 }
1828};
1829
1830const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001831const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832const int32_t InputDeviceTest::DEVICE_ID = 1;
1833const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1834const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1835const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1836 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1837
1838TEST_F(InputDeviceTest, ImmutableProperties) {
1839 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001840 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001841 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1842}
1843
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001844TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1845 ASSERT_EQ(mDevice->isEnabled(), true);
1846}
1847
Michael Wrightd02c5b62014-02-10 15:10:22 -08001848TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1849 // Configuration.
1850 InputReaderConfiguration config;
1851 mDevice->configure(ARBITRARY_TIME, &config, 0);
1852
1853 // Reset.
1854 mDevice->reset(ARBITRARY_TIME);
1855
1856 NotifyDeviceResetArgs resetArgs;
1857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1858 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1859 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1860
1861 // Metadata.
1862 ASSERT_TRUE(mDevice->isIgnored());
1863 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1864
1865 InputDeviceInfo info;
1866 mDevice->getDeviceInfo(&info);
1867 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001868 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1870 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1871
1872 // State queries.
1873 ASSERT_EQ(0, mDevice->getMetaState());
1874
1875 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1876 << "Ignored device should return unknown key code state.";
1877 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1878 << "Ignored device should return unknown scan code state.";
1879 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1880 << "Ignored device should return unknown switch state.";
1881
1882 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1883 uint8_t flags[2] = { 0, 1 };
1884 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1885 << "Ignored device should never mark any key codes.";
1886 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1887 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1888}
1889
1890TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1891 // Configuration.
1892 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1893
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001894 FakeInputMapper& mapper1 = mDevice->addMapper<FakeInputMapper>(AINPUT_SOURCE_KEYBOARD);
1895 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1896 mapper1.setMetaState(AMETA_ALT_ON);
1897 mapper1.addSupportedKeyCode(AKEYCODE_A);
1898 mapper1.addSupportedKeyCode(AKEYCODE_B);
1899 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1900 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1901 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
1902 mapper1.setScanCodeState(3, AKEY_STATE_UP);
1903 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001905 FakeInputMapper& mapper2 = mDevice->addMapper<FakeInputMapper>(AINPUT_SOURCE_TOUCHSCREEN);
1906 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001907
1908 InputReaderConfiguration config;
1909 mDevice->configure(ARBITRARY_TIME, &config, 0);
1910
1911 String8 propertyValue;
1912 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1913 << "Device should have read configuration during configuration phase.";
1914 ASSERT_STREQ("value", propertyValue.string());
1915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001916 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
1917 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918
1919 // Reset
1920 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001921 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
1922 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923
1924 NotifyDeviceResetArgs resetArgs;
1925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1926 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1927 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1928
1929 // Metadata.
1930 ASSERT_FALSE(mDevice->isIgnored());
1931 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1932
1933 InputDeviceInfo info;
1934 mDevice->getDeviceInfo(&info);
1935 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001936 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001937 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1938 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1939
1940 // State queries.
1941 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1942 << "Should query mappers and combine meta states.";
1943
1944 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1945 << "Should return unknown key code state when source not supported.";
1946 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1947 << "Should return unknown scan code state when source not supported.";
1948 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1949 << "Should return unknown switch state when source not supported.";
1950
1951 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1952 << "Should query mapper when source is supported.";
1953 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1954 << "Should query mapper when source is supported.";
1955 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1956 << "Should query mapper when source is supported.";
1957
1958 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1959 uint8_t flags[4] = { 0, 0, 0, 1 };
1960 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1961 << "Should do nothing when source is unsupported.";
1962 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1963 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1964 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1965 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1966
1967 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1968 << "Should query mapper when source is supported.";
1969 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1970 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1971 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1972 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1973
1974 // Event handling.
1975 RawEvent event;
1976 mDevice->process(&event, 1);
1977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001978 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
1979 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001980}
1981
Arthur Hung2c9a3342019-07-23 14:18:59 +08001982// A single input device is associated with a specific display. Check that:
1983// 1. Device is disabled if the viewport corresponding to the associated display is not found
1984// 2. Device is disabled when setEnabled API is called
1985TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001986 mDevice->addMapper<FakeInputMapper>(AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08001987
1988 // First Configuration.
1989 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1990
1991 // Device should be enabled by default.
1992 ASSERT_TRUE(mDevice->isEnabled());
1993
1994 // Prepare associated info.
1995 constexpr uint8_t hdmi = 1;
1996 const std::string UNIQUE_ID = "local:1";
1997
1998 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1999 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2000 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2001 // Device should be disabled because it is associated with a specific display via
2002 // input port <-> display port association, but the corresponding display is not found
2003 ASSERT_FALSE(mDevice->isEnabled());
2004
2005 // Prepare displays.
2006 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2007 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2008 ViewportType::VIEWPORT_INTERNAL);
2009 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2010 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2011 ASSERT_TRUE(mDevice->isEnabled());
2012
2013 // Device should be disabled after set disable.
2014 mFakePolicy->addDisabledDevice(mDevice->getId());
2015 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2016 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2017 ASSERT_FALSE(mDevice->isEnabled());
2018
2019 // Device should still be disabled even found the associated display.
2020 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2021 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2022 ASSERT_FALSE(mDevice->isEnabled());
2023}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002024
2025// --- InputMapperTest ---
2026
2027class InputMapperTest : public testing::Test {
2028protected:
2029 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002030 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002031 static const int32_t DEVICE_ID;
2032 static const int32_t DEVICE_GENERATION;
2033 static const int32_t DEVICE_CONTROLLER_NUMBER;
2034 static const uint32_t DEVICE_CLASSES;
2035
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002036 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002037 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002038 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002039 FakeInputReaderContext* mFakeContext;
2040 InputDevice* mDevice;
2041
Prabir Pradhan28efc192019-11-05 01:10:04 +00002042 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002043 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002044 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002045 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002046 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2047 InputDeviceIdentifier identifier;
2048 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002049 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002050 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2051 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2052
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002053 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002054 }
2055
Prabir Pradhan28efc192019-11-05 01:10:04 +00002056 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002057 delete mDevice;
2058 delete mFakeContext;
2059 mFakeListener.clear();
2060 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002061 }
2062
2063 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002064 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002065 }
2066
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002067 void configureDevice(uint32_t changes) {
2068 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2069 }
2070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002071 template <class T, typename... Args>
2072 T& addMapperAndConfigure(Args... args) {
2073 T& mapper = mDevice->addMapper<T>(args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002074 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002075 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002076 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002077 }
2078
2079 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002080 int32_t orientation, const std::string& uniqueId,
2081 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002082 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002083 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002084 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2085 }
2086
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002087 void clearViewports() {
2088 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002089 }
2090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002091 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2092 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002093 RawEvent event;
2094 event.when = when;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002095 event.deviceId = mapper.getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002096 event.type = type;
2097 event.code = code;
2098 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002099 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100 }
2101
2102 static void assertMotionRange(const InputDeviceInfo& info,
2103 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2104 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002105 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002106 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2107 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2108 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2109 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2110 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2111 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2112 }
2113
2114 static void assertPointerCoords(const PointerCoords& coords,
2115 float x, float y, float pressure, float size,
2116 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2117 float orientation, float distance) {
2118 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2119 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2120 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2121 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2122 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2123 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2124 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2125 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2126 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2127 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2128 }
2129
2130 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2131 float actualX, actualY;
2132 controller->getPosition(&actualX, &actualY);
2133 ASSERT_NEAR(x, actualX, 1);
2134 ASSERT_NEAR(y, actualY, 1);
2135 }
2136};
2137
2138const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002139const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002140const int32_t InputMapperTest::DEVICE_ID = 1;
2141const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2142const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2143const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
2144
2145
2146// --- SwitchInputMapperTest ---
2147
2148class SwitchInputMapperTest : public InputMapperTest {
2149protected:
2150};
2151
2152TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002153 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002155 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002156}
2157
2158TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002159 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002160
2161 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002162 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002163
2164 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002165 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002166}
2167
2168TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002169 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002170
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002171 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2172 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2173 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2174 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002175
2176 NotifySwitchArgs args;
2177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2178 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002179 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2180 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002181 args.switchMask);
2182 ASSERT_EQ(uint32_t(0), args.policyFlags);
2183}
2184
2185
2186// --- KeyboardInputMapperTest ---
2187
2188class KeyboardInputMapperTest : public InputMapperTest {
2189protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002190 const std::string UNIQUE_ID = "local:0";
2191
2192 void prepareDisplay(int32_t orientation);
2193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002194 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002195 int32_t originalKeyCode, int32_t rotatedKeyCode,
2196 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002197};
2198
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002199/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2200 * orientation.
2201 */
2202void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2203 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002204 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002205}
2206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002207void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002208 int32_t originalScanCode, int32_t originalKeyCode,
2209 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002210 NotifyKeyArgs args;
2211
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002212 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2214 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2215 ASSERT_EQ(originalScanCode, args.scanCode);
2216 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002217 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002218
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002219 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2221 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2222 ASSERT_EQ(originalScanCode, args.scanCode);
2223 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002224 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002225}
2226
Michael Wrightd02c5b62014-02-10 15:10:22 -08002227TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002228 KeyboardInputMapper& mapper =
2229 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2230 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002232 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002233}
2234
2235TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2236 const int32_t USAGE_A = 0x070004;
2237 const int32_t USAGE_UNKNOWN = 0x07ffff;
2238 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2239 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002241 KeyboardInputMapper& mapper =
2242 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2243 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002244
2245 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002246 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002247 NotifyKeyArgs args;
2248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2249 ASSERT_EQ(DEVICE_ID, args.deviceId);
2250 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2251 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2252 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2253 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2254 ASSERT_EQ(KEY_HOME, args.scanCode);
2255 ASSERT_EQ(AMETA_NONE, args.metaState);
2256 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2257 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2258 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2259
2260 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002261 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2263 ASSERT_EQ(DEVICE_ID, args.deviceId);
2264 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2265 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2266 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2267 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2268 ASSERT_EQ(KEY_HOME, args.scanCode);
2269 ASSERT_EQ(AMETA_NONE, args.metaState);
2270 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2271 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2272 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2273
2274 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002275 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2276 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2278 ASSERT_EQ(DEVICE_ID, args.deviceId);
2279 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2280 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2281 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2282 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2283 ASSERT_EQ(0, args.scanCode);
2284 ASSERT_EQ(AMETA_NONE, args.metaState);
2285 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2286 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2287 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2288
2289 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002290 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2291 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2293 ASSERT_EQ(DEVICE_ID, args.deviceId);
2294 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2295 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2296 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2297 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2298 ASSERT_EQ(0, args.scanCode);
2299 ASSERT_EQ(AMETA_NONE, args.metaState);
2300 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2301 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2302 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2303
2304 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002305 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2306 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2308 ASSERT_EQ(DEVICE_ID, args.deviceId);
2309 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2310 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2311 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2312 ASSERT_EQ(0, args.keyCode);
2313 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2314 ASSERT_EQ(AMETA_NONE, args.metaState);
2315 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2316 ASSERT_EQ(0U, args.policyFlags);
2317 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2318
2319 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002320 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2321 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2323 ASSERT_EQ(DEVICE_ID, args.deviceId);
2324 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2325 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2326 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2327 ASSERT_EQ(0, args.keyCode);
2328 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2329 ASSERT_EQ(AMETA_NONE, args.metaState);
2330 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2331 ASSERT_EQ(0U, args.policyFlags);
2332 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2333}
2334
2335TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2336 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2337 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2338
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002339 KeyboardInputMapper& mapper =
2340 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2341 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002342
2343 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002344 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345
2346 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002347 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002348 NotifyKeyArgs args;
2349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002351 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2353
2354 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002355 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2357 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002359
2360 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002361 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2363 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002364 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365
2366 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002367 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2369 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002370 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002371 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2372}
2373
2374TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2375 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2376 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2377 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2378 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002380 KeyboardInputMapper& mapper =
2381 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2382 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002384 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002385 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2386 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2387 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2388 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2389 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2390 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2391 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2392 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2393}
2394
2395TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2396 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2397 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2398 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2399 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2400
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002402 KeyboardInputMapper& mapper =
2403 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2404 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002406 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002407 ASSERT_NO_FATAL_FAILURE(
2408 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2409 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2410 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2411 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2412 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2413 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2414 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002416 clearViewports();
2417 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002418 ASSERT_NO_FATAL_FAILURE(
2419 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2420 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2421 AKEYCODE_DPAD_UP, DISPLAY_ID));
2422 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2423 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2424 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2425 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002426
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002427 clearViewports();
2428 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002429 ASSERT_NO_FATAL_FAILURE(
2430 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2431 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2432 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2433 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2434 AKEYCODE_DPAD_UP, DISPLAY_ID));
2435 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2436 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002438 clearViewports();
2439 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002440 ASSERT_NO_FATAL_FAILURE(
2441 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2442 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2443 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2444 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2445 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2446 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2447 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448
2449 // Special case: if orientation changes while key is down, we still emit the same keycode
2450 // in the key up as we did in the key down.
2451 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002452 clearViewports();
2453 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002454 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2456 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2457 ASSERT_EQ(KEY_UP, args.scanCode);
2458 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2459
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002460 clearViewports();
2461 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002462 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2464 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2465 ASSERT_EQ(KEY_UP, args.scanCode);
2466 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2467}
2468
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002469TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2470 // If the keyboard is not orientation aware,
2471 // key events should not be associated with a specific display id
2472 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002474 KeyboardInputMapper& mapper =
2475 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2476 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002477 NotifyKeyArgs args;
2478
2479 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002480 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002482 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2484 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2485
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002486 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002487 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002489 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2491 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2492}
2493
2494TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2495 // If the keyboard is orientation aware,
2496 // key events should be associated with the internal viewport
2497 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2498
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002499 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002500 KeyboardInputMapper& mapper =
2501 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2502 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002503 NotifyKeyArgs args;
2504
2505 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2506 // ^--- already checked by the previous test
2507
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002508 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002509 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002510 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002512 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2514 ASSERT_EQ(DISPLAY_ID, args.displayId);
2515
2516 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002517 clearViewports();
2518 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002519 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002520 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002522 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2524 ASSERT_EQ(newDisplayId, args.displayId);
2525}
2526
Michael Wrightd02c5b62014-02-10 15:10:22 -08002527TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002528 KeyboardInputMapper& mapper =
2529 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2530 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531
2532 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002533 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534
2535 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002536 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002537}
2538
2539TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002540 KeyboardInputMapper& mapper =
2541 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2542 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543
2544 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002545 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546
2547 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002548 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002549}
2550
2551TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002552 KeyboardInputMapper& mapper =
2553 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2554 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555
2556 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2557
2558 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2559 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002560 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561 ASSERT_TRUE(flags[0]);
2562 ASSERT_FALSE(flags[1]);
2563}
2564
2565TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2566 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2567 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2568 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2569 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2570 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2571 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002573 KeyboardInputMapper& mapper =
2574 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2575 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
2577 // Initialization should have turned all of the lights off.
2578 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2579 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2580 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2581
2582 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002583 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2584 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2586 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2587 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002588 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002589
2590 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002591 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2592 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002593 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2594 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2595 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002596 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597
2598 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002599 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2600 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002601 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2602 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2603 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002604 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002605
2606 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002607 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2608 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2610 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2611 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002612 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
2614 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002615 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2616 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2618 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2619 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002620 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621
2622 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002623 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2624 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2626 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2627 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002628 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002629}
2630
Arthur Hung2c9a3342019-07-23 14:18:59 +08002631TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2632 // keyboard 1.
2633 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2634 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2635 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2636 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2637
2638 // keyboard 2.
2639 const std::string USB2 = "USB2";
2640 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2641 InputDeviceIdentifier identifier;
2642 identifier.name = "KEYBOARD2";
2643 identifier.location = USB2;
2644 std::unique_ptr<InputDevice> device2 =
2645 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2646 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2647 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2648 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2649 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2650 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2651 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2652
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002653 KeyboardInputMapper& mapper =
2654 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2655 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002656
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002657 KeyboardInputMapper& mapper2 =
2658 device2->addMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2659 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002660 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2661 device2->reset(ARBITRARY_TIME);
2662
2663 // Prepared displays and associated info.
2664 constexpr uint8_t hdmi1 = 0;
2665 constexpr uint8_t hdmi2 = 1;
2666 const std::string SECONDARY_UNIQUE_ID = "local:1";
2667
2668 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2669 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2670
2671 // No associated display viewport found, should disable the device.
2672 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2673 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2674 ASSERT_FALSE(device2->isEnabled());
2675
2676 // Prepare second display.
2677 constexpr int32_t newDisplayId = 2;
2678 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2679 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2680 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2681 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2682 // Default device will reconfigure above, need additional reconfiguration for another device.
2683 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2684 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2685
2686 // Device should be enabled after the associated display is found.
2687 ASSERT_TRUE(mDevice->isEnabled());
2688 ASSERT_TRUE(device2->isEnabled());
2689
2690 // Test pad key events
2691 ASSERT_NO_FATAL_FAILURE(
2692 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2693 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2694 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2695 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2696 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2697 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2698 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2699
2700 ASSERT_NO_FATAL_FAILURE(
2701 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2702 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2703 AKEYCODE_DPAD_RIGHT, newDisplayId));
2704 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2705 AKEYCODE_DPAD_DOWN, newDisplayId));
2706 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2707 AKEYCODE_DPAD_LEFT, newDisplayId));
2708}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709
Powei Fengd041c5d2019-05-03 17:11:33 -07002710TEST_F(KeyboardInputMapperTest, ExternalDevice_WakeBehavior) {
2711 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2712 // marked as WAKE in the keylayout file to trigger wake.
2713 mDevice->setExternal(true);
2714
2715 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2716 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2717 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE, POLICY_FLAG_WAKE);
2718
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002719 KeyboardInputMapper& mapper =
2720 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2721 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002722
2723 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2724 NotifyKeyArgs args;
2725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2726 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2727
2728 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2730 ASSERT_EQ(uint32_t(0), args.policyFlags);
2731
2732 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2734 ASSERT_EQ(uint32_t(0), args.policyFlags);
2735
2736 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2738 ASSERT_EQ(uint32_t(0), args.policyFlags);
2739
2740 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2742 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2743
2744 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2746 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2747}
2748
2749TEST_F(KeyboardInputMapperTest, ExternalDevice_DoNotWakeByDefaultBehavior) {
2750 // Tv Remote key's wake behavior is prescribed by the keylayout file.
2751 mDevice->setExternal(true);
2752
2753 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2754 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2755 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
2756
Powei Fengd041c5d2019-05-03 17:11:33 -07002757 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002758 KeyboardInputMapper& mapper =
2759 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2760 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002761
2762 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2763 NotifyKeyArgs args;
2764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2765 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2766
2767 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2769 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2770
2771 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2773 ASSERT_EQ(uint32_t(0), args.policyFlags);
2774
2775 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2777 ASSERT_EQ(uint32_t(0), args.policyFlags);
2778
2779 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2781 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2782
2783 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2785 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2786}
2787
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788// --- CursorInputMapperTest ---
2789
2790class CursorInputMapperTest : public InputMapperTest {
2791protected:
2792 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2793
2794 sp<FakePointerController> mFakePointerController;
2795
Prabir Pradhan28efc192019-11-05 01:10:04 +00002796 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002797 InputMapperTest::SetUp();
2798
2799 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002800 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801 }
2802
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002803 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
2804 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002805
2806 void prepareDisplay(int32_t orientation) {
2807 const std::string uniqueId = "local:0";
2808 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2809 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2810 orientation, uniqueId, NO_PORT, viewportType);
2811 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812};
2813
2814const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2815
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002816void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
2817 int32_t originalY, int32_t rotatedX,
2818 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819 NotifyMotionArgs args;
2820
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002821 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2822 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2823 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2827 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2828 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2829 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2830}
2831
2832TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002834 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002836 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837}
2838
2839TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002840 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002841 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002843 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844}
2845
2846TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002848 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849
2850 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002851 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852
2853 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002854 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2855 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2857 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2858
2859 // When the bounds are set, then there should be a valid motion range.
2860 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2861
2862 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002863 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864
2865 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2866 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2867 1, 800 - 1, 0.0f, 0.0f));
2868 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2869 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2870 2, 480 - 1, 0.0f, 0.0f));
2871 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2872 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2873 0.0f, 1.0f, 0.0f, 0.0f));
2874}
2875
2876TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002878 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879
2880 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002881 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882
2883 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2884 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2885 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2886 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2887 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2888 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2889 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2890 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2891 0.0f, 1.0f, 0.0f, 0.0f));
2892}
2893
2894TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002895 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002896 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897
2898 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2899
2900 NotifyMotionArgs args;
2901
2902 // Button press.
2903 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002904 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2905 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2907 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2908 ASSERT_EQ(DEVICE_ID, args.deviceId);
2909 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2910 ASSERT_EQ(uint32_t(0), args.policyFlags);
2911 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2912 ASSERT_EQ(0, args.flags);
2913 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2914 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2915 ASSERT_EQ(0, args.edgeFlags);
2916 ASSERT_EQ(uint32_t(1), args.pointerCount);
2917 ASSERT_EQ(0, args.pointerProperties[0].id);
2918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2920 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2921 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2922 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2923 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2924
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2926 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2927 ASSERT_EQ(DEVICE_ID, args.deviceId);
2928 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2929 ASSERT_EQ(uint32_t(0), args.policyFlags);
2930 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2931 ASSERT_EQ(0, args.flags);
2932 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2933 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2934 ASSERT_EQ(0, args.edgeFlags);
2935 ASSERT_EQ(uint32_t(1), args.pointerCount);
2936 ASSERT_EQ(0, args.pointerProperties[0].id);
2937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2939 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2940 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2941 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2942 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2943
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002945 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2946 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2948 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2949 ASSERT_EQ(DEVICE_ID, args.deviceId);
2950 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2951 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002952 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2953 ASSERT_EQ(0, args.flags);
2954 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2955 ASSERT_EQ(0, args.buttonState);
2956 ASSERT_EQ(0, args.edgeFlags);
2957 ASSERT_EQ(uint32_t(1), args.pointerCount);
2958 ASSERT_EQ(0, args.pointerProperties[0].id);
2959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2961 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2962 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2963 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2964 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2965
2966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2967 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2968 ASSERT_EQ(DEVICE_ID, args.deviceId);
2969 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2970 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2972 ASSERT_EQ(0, args.flags);
2973 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2974 ASSERT_EQ(0, args.buttonState);
2975 ASSERT_EQ(0, args.edgeFlags);
2976 ASSERT_EQ(uint32_t(1), args.pointerCount);
2977 ASSERT_EQ(0, args.pointerProperties[0].id);
2978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2980 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2981 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2982 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2983 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2984}
2985
2986TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002988 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989
2990 NotifyMotionArgs args;
2991
2992 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002993 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2994 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2998 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2999
3000 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003001 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3002 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3006 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3007}
3008
3009TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003011 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012
3013 NotifyMotionArgs args;
3014
3015 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003016 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3017 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3019 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3021 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3022
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3026 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3027
Michael Wrightd02c5b62014-02-10 15:10:22 -08003028 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003029 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3034 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3035
3036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3039 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3040}
3041
3042TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003044 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045
3046 NotifyMotionArgs args;
3047
3048 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003049 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3050 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3051 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3052 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3054 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3056 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3057 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3058
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3062 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3063 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3064
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003066 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3067 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3068 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3072 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3073 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3074
3075 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003076 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3077 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003079 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3081 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3082
3083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3086 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3087}
3088
3089TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003093 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3098 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3099 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3102}
3103
3104TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 addConfigurationProperty("cursor.mode", "navigation");
3106 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003107 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003109 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3117 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3118
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003119 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3121 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3122 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3123 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3128
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003129 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3138
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003139 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3148}
3149
3150TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003152 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153
3154 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3155 mFakePointerController->setPosition(100, 200);
3156 mFakePointerController->setButtonState(0);
3157
3158 NotifyMotionArgs motionArgs;
3159 NotifyKeyArgs keyArgs;
3160
3161 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003162 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3163 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3165 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3166 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3167 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3169 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3170
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3173 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3174 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3176 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3177
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003178 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3179 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182 ASSERT_EQ(0, motionArgs.buttonState);
3183 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3185 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3186
3187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003188 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003189 ASSERT_EQ(0, motionArgs.buttonState);
3190 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3192 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3193
3194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003196 ASSERT_EQ(0, motionArgs.buttonState);
3197 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3199 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3200
3201 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003202 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3203 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3204 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3206 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3207 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3208 motionArgs.buttonState);
3209 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3210 mFakePointerController->getButtonState());
3211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3212 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3213
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3216 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3217 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3218 mFakePointerController->getButtonState());
3219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3220 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3221
3222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3224 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3225 motionArgs.buttonState);
3226 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3227 mFakePointerController->getButtonState());
3228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3229 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3230
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003231 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3232 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003234 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3236 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3238 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3239
3240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003242 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3243 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3245 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3246
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003247 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3248 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3251 ASSERT_EQ(0, motionArgs.buttonState);
3252 ASSERT_EQ(0, mFakePointerController->getButtonState());
3253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3254 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 -08003255 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3256 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003257
3258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 ASSERT_EQ(0, motionArgs.buttonState);
3260 ASSERT_EQ(0, mFakePointerController->getButtonState());
3261 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3263 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003264
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3266 ASSERT_EQ(0, motionArgs.buttonState);
3267 ASSERT_EQ(0, mFakePointerController->getButtonState());
3268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3270 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3271
3272 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003273 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3274 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3276 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3277 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003278
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003280 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3282 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3284 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3285
3286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3288 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3289 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3291 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3292
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003293 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3294 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297 ASSERT_EQ(0, motionArgs.buttonState);
3298 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3300 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3301
3302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003304 ASSERT_EQ(0, motionArgs.buttonState);
3305 ASSERT_EQ(0, mFakePointerController->getButtonState());
3306
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3308 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3310 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3311 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3312
3313 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003314 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3315 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3317 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3318 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003319
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003321 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3323 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3325 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3326
3327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3329 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3330 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3332 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3333
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003334 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3335 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 ASSERT_EQ(0, motionArgs.buttonState);
3339 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3341 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003342
3343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3344 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3345 ASSERT_EQ(0, motionArgs.buttonState);
3346 ASSERT_EQ(0, mFakePointerController->getButtonState());
3347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3348 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3349
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3351 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3352 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3353
3354 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003355 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3356 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3358 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3359 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003360
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003362 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3364 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3366 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3367
3368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3369 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3370 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3371 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3373 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3374
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003375 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3376 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003378 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379 ASSERT_EQ(0, motionArgs.buttonState);
3380 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3382 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 -08003383
3384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3385 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3386 ASSERT_EQ(0, motionArgs.buttonState);
3387 ASSERT_EQ(0, mFakePointerController->getButtonState());
3388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3389 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3390
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3392 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3393 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3394
3395 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003396 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3397 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3399 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3400 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003401
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3405 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3407 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3408
3409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3411 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3412 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3414 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3415
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003416 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3417 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003419 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 ASSERT_EQ(0, motionArgs.buttonState);
3421 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3423 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 -08003424
3425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3426 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3427 ASSERT_EQ(0, motionArgs.buttonState);
3428 ASSERT_EQ(0, mFakePointerController->getButtonState());
3429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3430 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3431
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3433 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3434 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3435}
3436
3437TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003439 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440
3441 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3442 mFakePointerController->setPosition(100, 200);
3443 mFakePointerController->setButtonState(0);
3444
3445 NotifyMotionArgs args;
3446
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003447 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3448 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3449 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003451 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3452 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3454 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3455 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3456}
3457
3458TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003459 addConfigurationProperty("cursor.mode", "pointer");
3460 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003461 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003462
3463 NotifyDeviceResetArgs resetArgs;
3464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3465 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3466 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3467
3468 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3469 mFakePointerController->setPosition(100, 200);
3470 mFakePointerController->setButtonState(0);
3471
3472 NotifyMotionArgs args;
3473
3474 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003475 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3476 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3477 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3479 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3482 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3483 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3484
3485 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003486 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3487 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3489 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3490 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3492 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3494 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3497 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3498
3499 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003500 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3501 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3503 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3506 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3508 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3509 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3511 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3512
3513 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003514 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3515 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3516 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3518 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3521 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3522 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3523
3524 // Disable pointer capture and check that the device generation got bumped
3525 // and events are generated the usual way.
3526 const uint32_t generation = mFakeContext->getGeneration();
3527 mFakePolicy->setPointerCapture(false);
3528 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3529 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3530
3531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3532 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3533 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3534
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003535 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3536 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3537 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3539 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3542 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3543 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3544}
3545
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003546TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003547 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003548
Garfield Tan888a6a42020-01-09 11:39:16 -08003549 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003550 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003551 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3552 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3553 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3554 ViewportType::VIEWPORT_EXTERNAL);
3555 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3556 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3557
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003558 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3559 mFakePointerController->setPosition(100, 200);
3560 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003561
3562 NotifyMotionArgs args;
3563 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3564 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3565 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3567 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3568 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3570 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3571 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3572 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3573}
3574
Michael Wrightd02c5b62014-02-10 15:10:22 -08003575// --- TouchInputMapperTest ---
3576
3577class TouchInputMapperTest : public InputMapperTest {
3578protected:
3579 static const int32_t RAW_X_MIN;
3580 static const int32_t RAW_X_MAX;
3581 static const int32_t RAW_Y_MIN;
3582 static const int32_t RAW_Y_MAX;
3583 static const int32_t RAW_TOUCH_MIN;
3584 static const int32_t RAW_TOUCH_MAX;
3585 static const int32_t RAW_TOOL_MIN;
3586 static const int32_t RAW_TOOL_MAX;
3587 static const int32_t RAW_PRESSURE_MIN;
3588 static const int32_t RAW_PRESSURE_MAX;
3589 static const int32_t RAW_ORIENTATION_MIN;
3590 static const int32_t RAW_ORIENTATION_MAX;
3591 static const int32_t RAW_DISTANCE_MIN;
3592 static const int32_t RAW_DISTANCE_MAX;
3593 static const int32_t RAW_TILT_MIN;
3594 static const int32_t RAW_TILT_MAX;
3595 static const int32_t RAW_ID_MIN;
3596 static const int32_t RAW_ID_MAX;
3597 static const int32_t RAW_SLOT_MIN;
3598 static const int32_t RAW_SLOT_MAX;
3599 static const float X_PRECISION;
3600 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003601 static const float X_PRECISION_VIRTUAL;
3602 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603
3604 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003605 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606
3607 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3608
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003609 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003610 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003611
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612 enum Axes {
3613 POSITION = 1 << 0,
3614 TOUCH = 1 << 1,
3615 TOOL = 1 << 2,
3616 PRESSURE = 1 << 3,
3617 ORIENTATION = 1 << 4,
3618 MINOR = 1 << 5,
3619 ID = 1 << 6,
3620 DISTANCE = 1 << 7,
3621 TILT = 1 << 8,
3622 SLOT = 1 << 9,
3623 TOOL_TYPE = 1 << 10,
3624 };
3625
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003626 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3627 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003628 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003630 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003631 int32_t toRawX(float displayX);
3632 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003633 float toCookedX(float rawX, float rawY);
3634 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003636 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003637 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003638 float toDisplayY(int32_t rawY, int32_t displayHeight);
3639
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640};
3641
3642const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3643const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3644const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3645const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3646const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3647const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3648const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3649const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003650const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3651const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3653const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3654const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3655const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3656const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3657const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3658const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3659const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3660const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3661const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3662const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3663const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003664const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3665 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3666const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3667 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003668const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3669 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670
3671const float TouchInputMapperTest::GEOMETRIC_SCALE =
3672 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3673 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3674
3675const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3676 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3677 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3678};
3679
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003680void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003681 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003682 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3683}
3684
3685void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3686 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3687 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688}
3689
Santos Cordonfa5cf462017-04-05 10:37:00 -07003690void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003691 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3692 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003693 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003694}
3695
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696void TouchInputMapperTest::prepareVirtualKeys() {
3697 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3698 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3699 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3700 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3701}
3702
Jason Gerecke489fda82012-09-07 17:19:40 -07003703void TouchInputMapperTest::prepareLocationCalibration() {
3704 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3705}
3706
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707int32_t TouchInputMapperTest::toRawX(float displayX) {
3708 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3709}
3710
3711int32_t TouchInputMapperTest::toRawY(float displayY) {
3712 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3713}
3714
Jason Gerecke489fda82012-09-07 17:19:40 -07003715float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3716 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3717 return rawX;
3718}
3719
3720float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3721 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3722 return rawY;
3723}
3724
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003726 return toDisplayX(rawX, DISPLAY_WIDTH);
3727}
3728
3729float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3730 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731}
3732
3733float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003734 return toDisplayY(rawY, DISPLAY_HEIGHT);
3735}
3736
3737float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3738 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739}
3740
3741
3742// --- SingleTouchInputMapperTest ---
3743
3744class SingleTouchInputMapperTest : public TouchInputMapperTest {
3745protected:
3746 void prepareButtons();
3747 void prepareAxes(int axes);
3748
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003749 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3750 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3751 void processUp(SingleTouchInputMapper& mappery);
3752 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3753 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3754 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3755 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3756 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3757 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758};
3759
3760void SingleTouchInputMapperTest::prepareButtons() {
3761 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3762}
3763
3764void SingleTouchInputMapperTest::prepareAxes(int axes) {
3765 if (axes & POSITION) {
3766 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3767 RAW_X_MIN, RAW_X_MAX, 0, 0);
3768 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3769 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3770 }
3771 if (axes & PRESSURE) {
3772 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3773 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3774 }
3775 if (axes & TOOL) {
3776 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3777 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3778 }
3779 if (axes & DISTANCE) {
3780 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3781 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3782 }
3783 if (axes & TILT) {
3784 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3785 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3786 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3787 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3788 }
3789}
3790
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003791void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003792 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3793 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3794 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795}
3796
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003797void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003798 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3799 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800}
3801
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003802void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003803 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804}
3805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003807 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808}
3809
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003810void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
3811 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003812 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813}
3814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003815void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003816 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817}
3818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003819void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
3820 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003821 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3822 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823}
3824
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003825void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
3826 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003827 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828}
3829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003830void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003831 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832}
3833
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835 prepareButtons();
3836 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003837 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003839 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840}
3841
3842TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3844 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3845 prepareButtons();
3846 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003847 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003849 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850}
3851
3852TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853 prepareButtons();
3854 prepareAxes(POSITION);
3855 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003856 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859}
3860
3861TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862 prepareButtons();
3863 prepareAxes(POSITION);
3864 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003867 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868}
3869
3870TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871 addConfigurationProperty("touch.deviceType", "touchScreen");
3872 prepareDisplay(DISPLAY_ORIENTATION_0);
3873 prepareButtons();
3874 prepareAxes(POSITION);
3875 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877
3878 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003879 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880
3881 // Virtual key is down.
3882 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3883 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3884 processDown(mapper, x, y);
3885 processSync(mapper);
3886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3887
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003888 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889
3890 // Virtual key is up.
3891 processUp(mapper);
3892 processSync(mapper);
3893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003895 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896}
3897
3898TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899 addConfigurationProperty("touch.deviceType", "touchScreen");
3900 prepareDisplay(DISPLAY_ORIENTATION_0);
3901 prepareButtons();
3902 prepareAxes(POSITION);
3903 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003904 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905
3906 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003907 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908
3909 // Virtual key is down.
3910 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3911 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3912 processDown(mapper, x, y);
3913 processSync(mapper);
3914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003916 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917
3918 // Virtual key is up.
3919 processUp(mapper);
3920 processSync(mapper);
3921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3922
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003923 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924}
3925
3926TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 addConfigurationProperty("touch.deviceType", "touchScreen");
3928 prepareDisplay(DISPLAY_ORIENTATION_0);
3929 prepareButtons();
3930 prepareAxes(POSITION);
3931 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003932 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933
3934 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3935 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003936 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937 ASSERT_TRUE(flags[0]);
3938 ASSERT_FALSE(flags[1]);
3939}
3940
3941TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942 addConfigurationProperty("touch.deviceType", "touchScreen");
3943 prepareDisplay(DISPLAY_ORIENTATION_0);
3944 prepareButtons();
3945 prepareAxes(POSITION);
3946 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003947 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948
3949 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3950
3951 NotifyKeyArgs args;
3952
3953 // Press virtual key.
3954 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3955 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3956 processDown(mapper, x, y);
3957 processSync(mapper);
3958
3959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3960 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3961 ASSERT_EQ(DEVICE_ID, args.deviceId);
3962 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3963 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3964 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3965 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3966 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3967 ASSERT_EQ(KEY_HOME, args.scanCode);
3968 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3969 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3970
3971 // Release virtual key.
3972 processUp(mapper);
3973 processSync(mapper);
3974
3975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3976 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3977 ASSERT_EQ(DEVICE_ID, args.deviceId);
3978 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3979 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3980 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3981 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3982 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3983 ASSERT_EQ(KEY_HOME, args.scanCode);
3984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3985 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3986
3987 // Should not have sent any motions.
3988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3989}
3990
3991TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992 addConfigurationProperty("touch.deviceType", "touchScreen");
3993 prepareDisplay(DISPLAY_ORIENTATION_0);
3994 prepareButtons();
3995 prepareAxes(POSITION);
3996 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998
3999 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4000
4001 NotifyKeyArgs keyArgs;
4002
4003 // Press virtual key.
4004 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4005 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4006 processDown(mapper, x, y);
4007 processSync(mapper);
4008
4009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4010 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4011 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4012 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4013 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4014 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4015 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4016 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4017 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4018 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4019 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4020
4021 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4022 // into the display area.
4023 y -= 100;
4024 processMove(mapper, x, y);
4025 processSync(mapper);
4026
4027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4028 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4029 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4030 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4031 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4032 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4033 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4034 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4035 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4036 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4037 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4038 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4039
4040 NotifyMotionArgs motionArgs;
4041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4042 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4043 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4044 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4045 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4046 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4047 ASSERT_EQ(0, motionArgs.flags);
4048 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4049 ASSERT_EQ(0, motionArgs.buttonState);
4050 ASSERT_EQ(0, motionArgs.edgeFlags);
4051 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4052 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4055 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4056 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4057 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4058 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4059
4060 // Keep moving out of bounds. Should generate a pointer move.
4061 y -= 50;
4062 processMove(mapper, x, y);
4063 processSync(mapper);
4064
4065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4066 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4067 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4068 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4069 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4071 ASSERT_EQ(0, motionArgs.flags);
4072 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4073 ASSERT_EQ(0, motionArgs.buttonState);
4074 ASSERT_EQ(0, motionArgs.edgeFlags);
4075 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4076 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4079 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4080 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4081 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4083
4084 // Release out of bounds. Should generate a pointer up.
4085 processUp(mapper);
4086 processSync(mapper);
4087
4088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4089 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4090 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4091 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4092 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4093 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4094 ASSERT_EQ(0, motionArgs.flags);
4095 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4096 ASSERT_EQ(0, motionArgs.buttonState);
4097 ASSERT_EQ(0, motionArgs.edgeFlags);
4098 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4099 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4102 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4103 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4104 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4105 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4106
4107 // Should not have sent any more keys or motions.
4108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4110}
4111
4112TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 addConfigurationProperty("touch.deviceType", "touchScreen");
4114 prepareDisplay(DISPLAY_ORIENTATION_0);
4115 prepareButtons();
4116 prepareAxes(POSITION);
4117 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004118 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119
4120 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4121
4122 NotifyMotionArgs motionArgs;
4123
4124 // Initially go down out of bounds.
4125 int32_t x = -10;
4126 int32_t y = -10;
4127 processDown(mapper, x, y);
4128 processSync(mapper);
4129
4130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4131
4132 // Move into the display area. Should generate a pointer down.
4133 x = 50;
4134 y = 75;
4135 processMove(mapper, x, y);
4136 processSync(mapper);
4137
4138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4139 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4140 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4141 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4142 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4144 ASSERT_EQ(0, motionArgs.flags);
4145 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4146 ASSERT_EQ(0, motionArgs.buttonState);
4147 ASSERT_EQ(0, motionArgs.edgeFlags);
4148 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4149 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4152 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4153 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4154 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4155 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4156
4157 // Release. Should generate a pointer up.
4158 processUp(mapper);
4159 processSync(mapper);
4160
4161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4162 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4163 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4164 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4165 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4166 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4167 ASSERT_EQ(0, motionArgs.flags);
4168 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4169 ASSERT_EQ(0, motionArgs.buttonState);
4170 ASSERT_EQ(0, motionArgs.edgeFlags);
4171 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4172 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4175 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4176 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4177 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4178 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4179
4180 // Should not have sent any more keys or motions.
4181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4183}
4184
Santos Cordonfa5cf462017-04-05 10:37:00 -07004185TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004186 addConfigurationProperty("touch.deviceType", "touchScreen");
4187 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4188
4189 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4190 prepareButtons();
4191 prepareAxes(POSITION);
4192 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004193 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004194
4195 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4196
4197 NotifyMotionArgs motionArgs;
4198
4199 // Down.
4200 int32_t x = 100;
4201 int32_t y = 125;
4202 processDown(mapper, x, y);
4203 processSync(mapper);
4204
4205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4207 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4208 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4209 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4210 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4211 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4212 ASSERT_EQ(0, motionArgs.flags);
4213 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4214 ASSERT_EQ(0, motionArgs.buttonState);
4215 ASSERT_EQ(0, motionArgs.edgeFlags);
4216 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4217 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4220 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4221 1, 0, 0, 0, 0, 0, 0, 0));
4222 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4223 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4225
4226 // Move.
4227 x += 50;
4228 y += 75;
4229 processMove(mapper, x, y);
4230 processSync(mapper);
4231
4232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4234 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4235 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4236 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4237 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4239 ASSERT_EQ(0, motionArgs.flags);
4240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4241 ASSERT_EQ(0, motionArgs.buttonState);
4242 ASSERT_EQ(0, motionArgs.edgeFlags);
4243 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4244 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4247 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4248 1, 0, 0, 0, 0, 0, 0, 0));
4249 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4250 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4251 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4252
4253 // Up.
4254 processUp(mapper);
4255 processSync(mapper);
4256
4257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4259 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4260 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4261 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4262 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4263 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4264 ASSERT_EQ(0, motionArgs.flags);
4265 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4266 ASSERT_EQ(0, motionArgs.buttonState);
4267 ASSERT_EQ(0, motionArgs.edgeFlags);
4268 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4269 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4272 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4273 1, 0, 0, 0, 0, 0, 0, 0));
4274 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4275 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4277
4278 // Should not have sent any more keys or motions.
4279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4281}
4282
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 addConfigurationProperty("touch.deviceType", "touchScreen");
4285 prepareDisplay(DISPLAY_ORIENTATION_0);
4286 prepareButtons();
4287 prepareAxes(POSITION);
4288 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004289 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290
4291 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4292
4293 NotifyMotionArgs motionArgs;
4294
4295 // Down.
4296 int32_t x = 100;
4297 int32_t y = 125;
4298 processDown(mapper, x, y);
4299 processSync(mapper);
4300
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4302 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4303 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4304 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4305 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4306 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4307 ASSERT_EQ(0, motionArgs.flags);
4308 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4309 ASSERT_EQ(0, motionArgs.buttonState);
4310 ASSERT_EQ(0, motionArgs.edgeFlags);
4311 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4312 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4315 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4316 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4317 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4318 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4319
4320 // Move.
4321 x += 50;
4322 y += 75;
4323 processMove(mapper, x, y);
4324 processSync(mapper);
4325
4326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4328 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4329 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4330 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4332 ASSERT_EQ(0, motionArgs.flags);
4333 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4334 ASSERT_EQ(0, motionArgs.buttonState);
4335 ASSERT_EQ(0, motionArgs.edgeFlags);
4336 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4340 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4341 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4342 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4344
4345 // Up.
4346 processUp(mapper);
4347 processSync(mapper);
4348
4349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4354 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4355 ASSERT_EQ(0, motionArgs.flags);
4356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4357 ASSERT_EQ(0, motionArgs.buttonState);
4358 ASSERT_EQ(0, motionArgs.edgeFlags);
4359 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4363 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4367
4368 // Should not have sent any more keys or motions.
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4371}
4372
4373TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 addConfigurationProperty("touch.deviceType", "touchScreen");
4375 prepareButtons();
4376 prepareAxes(POSITION);
4377 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004378 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379
4380 NotifyMotionArgs args;
4381
4382 // Rotation 90.
4383 prepareDisplay(DISPLAY_ORIENTATION_90);
4384 processDown(mapper, toRawX(50), toRawY(75));
4385 processSync(mapper);
4386
4387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4388 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4389 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4390
4391 processUp(mapper);
4392 processSync(mapper);
4393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4394}
4395
4396TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 addConfigurationProperty("touch.deviceType", "touchScreen");
4398 prepareButtons();
4399 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004400 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401
4402 NotifyMotionArgs args;
4403
4404 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004405 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 prepareDisplay(DISPLAY_ORIENTATION_0);
4407 processDown(mapper, toRawX(50), toRawY(75));
4408 processSync(mapper);
4409
4410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4411 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4412 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4413
4414 processUp(mapper);
4415 processSync(mapper);
4416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4417
4418 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004419 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 prepareDisplay(DISPLAY_ORIENTATION_90);
4421 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4422 processSync(mapper);
4423
4424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4425 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4426 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4427
4428 processUp(mapper);
4429 processSync(mapper);
4430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4431
4432 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004433 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 prepareDisplay(DISPLAY_ORIENTATION_180);
4435 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4436 processSync(mapper);
4437
4438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4439 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4440 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4441
4442 processUp(mapper);
4443 processSync(mapper);
4444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4445
4446 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004447 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 prepareDisplay(DISPLAY_ORIENTATION_270);
4449 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4450 processSync(mapper);
4451
4452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4453 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4454 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4455
4456 processUp(mapper);
4457 processSync(mapper);
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4459}
4460
4461TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462 addConfigurationProperty("touch.deviceType", "touchScreen");
4463 prepareDisplay(DISPLAY_ORIENTATION_0);
4464 prepareButtons();
4465 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004466 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467
4468 // These calculations are based on the input device calibration documentation.
4469 int32_t rawX = 100;
4470 int32_t rawY = 200;
4471 int32_t rawPressure = 10;
4472 int32_t rawToolMajor = 12;
4473 int32_t rawDistance = 2;
4474 int32_t rawTiltX = 30;
4475 int32_t rawTiltY = 110;
4476
4477 float x = toDisplayX(rawX);
4478 float y = toDisplayY(rawY);
4479 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4480 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4481 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4482 float distance = float(rawDistance);
4483
4484 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4485 float tiltScale = M_PI / 180;
4486 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4487 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4488 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4489 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4490
4491 processDown(mapper, rawX, rawY);
4492 processPressure(mapper, rawPressure);
4493 processToolMajor(mapper, rawToolMajor);
4494 processDistance(mapper, rawDistance);
4495 processTilt(mapper, rawTiltX, rawTiltY);
4496 processSync(mapper);
4497
4498 NotifyMotionArgs args;
4499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4501 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4502 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4503}
4504
Jason Gerecke489fda82012-09-07 17:19:40 -07004505TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004506 addConfigurationProperty("touch.deviceType", "touchScreen");
4507 prepareDisplay(DISPLAY_ORIENTATION_0);
4508 prepareLocationCalibration();
4509 prepareButtons();
4510 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004511 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004512
4513 int32_t rawX = 100;
4514 int32_t rawY = 200;
4515
4516 float x = toDisplayX(toCookedX(rawX, rawY));
4517 float y = toDisplayY(toCookedY(rawX, rawY));
4518
4519 processDown(mapper, rawX, rawY);
4520 processSync(mapper);
4521
4522 NotifyMotionArgs args;
4523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4525 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4526}
4527
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 addConfigurationProperty("touch.deviceType", "touchScreen");
4530 prepareDisplay(DISPLAY_ORIENTATION_0);
4531 prepareButtons();
4532 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004533 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534
4535 NotifyMotionArgs motionArgs;
4536 NotifyKeyArgs keyArgs;
4537
4538 processDown(mapper, 100, 200);
4539 processSync(mapper);
4540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4541 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4542 ASSERT_EQ(0, motionArgs.buttonState);
4543
4544 // press BTN_LEFT, release BTN_LEFT
4545 processKey(mapper, BTN_LEFT, 1);
4546 processSync(mapper);
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4549 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4550
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4553 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4554
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 processKey(mapper, BTN_LEFT, 0);
4556 processSync(mapper);
4557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004560
4561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004563 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
4565 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4566 processKey(mapper, BTN_RIGHT, 1);
4567 processKey(mapper, BTN_MIDDLE, 1);
4568 processSync(mapper);
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4571 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4572 motionArgs.buttonState);
4573
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4576 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4577
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4579 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4580 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4581 motionArgs.buttonState);
4582
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583 processKey(mapper, BTN_RIGHT, 0);
4584 processSync(mapper);
4585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004588
4589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004591 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004592
4593 processKey(mapper, BTN_MIDDLE, 0);
4594 processSync(mapper);
4595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004596 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004598
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004601 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602
4603 // press BTN_BACK, release BTN_BACK
4604 processKey(mapper, BTN_BACK, 1);
4605 processSync(mapper);
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4607 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4608 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004609
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4613
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4616 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617
4618 processKey(mapper, BTN_BACK, 0);
4619 processSync(mapper);
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004623
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004626 ASSERT_EQ(0, motionArgs.buttonState);
4627
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4629 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4630 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4631
4632 // press BTN_SIDE, release BTN_SIDE
4633 processKey(mapper, BTN_SIDE, 1);
4634 processSync(mapper);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4636 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4637 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004638
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004641 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4642
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4645 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646
4647 processKey(mapper, BTN_SIDE, 0);
4648 processSync(mapper);
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655 ASSERT_EQ(0, motionArgs.buttonState);
4656
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4658 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4659 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4660
4661 // press BTN_FORWARD, release BTN_FORWARD
4662 processKey(mapper, BTN_FORWARD, 1);
4663 processSync(mapper);
4664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4665 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4666 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004667
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004670 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4671
4672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4673 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4674 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675
4676 processKey(mapper, BTN_FORWARD, 0);
4677 processSync(mapper);
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004681
4682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004684 ASSERT_EQ(0, motionArgs.buttonState);
4685
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4687 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4688 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4689
4690 // press BTN_EXTRA, release BTN_EXTRA
4691 processKey(mapper, BTN_EXTRA, 1);
4692 processSync(mapper);
4693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4694 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4695 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004696
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004699 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4700
4701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4703 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704
4705 processKey(mapper, BTN_EXTRA, 0);
4706 processSync(mapper);
4707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004710
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004713 ASSERT_EQ(0, motionArgs.buttonState);
4714
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4716 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4717 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4718
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4720
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 // press BTN_STYLUS, release BTN_STYLUS
4722 processKey(mapper, BTN_STYLUS, 1);
4723 processSync(mapper);
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004726 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4727
4728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4729 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4730 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731
4732 processKey(mapper, BTN_STYLUS, 0);
4733 processSync(mapper);
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737
4738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741
4742 // press BTN_STYLUS2, release BTN_STYLUS2
4743 processKey(mapper, BTN_STYLUS2, 1);
4744 processSync(mapper);
4745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004747 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4748
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4751 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752
4753 processKey(mapper, BTN_STYLUS2, 0);
4754 processSync(mapper);
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004758
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004761 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762
4763 // release touch
4764 processUp(mapper);
4765 processSync(mapper);
4766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4768 ASSERT_EQ(0, motionArgs.buttonState);
4769}
4770
4771TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 addConfigurationProperty("touch.deviceType", "touchScreen");
4773 prepareDisplay(DISPLAY_ORIENTATION_0);
4774 prepareButtons();
4775 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004776 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777
4778 NotifyMotionArgs motionArgs;
4779
4780 // default tool type is finger
4781 processDown(mapper, 100, 200);
4782 processSync(mapper);
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4784 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4786
4787 // eraser
4788 processKey(mapper, BTN_TOOL_RUBBER, 1);
4789 processSync(mapper);
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4793
4794 // stylus
4795 processKey(mapper, BTN_TOOL_RUBBER, 0);
4796 processKey(mapper, BTN_TOOL_PEN, 1);
4797 processSync(mapper);
4798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4801
4802 // brush
4803 processKey(mapper, BTN_TOOL_PEN, 0);
4804 processKey(mapper, BTN_TOOL_BRUSH, 1);
4805 processSync(mapper);
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4809
4810 // pencil
4811 processKey(mapper, BTN_TOOL_BRUSH, 0);
4812 processKey(mapper, BTN_TOOL_PENCIL, 1);
4813 processSync(mapper);
4814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4817
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004818 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 processKey(mapper, BTN_TOOL_PENCIL, 0);
4820 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4821 processSync(mapper);
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4825
4826 // mouse
4827 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4828 processKey(mapper, BTN_TOOL_MOUSE, 1);
4829 processSync(mapper);
4830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4833
4834 // lens
4835 processKey(mapper, BTN_TOOL_MOUSE, 0);
4836 processKey(mapper, BTN_TOOL_LENS, 1);
4837 processSync(mapper);
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4841
4842 // double-tap
4843 processKey(mapper, BTN_TOOL_LENS, 0);
4844 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4845 processSync(mapper);
4846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4849
4850 // triple-tap
4851 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4852 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4853 processSync(mapper);
4854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4857
4858 // quad-tap
4859 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4860 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4861 processSync(mapper);
4862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4865
4866 // finger
4867 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4868 processKey(mapper, BTN_TOOL_FINGER, 1);
4869 processSync(mapper);
4870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4873
4874 // stylus trumps finger
4875 processKey(mapper, BTN_TOOL_PEN, 1);
4876 processSync(mapper);
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4880
4881 // eraser trumps stylus
4882 processKey(mapper, BTN_TOOL_RUBBER, 1);
4883 processSync(mapper);
4884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4887
4888 // mouse trumps eraser
4889 processKey(mapper, BTN_TOOL_MOUSE, 1);
4890 processSync(mapper);
4891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4894
4895 // back to default tool type
4896 processKey(mapper, BTN_TOOL_MOUSE, 0);
4897 processKey(mapper, BTN_TOOL_RUBBER, 0);
4898 processKey(mapper, BTN_TOOL_PEN, 0);
4899 processKey(mapper, BTN_TOOL_FINGER, 0);
4900 processSync(mapper);
4901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4903 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4904}
4905
4906TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907 addConfigurationProperty("touch.deviceType", "touchScreen");
4908 prepareDisplay(DISPLAY_ORIENTATION_0);
4909 prepareButtons();
4910 prepareAxes(POSITION);
4911 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004912 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913
4914 NotifyMotionArgs motionArgs;
4915
4916 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4917 processKey(mapper, BTN_TOOL_FINGER, 1);
4918 processMove(mapper, 100, 200);
4919 processSync(mapper);
4920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4921 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4923 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4924
4925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4926 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4928 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4929
4930 // move a little
4931 processMove(mapper, 150, 250);
4932 processSync(mapper);
4933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4934 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4936 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4937
4938 // down when BTN_TOUCH is pressed, pressure defaults to 1
4939 processKey(mapper, BTN_TOUCH, 1);
4940 processSync(mapper);
4941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4942 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4944 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4945
4946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4947 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4949 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4950
4951 // up when BTN_TOUCH is released, hover restored
4952 processKey(mapper, BTN_TOUCH, 0);
4953 processSync(mapper);
4954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4955 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4957 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4958
4959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4960 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4962 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4963
4964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4965 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4967 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4968
4969 // exit hover when pointer goes away
4970 processKey(mapper, BTN_TOOL_FINGER, 0);
4971 processSync(mapper);
4972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4973 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4975 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4976}
4977
4978TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979 addConfigurationProperty("touch.deviceType", "touchScreen");
4980 prepareDisplay(DISPLAY_ORIENTATION_0);
4981 prepareButtons();
4982 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004983 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984
4985 NotifyMotionArgs motionArgs;
4986
4987 // initially hovering because pressure is 0
4988 processDown(mapper, 100, 200);
4989 processPressure(mapper, 0);
4990 processSync(mapper);
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4992 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4994 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4995
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4999 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5000
5001 // move a little
5002 processMove(mapper, 150, 250);
5003 processSync(mapper);
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5005 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5007 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5008
5009 // down when pressure is non-zero
5010 processPressure(mapper, RAW_PRESSURE_MAX);
5011 processSync(mapper);
5012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5013 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5015 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5016
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5018 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5020 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5021
5022 // up when pressure becomes 0, hover restored
5023 processPressure(mapper, 0);
5024 processSync(mapper);
5025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5026 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5028 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5029
5030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5033 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5034
5035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5036 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5038 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5039
5040 // exit hover when pointer goes away
5041 processUp(mapper);
5042 processSync(mapper);
5043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5044 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5046 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5047}
5048
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049// --- MultiTouchInputMapperTest ---
5050
5051class MultiTouchInputMapperTest : public TouchInputMapperTest {
5052protected:
5053 void prepareAxes(int axes);
5054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005055 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5056 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5057 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5058 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5059 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5060 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5061 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5062 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5063 void processId(MultiTouchInputMapper& mapper, int32_t id);
5064 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5065 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5066 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5067 void processMTSync(MultiTouchInputMapper& mapper);
5068 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069};
5070
5071void MultiTouchInputMapperTest::prepareAxes(int axes) {
5072 if (axes & POSITION) {
5073 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
5074 RAW_X_MIN, RAW_X_MAX, 0, 0);
5075 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
5076 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
5077 }
5078 if (axes & TOUCH) {
5079 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
5080 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
5081 if (axes & MINOR) {
5082 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
5083 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
5084 }
5085 }
5086 if (axes & TOOL) {
5087 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
5088 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
5089 if (axes & MINOR) {
5090 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
5091 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
5092 }
5093 }
5094 if (axes & ORIENTATION) {
5095 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
5096 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
5097 }
5098 if (axes & PRESSURE) {
5099 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
5100 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
5101 }
5102 if (axes & DISTANCE) {
5103 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
5104 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
5105 }
5106 if (axes & ID) {
5107 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
5108 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
5109 }
5110 if (axes & SLOT) {
5111 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
5112 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5113 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
5114 }
5115 if (axes & TOOL_TYPE) {
5116 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
5117 0, MT_TOOL_MAX, 0, 0);
5118 }
5119}
5120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005121void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5122 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005123 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5124 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125}
5126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005127void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5128 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005129 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130}
5131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005132void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5133 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005134 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135}
5136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005137void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005138 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139}
5140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005141void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005142 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143}
5144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005145void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5146 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005147 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148}
5149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005150void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005151 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152}
5153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005154void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005155 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156}
5157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005158void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005159 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005160}
5161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005162void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005163 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164}
5165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005166void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005167 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168}
5169
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005170void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5171 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005172 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173}
5174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005175void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005176 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177}
5178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005179void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005180 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181}
5182
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184 addConfigurationProperty("touch.deviceType", "touchScreen");
5185 prepareDisplay(DISPLAY_ORIENTATION_0);
5186 prepareAxes(POSITION);
5187 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005188 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189
5190 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5191
5192 NotifyMotionArgs motionArgs;
5193
5194 // Two fingers down at once.
5195 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5196 processPosition(mapper, x1, y1);
5197 processMTSync(mapper);
5198 processPosition(mapper, x2, y2);
5199 processMTSync(mapper);
5200 processSync(mapper);
5201
5202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5203 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5204 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5205 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5206 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5208 ASSERT_EQ(0, motionArgs.flags);
5209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5210 ASSERT_EQ(0, motionArgs.buttonState);
5211 ASSERT_EQ(0, motionArgs.edgeFlags);
5212 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5213 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5216 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5217 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5218 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5219 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5220
5221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5222 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5223 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5224 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5225 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5226 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5227 motionArgs.action);
5228 ASSERT_EQ(0, motionArgs.flags);
5229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5230 ASSERT_EQ(0, motionArgs.buttonState);
5231 ASSERT_EQ(0, motionArgs.edgeFlags);
5232 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5235 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5238 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5240 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5241 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5242 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5243 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5244
5245 // Move.
5246 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5247 processPosition(mapper, x1, y1);
5248 processMTSync(mapper);
5249 processPosition(mapper, x2, y2);
5250 processMTSync(mapper);
5251 processSync(mapper);
5252
5253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5254 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5255 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5256 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5257 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5259 ASSERT_EQ(0, motionArgs.flags);
5260 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5261 ASSERT_EQ(0, motionArgs.buttonState);
5262 ASSERT_EQ(0, motionArgs.edgeFlags);
5263 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5264 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5265 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5266 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5269 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5271 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5272 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5273 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5274 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5275
5276 // First finger up.
5277 x2 += 15; y2 -= 20;
5278 processPosition(mapper, x2, y2);
5279 processMTSync(mapper);
5280 processSync(mapper);
5281
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5284 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5285 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5286 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5287 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5288 motionArgs.action);
5289 ASSERT_EQ(0, motionArgs.flags);
5290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5291 ASSERT_EQ(0, motionArgs.buttonState);
5292 ASSERT_EQ(0, motionArgs.edgeFlags);
5293 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5294 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5296 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5299 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5301 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5302 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5303 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5304 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5305
5306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5307 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5308 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5309 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5310 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5312 ASSERT_EQ(0, motionArgs.flags);
5313 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5314 ASSERT_EQ(0, motionArgs.buttonState);
5315 ASSERT_EQ(0, motionArgs.edgeFlags);
5316 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5317 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5320 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5321 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5322 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5324
5325 // Move.
5326 x2 += 20; y2 -= 25;
5327 processPosition(mapper, x2, y2);
5328 processMTSync(mapper);
5329 processSync(mapper);
5330
5331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5333 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5334 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5335 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5337 ASSERT_EQ(0, motionArgs.flags);
5338 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5339 ASSERT_EQ(0, motionArgs.buttonState);
5340 ASSERT_EQ(0, motionArgs.edgeFlags);
5341 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5342 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5345 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5346 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5347 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5349
5350 // New finger down.
5351 int32_t x3 = 700, y3 = 300;
5352 processPosition(mapper, x2, y2);
5353 processMTSync(mapper);
5354 processPosition(mapper, x3, y3);
5355 processMTSync(mapper);
5356 processSync(mapper);
5357
5358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5359 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5360 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5361 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5362 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5363 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5364 motionArgs.action);
5365 ASSERT_EQ(0, motionArgs.flags);
5366 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5367 ASSERT_EQ(0, motionArgs.buttonState);
5368 ASSERT_EQ(0, motionArgs.edgeFlags);
5369 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5370 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5372 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5375 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5377 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5378 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5379 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5381
5382 // Second finger up.
5383 x3 += 30; y3 -= 20;
5384 processPosition(mapper, x3, y3);
5385 processMTSync(mapper);
5386 processSync(mapper);
5387
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5390 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5391 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5392 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5393 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5394 motionArgs.action);
5395 ASSERT_EQ(0, motionArgs.flags);
5396 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5397 ASSERT_EQ(0, motionArgs.buttonState);
5398 ASSERT_EQ(0, motionArgs.edgeFlags);
5399 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5400 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5402 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5405 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5407 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5408 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5409 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5410 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5411
5412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5413 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5414 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5415 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5416 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5418 ASSERT_EQ(0, motionArgs.flags);
5419 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5420 ASSERT_EQ(0, motionArgs.buttonState);
5421 ASSERT_EQ(0, motionArgs.edgeFlags);
5422 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5423 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5426 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5427 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5428 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5430
5431 // Last finger up.
5432 processMTSync(mapper);
5433 processSync(mapper);
5434
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5436 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5437 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5438 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5439 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5440 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5441 ASSERT_EQ(0, motionArgs.flags);
5442 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5443 ASSERT_EQ(0, motionArgs.buttonState);
5444 ASSERT_EQ(0, motionArgs.edgeFlags);
5445 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5446 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5449 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5450 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5451 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5453
5454 // Should not have sent any more keys or motions.
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5457}
5458
5459TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460 addConfigurationProperty("touch.deviceType", "touchScreen");
5461 prepareDisplay(DISPLAY_ORIENTATION_0);
5462 prepareAxes(POSITION | ID);
5463 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005464 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465
5466 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5467
5468 NotifyMotionArgs motionArgs;
5469
5470 // Two fingers down at once.
5471 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5472 processPosition(mapper, x1, y1);
5473 processId(mapper, 1);
5474 processMTSync(mapper);
5475 processPosition(mapper, x2, y2);
5476 processId(mapper, 2);
5477 processMTSync(mapper);
5478 processSync(mapper);
5479
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5481 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5482 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5486 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5487
5488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5489 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5490 motionArgs.action);
5491 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5492 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5494 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5497 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5499 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5500
5501 // Move.
5502 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5503 processPosition(mapper, x1, y1);
5504 processId(mapper, 1);
5505 processMTSync(mapper);
5506 processPosition(mapper, x2, y2);
5507 processId(mapper, 2);
5508 processMTSync(mapper);
5509 processSync(mapper);
5510
5511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5513 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5516 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5519 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5521 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5522
5523 // First finger up.
5524 x2 += 15; y2 -= 20;
5525 processPosition(mapper, x2, y2);
5526 processId(mapper, 2);
5527 processMTSync(mapper);
5528 processSync(mapper);
5529
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5531 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5532 motionArgs.action);
5533 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5534 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5536 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5539 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5541 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5545 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5546 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5549 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5550
5551 // Move.
5552 x2 += 20; y2 -= 25;
5553 processPosition(mapper, x2, y2);
5554 processId(mapper, 2);
5555 processMTSync(mapper);
5556 processSync(mapper);
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5560 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5561 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5564 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5565
5566 // New finger down.
5567 int32_t x3 = 700, y3 = 300;
5568 processPosition(mapper, x2, y2);
5569 processId(mapper, 2);
5570 processMTSync(mapper);
5571 processPosition(mapper, x3, y3);
5572 processId(mapper, 3);
5573 processMTSync(mapper);
5574 processSync(mapper);
5575
5576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5577 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5578 motionArgs.action);
5579 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5580 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5582 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5585 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5587 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5588
5589 // Second finger up.
5590 x3 += 30; y3 -= 20;
5591 processPosition(mapper, x3, y3);
5592 processId(mapper, 3);
5593 processMTSync(mapper);
5594 processSync(mapper);
5595
5596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5597 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5598 motionArgs.action);
5599 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5600 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5602 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5605 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5607 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5611 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5615 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5616
5617 // Last finger up.
5618 processMTSync(mapper);
5619 processSync(mapper);
5620
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5622 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5623 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5624 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5627 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5628
5629 // Should not have sent any more keys or motions.
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5632}
5633
5634TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 addConfigurationProperty("touch.deviceType", "touchScreen");
5636 prepareDisplay(DISPLAY_ORIENTATION_0);
5637 prepareAxes(POSITION | ID | SLOT);
5638 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005639 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640
5641 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5642
5643 NotifyMotionArgs motionArgs;
5644
5645 // Two fingers down at once.
5646 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5647 processPosition(mapper, x1, y1);
5648 processId(mapper, 1);
5649 processSlot(mapper, 1);
5650 processPosition(mapper, x2, y2);
5651 processId(mapper, 2);
5652 processSync(mapper);
5653
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5655 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5656 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5660 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5661
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5663 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5664 motionArgs.action);
5665 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5666 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5668 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5671 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5673 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5674
5675 // Move.
5676 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5677 processSlot(mapper, 0);
5678 processPosition(mapper, x1, y1);
5679 processSlot(mapper, 1);
5680 processPosition(mapper, x2, y2);
5681 processSync(mapper);
5682
5683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5685 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5686 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5688 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5691 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5693 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5694
5695 // First finger up.
5696 x2 += 15; y2 -= 20;
5697 processSlot(mapper, 0);
5698 processId(mapper, -1);
5699 processSlot(mapper, 1);
5700 processPosition(mapper, x2, y2);
5701 processSync(mapper);
5702
5703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5704 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5705 motionArgs.action);
5706 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5707 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5709 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5712 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5714 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5715
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5718 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5719 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5722 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5723
5724 // Move.
5725 x2 += 20; y2 -= 25;
5726 processPosition(mapper, x2, y2);
5727 processSync(mapper);
5728
5729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5731 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5732 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5735 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5736
5737 // New finger down.
5738 int32_t x3 = 700, y3 = 300;
5739 processPosition(mapper, x2, y2);
5740 processSlot(mapper, 0);
5741 processId(mapper, 3);
5742 processPosition(mapper, x3, y3);
5743 processSync(mapper);
5744
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5746 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5747 motionArgs.action);
5748 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5749 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5751 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5754 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5756 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5757
5758 // Second finger up.
5759 x3 += 30; y3 -= 20;
5760 processSlot(mapper, 1);
5761 processId(mapper, -1);
5762 processSlot(mapper, 0);
5763 processPosition(mapper, x3, y3);
5764 processSync(mapper);
5765
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5767 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5768 motionArgs.action);
5769 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5770 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5772 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5775 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5777 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5778
5779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5781 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5782 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5785 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5786
5787 // Last finger up.
5788 processId(mapper, -1);
5789 processSync(mapper);
5790
5791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5792 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5793 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5794 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5797 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5798
5799 // Should not have sent any more keys or motions.
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5802}
5803
5804TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805 addConfigurationProperty("touch.deviceType", "touchScreen");
5806 prepareDisplay(DISPLAY_ORIENTATION_0);
5807 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005808 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005809
5810 // These calculations are based on the input device calibration documentation.
5811 int32_t rawX = 100;
5812 int32_t rawY = 200;
5813 int32_t rawTouchMajor = 7;
5814 int32_t rawTouchMinor = 6;
5815 int32_t rawToolMajor = 9;
5816 int32_t rawToolMinor = 8;
5817 int32_t rawPressure = 11;
5818 int32_t rawDistance = 0;
5819 int32_t rawOrientation = 3;
5820 int32_t id = 5;
5821
5822 float x = toDisplayX(rawX);
5823 float y = toDisplayY(rawY);
5824 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5825 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5826 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5827 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5828 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5829 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5830 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5831 float distance = float(rawDistance);
5832
5833 processPosition(mapper, rawX, rawY);
5834 processTouchMajor(mapper, rawTouchMajor);
5835 processTouchMinor(mapper, rawTouchMinor);
5836 processToolMajor(mapper, rawToolMajor);
5837 processToolMinor(mapper, rawToolMinor);
5838 processPressure(mapper, rawPressure);
5839 processOrientation(mapper, rawOrientation);
5840 processDistance(mapper, rawDistance);
5841 processId(mapper, id);
5842 processMTSync(mapper);
5843 processSync(mapper);
5844
5845 NotifyMotionArgs args;
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5847 ASSERT_EQ(0, args.pointerProperties[0].id);
5848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5849 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5850 orientation, distance));
5851}
5852
5853TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854 addConfigurationProperty("touch.deviceType", "touchScreen");
5855 prepareDisplay(DISPLAY_ORIENTATION_0);
5856 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5857 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005858 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859
5860 // These calculations are based on the input device calibration documentation.
5861 int32_t rawX = 100;
5862 int32_t rawY = 200;
5863 int32_t rawTouchMajor = 140;
5864 int32_t rawTouchMinor = 120;
5865 int32_t rawToolMajor = 180;
5866 int32_t rawToolMinor = 160;
5867
5868 float x = toDisplayX(rawX);
5869 float y = toDisplayY(rawY);
5870 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5871 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5872 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5873 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5874 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5875
5876 processPosition(mapper, rawX, rawY);
5877 processTouchMajor(mapper, rawTouchMajor);
5878 processTouchMinor(mapper, rawTouchMinor);
5879 processToolMajor(mapper, rawToolMajor);
5880 processToolMinor(mapper, rawToolMinor);
5881 processMTSync(mapper);
5882 processSync(mapper);
5883
5884 NotifyMotionArgs args;
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5887 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5888}
5889
5890TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 addConfigurationProperty("touch.deviceType", "touchScreen");
5892 prepareDisplay(DISPLAY_ORIENTATION_0);
5893 prepareAxes(POSITION | TOUCH | TOOL);
5894 addConfigurationProperty("touch.size.calibration", "diameter");
5895 addConfigurationProperty("touch.size.scale", "10");
5896 addConfigurationProperty("touch.size.bias", "160");
5897 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005898 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899
5900 // These calculations are based on the input device calibration documentation.
5901 // Note: We only provide a single common touch/tool value because the device is assumed
5902 // not to emit separate values for each pointer (isSummed = 1).
5903 int32_t rawX = 100;
5904 int32_t rawY = 200;
5905 int32_t rawX2 = 150;
5906 int32_t rawY2 = 250;
5907 int32_t rawTouchMajor = 5;
5908 int32_t rawToolMajor = 8;
5909
5910 float x = toDisplayX(rawX);
5911 float y = toDisplayY(rawY);
5912 float x2 = toDisplayX(rawX2);
5913 float y2 = toDisplayY(rawY2);
5914 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5915 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5916 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5917
5918 processPosition(mapper, rawX, rawY);
5919 processTouchMajor(mapper, rawTouchMajor);
5920 processToolMajor(mapper, rawToolMajor);
5921 processMTSync(mapper);
5922 processPosition(mapper, rawX2, rawY2);
5923 processTouchMajor(mapper, rawTouchMajor);
5924 processToolMajor(mapper, rawToolMajor);
5925 processMTSync(mapper);
5926 processSync(mapper);
5927
5928 NotifyMotionArgs args;
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5931
5932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5933 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5934 args.action);
5935 ASSERT_EQ(size_t(2), args.pointerCount);
5936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5937 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5939 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5940}
5941
5942TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943 addConfigurationProperty("touch.deviceType", "touchScreen");
5944 prepareDisplay(DISPLAY_ORIENTATION_0);
5945 prepareAxes(POSITION | TOUCH | TOOL);
5946 addConfigurationProperty("touch.size.calibration", "area");
5947 addConfigurationProperty("touch.size.scale", "43");
5948 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005949 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950
5951 // These calculations are based on the input device calibration documentation.
5952 int32_t rawX = 100;
5953 int32_t rawY = 200;
5954 int32_t rawTouchMajor = 5;
5955 int32_t rawToolMajor = 8;
5956
5957 float x = toDisplayX(rawX);
5958 float y = toDisplayY(rawY);
5959 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5960 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5961 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5962
5963 processPosition(mapper, rawX, rawY);
5964 processTouchMajor(mapper, rawTouchMajor);
5965 processToolMajor(mapper, rawToolMajor);
5966 processMTSync(mapper);
5967 processSync(mapper);
5968
5969 NotifyMotionArgs args;
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5972 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5973}
5974
5975TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976 addConfigurationProperty("touch.deviceType", "touchScreen");
5977 prepareDisplay(DISPLAY_ORIENTATION_0);
5978 prepareAxes(POSITION | PRESSURE);
5979 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5980 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005981 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982
Michael Wrightaa449c92017-12-13 21:21:43 +00005983 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005984 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00005985 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5986 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5987 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5988
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989 // These calculations are based on the input device calibration documentation.
5990 int32_t rawX = 100;
5991 int32_t rawY = 200;
5992 int32_t rawPressure = 60;
5993
5994 float x = toDisplayX(rawX);
5995 float y = toDisplayY(rawY);
5996 float pressure = float(rawPressure) * 0.01f;
5997
5998 processPosition(mapper, rawX, rawY);
5999 processPressure(mapper, rawPressure);
6000 processMTSync(mapper);
6001 processSync(mapper);
6002
6003 NotifyMotionArgs args;
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6006 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6007}
6008
6009TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010 addConfigurationProperty("touch.deviceType", "touchScreen");
6011 prepareDisplay(DISPLAY_ORIENTATION_0);
6012 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006013 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014
6015 NotifyMotionArgs motionArgs;
6016 NotifyKeyArgs keyArgs;
6017
6018 processId(mapper, 1);
6019 processPosition(mapper, 100, 200);
6020 processSync(mapper);
6021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6022 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6023 ASSERT_EQ(0, motionArgs.buttonState);
6024
6025 // press BTN_LEFT, release BTN_LEFT
6026 processKey(mapper, BTN_LEFT, 1);
6027 processSync(mapper);
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6030 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6031
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6034 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6035
Michael Wrightd02c5b62014-02-10 15:10:22 -08006036 processKey(mapper, BTN_LEFT, 0);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006041
6042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006044 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045
6046 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6047 processKey(mapper, BTN_RIGHT, 1);
6048 processKey(mapper, BTN_MIDDLE, 1);
6049 processSync(mapper);
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6052 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6053 motionArgs.buttonState);
6054
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6057 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6058
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6061 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6062 motionArgs.buttonState);
6063
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 processKey(mapper, BTN_RIGHT, 0);
6065 processSync(mapper);
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006069
6070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073
6074 processKey(mapper, BTN_MIDDLE, 0);
6075 processSync(mapper);
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006079
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006082 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083
6084 // press BTN_BACK, release BTN_BACK
6085 processKey(mapper, BTN_BACK, 1);
6086 processSync(mapper);
6087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6088 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6089 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006090
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006093 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6094
6095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6097 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098
6099 processKey(mapper, BTN_BACK, 0);
6100 processSync(mapper);
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006104
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006107 ASSERT_EQ(0, motionArgs.buttonState);
6108
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6110 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6111 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6112
6113 // press BTN_SIDE, release BTN_SIDE
6114 processKey(mapper, BTN_SIDE, 1);
6115 processSync(mapper);
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6117 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6118 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006119
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006122 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6123
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6126 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127
6128 processKey(mapper, BTN_SIDE, 0);
6129 processSync(mapper);
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006133
6134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006136 ASSERT_EQ(0, motionArgs.buttonState);
6137
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6139 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6140 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6141
6142 // press BTN_FORWARD, release BTN_FORWARD
6143 processKey(mapper, BTN_FORWARD, 1);
6144 processSync(mapper);
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6146 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6147 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006148
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006151 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6152
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6155 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156
6157 processKey(mapper, BTN_FORWARD, 0);
6158 processSync(mapper);
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006162
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006165 ASSERT_EQ(0, motionArgs.buttonState);
6166
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6168 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6169 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6170
6171 // press BTN_EXTRA, release BTN_EXTRA
6172 processKey(mapper, BTN_EXTRA, 1);
6173 processSync(mapper);
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6175 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6176 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006177
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006180 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6181
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6184 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185
6186 processKey(mapper, BTN_EXTRA, 0);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006191
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006194 ASSERT_EQ(0, motionArgs.buttonState);
6195
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6197 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6198 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6199
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6201
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202 // press BTN_STYLUS, release BTN_STYLUS
6203 processKey(mapper, BTN_STYLUS, 1);
6204 processSync(mapper);
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6208
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6211 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212
6213 processKey(mapper, BTN_STYLUS, 0);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006218
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006221 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222
6223 // press BTN_STYLUS2, release BTN_STYLUS2
6224 processKey(mapper, BTN_STYLUS2, 1);
6225 processSync(mapper);
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006228 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6229
6230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6232 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233
6234 processKey(mapper, BTN_STYLUS2, 0);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006239
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006242 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243
6244 // release touch
6245 processId(mapper, -1);
6246 processSync(mapper);
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6249 ASSERT_EQ(0, motionArgs.buttonState);
6250}
6251
6252TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253 addConfigurationProperty("touch.deviceType", "touchScreen");
6254 prepareDisplay(DISPLAY_ORIENTATION_0);
6255 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006256 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257
6258 NotifyMotionArgs motionArgs;
6259
6260 // default tool type is finger
6261 processId(mapper, 1);
6262 processPosition(mapper, 100, 200);
6263 processSync(mapper);
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6267
6268 // eraser
6269 processKey(mapper, BTN_TOOL_RUBBER, 1);
6270 processSync(mapper);
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6274
6275 // stylus
6276 processKey(mapper, BTN_TOOL_RUBBER, 0);
6277 processKey(mapper, BTN_TOOL_PEN, 1);
6278 processSync(mapper);
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6282
6283 // brush
6284 processKey(mapper, BTN_TOOL_PEN, 0);
6285 processKey(mapper, BTN_TOOL_BRUSH, 1);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6290
6291 // pencil
6292 processKey(mapper, BTN_TOOL_BRUSH, 0);
6293 processKey(mapper, BTN_TOOL_PENCIL, 1);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6298
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006299 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 processKey(mapper, BTN_TOOL_PENCIL, 0);
6301 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6302 processSync(mapper);
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6306
6307 // mouse
6308 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6309 processKey(mapper, BTN_TOOL_MOUSE, 1);
6310 processSync(mapper);
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6314
6315 // lens
6316 processKey(mapper, BTN_TOOL_MOUSE, 0);
6317 processKey(mapper, BTN_TOOL_LENS, 1);
6318 processSync(mapper);
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6322
6323 // double-tap
6324 processKey(mapper, BTN_TOOL_LENS, 0);
6325 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6330
6331 // triple-tap
6332 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6333 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6334 processSync(mapper);
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6338
6339 // quad-tap
6340 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6341 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6346
6347 // finger
6348 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6349 processKey(mapper, BTN_TOOL_FINGER, 1);
6350 processSync(mapper);
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6354
6355 // stylus trumps finger
6356 processKey(mapper, BTN_TOOL_PEN, 1);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6361
6362 // eraser trumps stylus
6363 processKey(mapper, BTN_TOOL_RUBBER, 1);
6364 processSync(mapper);
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6368
6369 // mouse trumps eraser
6370 processKey(mapper, BTN_TOOL_MOUSE, 1);
6371 processSync(mapper);
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6375
6376 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6377 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6378 processSync(mapper);
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6382
6383 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6384 processToolType(mapper, MT_TOOL_PEN);
6385 processSync(mapper);
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6389
6390 // back to default tool type
6391 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6392 processKey(mapper, BTN_TOOL_MOUSE, 0);
6393 processKey(mapper, BTN_TOOL_RUBBER, 0);
6394 processKey(mapper, BTN_TOOL_PEN, 0);
6395 processKey(mapper, BTN_TOOL_FINGER, 0);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6400}
6401
6402TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006403 addConfigurationProperty("touch.deviceType", "touchScreen");
6404 prepareDisplay(DISPLAY_ORIENTATION_0);
6405 prepareAxes(POSITION | ID | SLOT);
6406 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006407 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006408
6409 NotifyMotionArgs motionArgs;
6410
6411 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6412 processId(mapper, 1);
6413 processPosition(mapper, 100, 200);
6414 processSync(mapper);
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6418 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6419
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6423 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6424
6425 // move a little
6426 processPosition(mapper, 150, 250);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6431 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6432
6433 // down when BTN_TOUCH is pressed, pressure defaults to 1
6434 processKey(mapper, BTN_TOUCH, 1);
6435 processSync(mapper);
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6439 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6440
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6442 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6444 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6445
6446 // up when BTN_TOUCH is released, hover restored
6447 processKey(mapper, BTN_TOUCH, 0);
6448 processSync(mapper);
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6452 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6453
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6457 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6458
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6460 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6462 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6463
6464 // exit hover when pointer goes away
6465 processId(mapper, -1);
6466 processSync(mapper);
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6470 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6471}
6472
6473TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006474 addConfigurationProperty("touch.deviceType", "touchScreen");
6475 prepareDisplay(DISPLAY_ORIENTATION_0);
6476 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006477 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478
6479 NotifyMotionArgs motionArgs;
6480
6481 // initially hovering because pressure is 0
6482 processId(mapper, 1);
6483 processPosition(mapper, 100, 200);
6484 processPressure(mapper, 0);
6485 processSync(mapper);
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6489 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6490
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6494 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6495
6496 // move a little
6497 processPosition(mapper, 150, 250);
6498 processSync(mapper);
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6500 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6502 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6503
6504 // down when pressure becomes non-zero
6505 processPressure(mapper, RAW_PRESSURE_MAX);
6506 processSync(mapper);
6507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6508 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6510 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6511
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6513 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6515 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6516
6517 // up when pressure becomes 0, hover restored
6518 processPressure(mapper, 0);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6523 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6524
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6528 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6529
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6531 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6533 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6534
6535 // exit hover when pointer goes away
6536 processId(mapper, -1);
6537 processSync(mapper);
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6539 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6541 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6542}
6543
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006544/**
6545 * Set the input device port <--> display port associations, and check that the
6546 * events are routed to the display that matches the display port.
6547 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6548 */
6549TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006550 const std::string usb2 = "USB2";
6551 const uint8_t hdmi1 = 0;
6552 const uint8_t hdmi2 = 1;
6553 const std::string secondaryUniqueId = "uniqueId2";
6554 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6555
6556 addConfigurationProperty("touch.deviceType", "touchScreen");
6557 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006558 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006559
6560 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6561 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6562
6563 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6564 // for this input device is specified, and the matching viewport is not present,
6565 // the input device should be disabled (at the mapper level).
6566
6567 // Add viewport for display 2 on hdmi2
6568 prepareSecondaryDisplay(type, hdmi2);
6569 // Send a touch event
6570 processPosition(mapper, 100, 100);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6573
6574 // Add viewport for display 1 on hdmi1
6575 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6576 // Send a touch event again
6577 processPosition(mapper, 100, 100);
6578 processSync(mapper);
6579
6580 NotifyMotionArgs args;
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6582 ASSERT_EQ(DISPLAY_ID, args.displayId);
6583}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006584
Arthur Hung41a712e2018-11-22 19:41:03 +08006585/**
6586 * Expect fallback to internal viewport if device is external and external viewport is not present.
6587 */
6588TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
Arthur Hung41a712e2018-11-22 19:41:03 +08006589 prepareAxes(POSITION);
6590 addConfigurationProperty("touch.deviceType", "touchScreen");
6591 prepareDisplay(DISPLAY_ORIENTATION_0);
6592 mDevice->setExternal(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006593 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung41a712e2018-11-22 19:41:03 +08006594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006595 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Arthur Hung41a712e2018-11-22 19:41:03 +08006596
6597 NotifyMotionArgs motionArgs;
6598
6599 // Expect the event to be sent to the internal viewport,
6600 // because an external viewport is not present.
6601 processPosition(mapper, 100, 100);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6605
6606 // Expect the event to be sent to the external viewport if it is present.
6607 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6608 processPosition(mapper, 100, 100);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6612}
6613
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006614TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006615 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006616 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006617 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006618 fakePointerController->setPosition(100, 200);
6619 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006620 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6621
Garfield Tan888a6a42020-01-09 11:39:16 -08006622 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6623 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6624
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006625 prepareDisplay(DISPLAY_ORIENTATION_0);
6626 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006627 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006628
6629 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006630 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006631
6632 NotifyMotionArgs motionArgs;
6633 processPosition(mapper, 100, 100);
6634 processSync(mapper);
6635
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6637 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6638 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6639}
6640
Arthur Hung7c645402019-01-25 17:45:42 +08006641TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6642 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006643 prepareAxes(POSITION | ID | SLOT);
6644 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006645 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006646
6647 // Create the second touch screen device, and enable multi fingers.
6648 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006649 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006650 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006651 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006652 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006653 std::unique_ptr<InputDevice> device2 =
6654 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6655 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006656 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6657 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6658 0 /*flat*/, 0 /*fuzz*/);
6659 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6660 0 /*flat*/, 0 /*fuzz*/);
6661 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6662 0 /*flat*/, 0 /*fuzz*/);
6663 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6664 0 /*flat*/, 0 /*fuzz*/);
6665 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6666 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6667 String8("touchScreen"));
6668
6669 // Setup the second touch screen device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006670 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006671 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6672 device2->reset(ARBITRARY_TIME);
6673
6674 // Setup PointerController.
6675 sp<FakePointerController> fakePointerController = new FakePointerController();
6676 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6677 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6678
6679 // Setup policy for associated displays and show touches.
6680 const uint8_t hdmi1 = 0;
6681 const uint8_t hdmi2 = 1;
6682 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6683 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6684 mFakePolicy->setShowTouches(true);
6685
6686 // Create displays.
6687 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6688 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6689
6690 // Default device will reconfigure above, need additional reconfiguration for another device.
6691 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6692 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6693
6694 // Two fingers down at default display.
6695 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6696 processPosition(mapper, x1, y1);
6697 processId(mapper, 1);
6698 processSlot(mapper, 1);
6699 processPosition(mapper, x2, y2);
6700 processId(mapper, 2);
6701 processSync(mapper);
6702
6703 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6704 fakePointerController->getSpots().find(DISPLAY_ID);
6705 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6706 ASSERT_EQ(size_t(2), iter->second.size());
6707
6708 // Two fingers down at second display.
6709 processPosition(mapper2, x1, y1);
6710 processId(mapper2, 1);
6711 processSlot(mapper2, 1);
6712 processPosition(mapper2, x2, y2);
6713 processId(mapper2, 2);
6714 processSync(mapper2);
6715
6716 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6717 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6718 ASSERT_EQ(size_t(2), iter->second.size());
6719}
6720
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006721TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006722 prepareAxes(POSITION);
6723 addConfigurationProperty("touch.deviceType", "touchScreen");
6724 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006725 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006726
6727 NotifyMotionArgs motionArgs;
6728 // Unrotated video frame
6729 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6730 std::vector<TouchVideoFrame> frames{frame};
6731 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6732 processPosition(mapper, 100, 200);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(frames, motionArgs.videoFrames);
6736
6737 // Subsequent touch events should not have any videoframes
6738 // This is implemented separately in FakeEventHub,
6739 // but that should match the behaviour of TouchVideoDevice.
6740 processPosition(mapper, 200, 200);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6744}
6745
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006746TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006747 prepareAxes(POSITION);
6748 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006749 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006750 // Unrotated video frame
6751 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6752 NotifyMotionArgs motionArgs;
6753
6754 // Test all 4 orientations
6755 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6756 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6757 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6758 clearViewports();
6759 prepareDisplay(orientation);
6760 std::vector<TouchVideoFrame> frames{frame};
6761 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6762 processPosition(mapper, 100, 200);
6763 processSync(mapper);
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6765 frames[0].rotate(orientation);
6766 ASSERT_EQ(frames, motionArgs.videoFrames);
6767 }
6768}
6769
6770TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006771 prepareAxes(POSITION);
6772 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006773 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006774 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6775 // so mix these.
6776 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6777 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6778 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6779 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6780 NotifyMotionArgs motionArgs;
6781
6782 prepareDisplay(DISPLAY_ORIENTATION_90);
6783 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6784 processPosition(mapper, 100, 200);
6785 processSync(mapper);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6787 std::for_each(frames.begin(), frames.end(),
6788 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6789 ASSERT_EQ(frames, motionArgs.videoFrames);
6790}
6791
Arthur Hung9da14732019-09-02 16:16:58 +08006792/**
6793 * If we had defined port associations, but the viewport is not ready, the touch device would be
6794 * expected to be disabled, and it should be enabled after the viewport has found.
6795 */
6796TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006797 constexpr uint8_t hdmi2 = 1;
6798 const std::string secondaryUniqueId = "uniqueId2";
6799 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6800
6801 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6802
6803 addConfigurationProperty("touch.deviceType", "touchScreen");
6804 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006805 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006806
6807 ASSERT_EQ(mDevice->isEnabled(), false);
6808
6809 // Add display on hdmi2, the device should be enabled and can receive touch event.
6810 prepareSecondaryDisplay(type, hdmi2);
6811 ASSERT_EQ(mDevice->isEnabled(), true);
6812
6813 // Send a touch event.
6814 processPosition(mapper, 100, 100);
6815 processSync(mapper);
6816
6817 NotifyMotionArgs args;
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6819 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6820}
6821
Arthur Hung6cd19a42019-08-30 19:04:12 +08006822/**
6823 * Test touch should not work if outside of surface.
6824 */
6825TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
Arthur Hung6cd19a42019-08-30 19:04:12 +08006826 addConfigurationProperty("touch.deviceType", "touchScreen");
6827 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006828 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006829 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung6cd19a42019-08-30 19:04:12 +08006830
Arthur Hung05de5772019-09-26 18:31:26 +08006831 // Touch on left-top area should work.
6832 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6833 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6834 processPosition(mapper, rawX, rawY);
6835 processSync(mapper);
6836
6837 NotifyMotionArgs args;
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6839
6840 // Reset.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006841 mapper.reset(ARBITRARY_TIME);
Arthur Hung05de5772019-09-26 18:31:26 +08006842
6843 // Let logical display be different to physical display and rotate 90-degrees.
6844 std::optional<DisplayViewport> internalViewport =
6845 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6846 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6847 internalViewport->logicalLeft = 0;
6848 internalViewport->logicalTop = 0;
6849 internalViewport->logicalRight = DISPLAY_HEIGHT;
6850 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6851
6852 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6853 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6854 internalViewport->physicalRight = DISPLAY_HEIGHT;
6855 internalViewport->physicalBottom = DISPLAY_WIDTH;
6856
6857 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6858 internalViewport->deviceHeight = DISPLAY_WIDTH;
6859 mFakePolicy->updateViewport(internalViewport.value());
6860 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6861
6862 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006863 processPosition(mapper, rawX, rawY);
6864 processSync(mapper);
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6866}
6867
Arthur Hung421eb1c2020-01-16 00:09:42 +08006868TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006869 addConfigurationProperty("touch.deviceType", "touchScreen");
6870 prepareDisplay(DISPLAY_ORIENTATION_0);
6871 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006872 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006873
6874 NotifyMotionArgs motionArgs;
6875
6876 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6877 // finger down
6878 processId(mapper, 1);
6879 processPosition(mapper, x1, y1);
6880 processSync(mapper);
6881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6882 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6884
6885 // finger move
6886 processId(mapper, 1);
6887 processPosition(mapper, x2, y2);
6888 processSync(mapper);
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6892
6893 // finger up.
6894 processId(mapper, -1);
6895 processSync(mapper);
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6897 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6899
6900 // new finger down
6901 processId(mapper, 1);
6902 processPosition(mapper, x3, y3);
6903 processSync(mapper);
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6905 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6907}
6908
6909/**
6910 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6911 * UP events should be ignored.
6912 */
6913TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006914 addConfigurationProperty("touch.deviceType", "touchScreen");
6915 prepareDisplay(DISPLAY_ORIENTATION_0);
6916 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006917 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006918
6919 NotifyMotionArgs motionArgs;
6920
6921 // default tool type is finger
6922 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6923 processId(mapper, 1);
6924 processPosition(mapper, x1, y1);
6925 processSync(mapper);
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6927 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6929
6930 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
6931 processToolType(mapper, MT_TOOL_PALM);
6932 processSync(mapper);
6933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6934 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6935
6936 // Ignore the following MOVE and UP events if had detect a palm event.
6937 processId(mapper, 1);
6938 processPosition(mapper, x2, y2);
6939 processSync(mapper);
6940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6941
6942 // finger up.
6943 processId(mapper, -1);
6944 processSync(mapper);
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6946
6947 // new finger down
6948 processToolType(mapper, MT_TOOL_FINGER);
6949 processId(mapper, 1);
6950 processPosition(mapper, x3, y3);
6951 processSync(mapper);
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6953 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6955}
6956
Michael Wrightd02c5b62014-02-10 15:10:22 -08006957} // namespace android