blob: af11256892d898479b59dde4bc613e8994dbed02 [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. Lewisa7b82e12020-02-12 15:40:45 -08001123 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001124 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125 InputDeviceIdentifier identifier;
1126 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001127 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001129 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 }
1131
Prabir Pradhan28efc192019-11-05 01:10:04 +00001132 // Make the protected loopOnce method accessible to tests.
1133 using InputReader::loopOnce;
1134
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001136 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1137 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001139 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001140 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141 return device;
1142 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001143 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 }
1145
1146 friend class InputReaderTest;
1147};
1148
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001149// --- InputReaderPolicyTest ---
1150class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001151protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001152 sp<FakeInputReaderPolicy> mFakePolicy;
1153
Prabir Pradhan28efc192019-11-05 01:10:04 +00001154 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1155 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001156};
1157
1158/**
1159 * Check that empty set of viewports is an acceptable configuration.
1160 * Also try to get internal viewport two different ways - by type and by uniqueId.
1161 *
1162 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1163 * Such configuration is not currently allowed.
1164 */
1165TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001166 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001167
1168 // We didn't add any viewports yet, so there shouldn't be any.
1169 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001170 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001171 ASSERT_FALSE(internalViewport);
1172
1173 // Add an internal viewport, then clear it
1174 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001175 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001176
1177 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001178 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001180 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181
1182 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001183 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001184 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001185 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001186
1187 mFakePolicy->clearViewports();
1188 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001189 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001190 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192 ASSERT_FALSE(internalViewport);
1193}
1194
1195TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1196 const std::string internalUniqueId = "local:0";
1197 const std::string externalUniqueId = "local:1";
1198 const std::string virtualUniqueId1 = "virtual:2";
1199 const std::string virtualUniqueId2 = "virtual:3";
1200 constexpr int32_t virtualDisplayId1 = 2;
1201 constexpr int32_t virtualDisplayId2 = 3;
1202
1203 // Add an internal viewport
1204 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001205 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001206 // Add an external viewport
1207 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001208 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001209 // Add an virtual viewport
1210 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001211 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001212 // Add another virtual viewport
1213 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001214 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001215
1216 // Check matching by type for internal
1217 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001218 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001219 ASSERT_TRUE(internalViewport);
1220 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1221
1222 // Check matching by type for external
1223 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001224 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001225 ASSERT_TRUE(externalViewport);
1226 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1227
1228 // Check matching by uniqueId for virtual viewport #1
1229 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001230 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001231 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001232 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001233 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1234 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1235
1236 // Check matching by uniqueId for virtual viewport #2
1237 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001238 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001239 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001240 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001241 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1242 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1243}
1244
1245
1246/**
1247 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1248 * that lookup works by checking display id.
1249 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1250 */
1251TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1252 const std::string uniqueId1 = "uniqueId1";
1253 const std::string uniqueId2 = "uniqueId2";
1254 constexpr int32_t displayId1 = 2;
1255 constexpr int32_t displayId2 = 3;
1256
1257 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1258 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1259 for (const ViewportType& type : types) {
1260 mFakePolicy->clearViewports();
1261 // Add a viewport
1262 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001263 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001264 // Add another viewport
1265 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001266 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001267
1268 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001269 std::optional<DisplayViewport> viewport1 =
1270 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001271 ASSERT_TRUE(viewport1);
1272 ASSERT_EQ(displayId1, viewport1->displayId);
1273 ASSERT_EQ(type, viewport1->type);
1274
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001275 std::optional<DisplayViewport> viewport2 =
1276 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001277 ASSERT_TRUE(viewport2);
1278 ASSERT_EQ(displayId2, viewport2->displayId);
1279 ASSERT_EQ(type, viewport2->type);
1280
1281 // When there are multiple viewports of the same kind, and uniqueId is not specified
1282 // in the call to getDisplayViewport, then that situation is not supported.
1283 // The viewports can be stored in any order, so we cannot rely on the order, since that
1284 // is just implementation detail.
1285 // However, we can check that it still returns *a* viewport, we just cannot assert
1286 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001287 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001288 ASSERT_TRUE(someViewport);
1289 }
1290}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001291
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001292/**
1293 * Check getDisplayViewportByPort
1294 */
1295TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1296 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1297 const std::string uniqueId1 = "uniqueId1";
1298 const std::string uniqueId2 = "uniqueId2";
1299 constexpr int32_t displayId1 = 1;
1300 constexpr int32_t displayId2 = 2;
1301 const uint8_t hdmi1 = 0;
1302 const uint8_t hdmi2 = 1;
1303 const uint8_t hdmi3 = 2;
1304
1305 mFakePolicy->clearViewports();
1306 // Add a viewport that's associated with some display port that's not of interest.
1307 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1308 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1309 // Add another viewport, connected to HDMI1 port
1310 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1311 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1312
1313 // Check that correct display viewport was returned by comparing the display ports.
1314 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1315 ASSERT_TRUE(hdmi1Viewport);
1316 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1317 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1318
1319 // Check that we can still get the same viewport using the uniqueId
1320 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1321 ASSERT_TRUE(hdmi1Viewport);
1322 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1323 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1324 ASSERT_EQ(type, hdmi1Viewport->type);
1325
1326 // Check that we cannot find a port with "HDMI2", because we never added one
1327 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1328 ASSERT_FALSE(hdmi2Viewport);
1329}
1330
Michael Wrightd02c5b62014-02-10 15:10:22 -08001331// --- InputReaderTest ---
1332
1333class InputReaderTest : public testing::Test {
1334protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001335 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001336 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001337 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001338 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339
Prabir Pradhan28efc192019-11-05 01:10:04 +00001340 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001341 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001342 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001343 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001344
Prabir Pradhan28efc192019-11-05 01:10:04 +00001345 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1346 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001347 }
1348
Prabir Pradhan28efc192019-11-05 01:10:04 +00001349 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001350 mFakeListener.clear();
1351 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352 }
1353
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001354 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1355 const PropertyMap* configuration) {
1356 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001357
1358 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001359 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001360 }
1361 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001362 mReader->loopOnce();
1363 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001364 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1365 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001366 }
1367
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001368 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001369 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001370 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001371 }
1372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001373 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001374 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001375 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001376 }
1377
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001378 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001379 const std::string& name, uint32_t classes,
1380 uint32_t sources,
1381 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001382 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1383 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001385 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 return mapper;
1387 }
1388};
1389
1390TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001391 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001392 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001393 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001394 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001395
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001396 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001397 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001398 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001399 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001400 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1402 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1403 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1404
1405 // Should also have received a notification describing the new input devices.
1406 inputDevices = mFakePolicy->getInputDevices();
1407 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001408 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001409 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001410 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1411 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1412 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1413}
1414
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001415TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001416 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001417 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001418 constexpr int32_t eventHubId = 1;
1419 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001420 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001421 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001422 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001423 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001424
Yi Kong9b14ac62018-07-17 13:48:38 -07001425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001426
1427 NotifyDeviceResetArgs resetArgs;
1428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001429 ASSERT_EQ(deviceId, resetArgs.deviceId);
1430
1431 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001432 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001433 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001434
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001436 ASSERT_EQ(deviceId, resetArgs.deviceId);
1437 ASSERT_EQ(device->isEnabled(), false);
1438
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001439 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001440 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001443 ASSERT_EQ(device->isEnabled(), false);
1444
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001445 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001446 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001448 ASSERT_EQ(deviceId, resetArgs.deviceId);
1449 ASSERT_EQ(device->isEnabled(), true);
1450}
1451
Michael Wrightd02c5b62014-02-10 15:10:22 -08001452TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001453 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1454 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1455 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001456 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001457 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001458 AINPUT_SOURCE_KEYBOARD, nullptr);
1459 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001460
1461 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1462 AINPUT_SOURCE_ANY, AKEYCODE_A))
1463 << "Should return unknown when the device id is >= 0 but unknown.";
1464
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001465 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1466 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1467 << "Should return unknown when the device id is valid but the sources are not "
1468 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001470 ASSERT_EQ(AKEY_STATE_DOWN,
1471 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1472 AKEYCODE_A))
1473 << "Should return value provided by mapper when device id is valid and the device "
1474 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001475
1476 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1477 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1478 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1479
1480 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1481 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1482 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1483}
1484
1485TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001486 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1487 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1488 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001489 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001490 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001491 AINPUT_SOURCE_KEYBOARD, nullptr);
1492 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001493
1494 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1495 AINPUT_SOURCE_ANY, KEY_A))
1496 << "Should return unknown when the device id is >= 0 but unknown.";
1497
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001498 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1499 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1500 << "Should return unknown when the device id is valid but the sources are not "
1501 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001502
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001503 ASSERT_EQ(AKEY_STATE_DOWN,
1504 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1505 KEY_A))
1506 << "Should return value provided by mapper when device id is valid and the device "
1507 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001508
1509 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1510 AINPUT_SOURCE_TRACKBALL, KEY_A))
1511 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1512
1513 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1514 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1515 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1516}
1517
1518TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1520 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1521 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001522 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001523 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001524 AINPUT_SOURCE_KEYBOARD, nullptr);
1525 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526
1527 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1528 AINPUT_SOURCE_ANY, SW_LID))
1529 << "Should return unknown when the device id is >= 0 but unknown.";
1530
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001531 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1532 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1533 << "Should return unknown when the device id is valid but the sources are not "
1534 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001536 ASSERT_EQ(AKEY_STATE_DOWN,
1537 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1538 SW_LID))
1539 << "Should return value provided by mapper when device id is valid and the device "
1540 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001541
1542 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1543 AINPUT_SOURCE_TRACKBALL, SW_LID))
1544 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1545
1546 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1547 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1548 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1549}
1550
1551TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1553 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1554 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001555 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001556 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001557 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001559 mapper.addSupportedKeyCode(AKEYCODE_A);
1560 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
1562 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1563 uint8_t flags[4] = { 0, 0, 0, 1 };
1564
1565 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1566 << "Should return false when device id is >= 0 but unknown.";
1567 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1568
1569 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001570 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1571 << "Should return false when device id is valid but the sources are not supported by "
1572 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1574
1575 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001576 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1577 keyCodes, flags))
1578 << "Should return value provided by mapper when device id is valid and the device "
1579 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1581
1582 flags[3] = 1;
1583 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1584 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1585 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1586
1587 flags[3] = 1;
1588 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1589 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1590 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1591}
1592
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001593TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001594 constexpr int32_t eventHubId = 1;
1595 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596
1597 NotifyConfigurationChangedArgs args;
1598
1599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1600 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1601}
1602
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001603TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001604 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1605 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1606 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001607 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001608 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001609 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001610
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001611 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001612 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1614
1615 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001616 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001618 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619 ASSERT_EQ(EV_KEY, event.type);
1620 ASSERT_EQ(KEY_A, event.code);
1621 ASSERT_EQ(1, event.value);
1622}
1623
Prabir Pradhan42611e02018-11-27 14:04:02 -08001624TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001626 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 constexpr int32_t eventHubId = 1;
1628 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001629 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001631 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001633
1634 NotifyDeviceResetArgs resetArgs;
1635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1636 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1637
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001638 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001639 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001641 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1642 prevSequenceNum = resetArgs.sequenceNum;
1643
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001644 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001645 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001647 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1648 prevSequenceNum = resetArgs.sequenceNum;
1649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001650 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001651 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001653 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1654 prevSequenceNum = resetArgs.sequenceNum;
1655}
1656
Arthur Hungc23540e2018-11-29 20:42:11 +08001657TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001659 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001660 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001661 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001662 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1663 FakeInputMapper& mapper =
1664 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001665 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001666
1667 const uint8_t hdmi1 = 1;
1668
1669 // Associated touch screen with second display.
1670 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1671
1672 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001673 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001674 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1675 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1676 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1677 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1678 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001679 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001680
1681 // Add the device, and make sure all of the callbacks are triggered.
1682 // The device is added after the input port associations are processed since
1683 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001684 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001687 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001688
Arthur Hung2c9a3342019-07-23 14:18:59 +08001689 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001690 ASSERT_EQ(deviceId, device->getId());
1691 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1692 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001693
1694 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001695 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001696 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001697 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001698}
1699
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001700// --- InputReaderIntegrationTest ---
1701
1702// These tests create and interact with the InputReader only through its interface.
1703// The InputReader is started during SetUp(), which starts its processing in its own
1704// thread. The tests use linux uinput to emulate input devices.
1705// NOTE: Interacting with the physical device while these tests are running may cause
1706// the tests to fail.
1707class InputReaderIntegrationTest : public testing::Test {
1708protected:
1709 sp<TestInputListener> mTestListener;
1710 sp<FakeInputReaderPolicy> mFakePolicy;
1711 sp<InputReaderInterface> mReader;
1712
1713 virtual void SetUp() override {
1714 mFakePolicy = new FakeInputReaderPolicy();
1715 mTestListener = new TestInputListener();
1716
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001717 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001718 ASSERT_EQ(mReader->start(), OK);
1719
1720 // Since this test is run on a real device, all the input devices connected
1721 // to the test device will show up in mReader. We wait for those input devices to
1722 // show up before beginning the tests.
1723 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1724 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1725 }
1726
1727 virtual void TearDown() override {
1728 ASSERT_EQ(mReader->stop(), OK);
1729 mTestListener.clear();
1730 mFakePolicy.clear();
1731 }
1732};
1733
1734TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1735 // An invalid input device that is only used for this test.
1736 class InvalidUinputDevice : public UinputDevice {
1737 public:
1738 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1739
1740 private:
1741 void configureDevice(int fd, uinput_user_dev* device) override {}
1742 };
1743
1744 const size_t numDevices = mFakePolicy->getInputDevices().size();
1745
1746 // UinputDevice does not set any event or key bits, so InputReader should not
1747 // consider it as a valid device.
1748 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1749 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1750 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1751 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1752
1753 invalidDevice.reset();
1754 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1755 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1756 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1757}
1758
1759TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1760 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1761
1762 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1763 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1764 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1765 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1766
1767 // Find the test device by its name.
1768 std::vector<InputDeviceInfo> inputDevices;
1769 mReader->getInputDevices(inputDevices);
1770 InputDeviceInfo* keyboardInfo = nullptr;
1771 const char* keyboardName = keyboard->getName();
1772 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1773 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1774 keyboardInfo = &inputDevices[i];
1775 break;
1776 }
1777 }
1778 ASSERT_NE(keyboardInfo, nullptr);
1779 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1780 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1781 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1782
1783 keyboard.reset();
1784 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1785 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1786 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1787}
1788
1789TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1790 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1791 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1792
1793 NotifyConfigurationChangedArgs configChangedArgs;
1794 ASSERT_NO_FATAL_FAILURE(
1795 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
1796 uint32_t prevSequenceNum = configChangedArgs.sequenceNum;
1797 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1798
1799 NotifyKeyArgs keyArgs;
1800 keyboard->pressAndReleaseHomeKey();
1801 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1802 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
1803 ASSERT_LT(prevSequenceNum, keyArgs.sequenceNum);
1804 prevSequenceNum = keyArgs.sequenceNum;
1805 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1806 prevTimestamp = keyArgs.eventTime;
1807
1808 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1809 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
1810 ASSERT_LT(prevSequenceNum, keyArgs.sequenceNum);
1811 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1812}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001813
1814// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001815class InputDeviceTest : public testing::Test {
1816protected:
1817 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001818 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001819 static const int32_t DEVICE_ID;
1820 static const int32_t DEVICE_GENERATION;
1821 static const int32_t DEVICE_CONTROLLER_NUMBER;
1822 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001825 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001827 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001828 FakeInputReaderContext* mFakeContext;
1829
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001830 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001831
Prabir Pradhan28efc192019-11-05 01:10:04 +00001832 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001833 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001834 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001835 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001836 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1837
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001838 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001839 InputDeviceIdentifier identifier;
1840 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001841 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001842 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1843 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001844 }
1845
Prabir Pradhan28efc192019-11-05 01:10:04 +00001846 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001847 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001848 delete mFakeContext;
1849 mFakeListener.clear();
1850 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851 }
1852};
1853
1854const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001855const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001856const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1858const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1859const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1860 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001861const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001862
1863TEST_F(InputDeviceTest, ImmutableProperties) {
1864 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001865 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001866 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001867}
1868
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001869TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
1870 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001871}
1872
Michael Wrightd02c5b62014-02-10 15:10:22 -08001873TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1874 // Configuration.
1875 InputReaderConfiguration config;
1876 mDevice->configure(ARBITRARY_TIME, &config, 0);
1877
1878 // Reset.
1879 mDevice->reset(ARBITRARY_TIME);
1880
1881 NotifyDeviceResetArgs resetArgs;
1882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1883 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1884 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1885
1886 // Metadata.
1887 ASSERT_TRUE(mDevice->isIgnored());
1888 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1889
1890 InputDeviceInfo info;
1891 mDevice->getDeviceInfo(&info);
1892 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001893 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1895 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1896
1897 // State queries.
1898 ASSERT_EQ(0, mDevice->getMetaState());
1899
1900 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1901 << "Ignored device should return unknown key code state.";
1902 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1903 << "Ignored device should return unknown scan code state.";
1904 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1905 << "Ignored device should return unknown switch state.";
1906
1907 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1908 uint8_t flags[2] = { 0, 1 };
1909 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1910 << "Ignored device should never mark any key codes.";
1911 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1912 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1913}
1914
1915TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1916 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001917 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001919 FakeInputMapper& mapper1 =
1920 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001921 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1922 mapper1.setMetaState(AMETA_ALT_ON);
1923 mapper1.addSupportedKeyCode(AKEYCODE_A);
1924 mapper1.addSupportedKeyCode(AKEYCODE_B);
1925 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1926 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1927 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
1928 mapper1.setScanCodeState(3, AKEY_STATE_UP);
1929 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 FakeInputMapper& mapper2 =
1932 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001933 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001934
1935 InputReaderConfiguration config;
1936 mDevice->configure(ARBITRARY_TIME, &config, 0);
1937
1938 String8 propertyValue;
1939 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1940 << "Device should have read configuration during configuration phase.";
1941 ASSERT_STREQ("value", propertyValue.string());
1942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001943 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
1944 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945
1946 // Reset
1947 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001948 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
1949 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001950
1951 NotifyDeviceResetArgs resetArgs;
1952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1953 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1954 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1955
1956 // Metadata.
1957 ASSERT_FALSE(mDevice->isIgnored());
1958 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1959
1960 InputDeviceInfo info;
1961 mDevice->getDeviceInfo(&info);
1962 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001963 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001964 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1965 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1966
1967 // State queries.
1968 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1969 << "Should query mappers and combine meta states.";
1970
1971 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1972 << "Should return unknown key code state when source not supported.";
1973 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1974 << "Should return unknown scan code state when source not supported.";
1975 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1976 << "Should return unknown switch state when source not supported.";
1977
1978 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1979 << "Should query mapper when source is supported.";
1980 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1981 << "Should query mapper when source is supported.";
1982 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1983 << "Should query mapper when source is supported.";
1984
1985 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1986 uint8_t flags[4] = { 0, 0, 0, 1 };
1987 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1988 << "Should do nothing when source is unsupported.";
1989 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1990 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1991 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1992 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1993
1994 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1995 << "Should query mapper when source is supported.";
1996 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1997 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1998 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1999 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2000
2001 // Event handling.
2002 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002003 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002004 mDevice->process(&event, 1);
2005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002006 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2007 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002008}
2009
Arthur Hung2c9a3342019-07-23 14:18:59 +08002010// A single input device is associated with a specific display. Check that:
2011// 1. Device is disabled if the viewport corresponding to the associated display is not found
2012// 2. Device is disabled when setEnabled API is called
2013TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002014 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002015
2016 // First Configuration.
2017 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2018
2019 // Device should be enabled by default.
2020 ASSERT_TRUE(mDevice->isEnabled());
2021
2022 // Prepare associated info.
2023 constexpr uint8_t hdmi = 1;
2024 const std::string UNIQUE_ID = "local:1";
2025
2026 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2027 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2028 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2029 // Device should be disabled because it is associated with a specific display via
2030 // input port <-> display port association, but the corresponding display is not found
2031 ASSERT_FALSE(mDevice->isEnabled());
2032
2033 // Prepare displays.
2034 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2035 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2036 ViewportType::VIEWPORT_INTERNAL);
2037 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2038 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2039 ASSERT_TRUE(mDevice->isEnabled());
2040
2041 // Device should be disabled after set disable.
2042 mFakePolicy->addDisabledDevice(mDevice->getId());
2043 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2044 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2045 ASSERT_FALSE(mDevice->isEnabled());
2046
2047 // Device should still be disabled even found the associated display.
2048 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2049 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2050 ASSERT_FALSE(mDevice->isEnabled());
2051}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002052
2053// --- InputMapperTest ---
2054
2055class InputMapperTest : public testing::Test {
2056protected:
2057 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002058 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002059 static const int32_t DEVICE_ID;
2060 static const int32_t DEVICE_GENERATION;
2061 static const int32_t DEVICE_CONTROLLER_NUMBER;
2062 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002063 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002064
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002065 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002066 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002067 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002068 FakeInputReaderContext* mFakeContext;
2069 InputDevice* mDevice;
2070
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002071 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002072 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002073 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002074 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002075 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2076 InputDeviceIdentifier identifier;
2077 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002078 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002079 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002080
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002081 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002082 }
2083
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002084 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2085
Prabir Pradhan28efc192019-11-05 01:10:04 +00002086 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002087 delete mDevice;
2088 delete mFakeContext;
2089 mFakeListener.clear();
2090 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002091 }
2092
2093 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002094 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002095 }
2096
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002097 void configureDevice(uint32_t changes) {
2098 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2099 }
2100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002101 template <class T, typename... Args>
2102 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002103 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002104 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002105 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002106 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002107 }
2108
2109 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002110 int32_t orientation, const std::string& uniqueId,
2111 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002112 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002113 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002114 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2115 }
2116
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002117 void clearViewports() {
2118 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002119 }
2120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002121 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2122 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002123 RawEvent event;
2124 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002125 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002126 event.type = type;
2127 event.code = code;
2128 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002129 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002130 }
2131
2132 static void assertMotionRange(const InputDeviceInfo& info,
2133 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2134 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002135 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002136 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2137 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2138 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2139 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2140 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2141 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2142 }
2143
2144 static void assertPointerCoords(const PointerCoords& coords,
2145 float x, float y, float pressure, float size,
2146 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2147 float orientation, float distance) {
2148 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2149 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2150 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2151 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2152 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2153 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2154 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2155 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2156 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2157 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2158 }
2159
2160 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2161 float actualX, actualY;
2162 controller->getPosition(&actualX, &actualY);
2163 ASSERT_NEAR(x, actualX, 1);
2164 ASSERT_NEAR(y, actualY, 1);
2165 }
2166};
2167
2168const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002169const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002170const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002171const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2172const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2173const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002174const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002175
2176// --- SwitchInputMapperTest ---
2177
2178class SwitchInputMapperTest : public InputMapperTest {
2179protected:
2180};
2181
2182TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002183 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002185 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002186}
2187
2188TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002189 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002190
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002191 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002192 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002193
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002194 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002195 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002196}
2197
2198TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002199 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002200
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002201 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2202 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2203 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2204 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002205
2206 NotifySwitchArgs args;
2207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2208 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002209 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2210 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002211 args.switchMask);
2212 ASSERT_EQ(uint32_t(0), args.policyFlags);
2213}
2214
2215
2216// --- KeyboardInputMapperTest ---
2217
2218class KeyboardInputMapperTest : public InputMapperTest {
2219protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002220 const std::string UNIQUE_ID = "local:0";
2221
2222 void prepareDisplay(int32_t orientation);
2223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002224 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002225 int32_t originalKeyCode, int32_t rotatedKeyCode,
2226 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002227};
2228
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002229/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2230 * orientation.
2231 */
2232void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2233 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002234 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002235}
2236
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002237void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002238 int32_t originalScanCode, int32_t originalKeyCode,
2239 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002240 NotifyKeyArgs args;
2241
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002242 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2244 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2245 ASSERT_EQ(originalScanCode, args.scanCode);
2246 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002247 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002248
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002249 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2251 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2252 ASSERT_EQ(originalScanCode, args.scanCode);
2253 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002254 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002255}
2256
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002258 KeyboardInputMapper& mapper =
2259 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2260 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002261
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002262 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002263}
2264
2265TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2266 const int32_t USAGE_A = 0x070004;
2267 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002268 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2269 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002271 KeyboardInputMapper& mapper =
2272 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2273 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274
2275 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002276 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002277 NotifyKeyArgs args;
2278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2279 ASSERT_EQ(DEVICE_ID, args.deviceId);
2280 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2281 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2282 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2283 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2284 ASSERT_EQ(KEY_HOME, args.scanCode);
2285 ASSERT_EQ(AMETA_NONE, args.metaState);
2286 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2287 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2288 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2289
2290 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002291 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 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_HOME, args.keyCode);
2298 ASSERT_EQ(KEY_HOME, 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 by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002305 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2306 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 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(AKEYCODE_A, args.keyCode);
2313 ASSERT_EQ(0, args.scanCode);
2314 ASSERT_EQ(AMETA_NONE, args.metaState);
2315 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2316 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2317 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2318
2319 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002320 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2321 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 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(AKEYCODE_A, args.keyCode);
2328 ASSERT_EQ(0, args.scanCode);
2329 ASSERT_EQ(AMETA_NONE, args.metaState);
2330 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2331 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2332 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2333
2334 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002335 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2336 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2338 ASSERT_EQ(DEVICE_ID, args.deviceId);
2339 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2340 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2341 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2342 ASSERT_EQ(0, args.keyCode);
2343 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2344 ASSERT_EQ(AMETA_NONE, args.metaState);
2345 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2346 ASSERT_EQ(0U, args.policyFlags);
2347 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2348
2349 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002350 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2351 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2353 ASSERT_EQ(DEVICE_ID, args.deviceId);
2354 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2355 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2356 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2357 ASSERT_EQ(0, args.keyCode);
2358 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2359 ASSERT_EQ(AMETA_NONE, args.metaState);
2360 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2361 ASSERT_EQ(0U, args.policyFlags);
2362 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2363}
2364
2365TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002366 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2367 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002369 KeyboardInputMapper& mapper =
2370 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2371 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002372
2373 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002374 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002375
2376 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002377 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378 NotifyKeyArgs args;
2379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2380 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2383
2384 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002385 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2387 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002388 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389
2390 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002391 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2393 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395
2396 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002397 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2399 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002400 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2402}
2403
2404TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002405 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2406 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2407 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2408 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002410 KeyboardInputMapper& mapper =
2411 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2412 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002413
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002414 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2416 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2417 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2418 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2419 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2420 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2421 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2422 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2423}
2424
2425TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002426 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2427 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2428 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2429 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002432 KeyboardInputMapper& mapper =
2433 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2434 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002436 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002437 ASSERT_NO_FATAL_FAILURE(
2438 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2439 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2440 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2441 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2442 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2443 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2444 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002446 clearViewports();
2447 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002448 ASSERT_NO_FATAL_FAILURE(
2449 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2450 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2451 AKEYCODE_DPAD_UP, DISPLAY_ID));
2452 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2453 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2454 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2455 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002457 clearViewports();
2458 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002459 ASSERT_NO_FATAL_FAILURE(
2460 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2461 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2462 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2463 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2464 AKEYCODE_DPAD_UP, DISPLAY_ID));
2465 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2466 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002468 clearViewports();
2469 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002470 ASSERT_NO_FATAL_FAILURE(
2471 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2472 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2473 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2474 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2475 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2476 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2477 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002478
2479 // Special case: if orientation changes while key is down, we still emit the same keycode
2480 // in the key up as we did in the key down.
2481 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002482 clearViewports();
2483 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002484 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2486 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2487 ASSERT_EQ(KEY_UP, args.scanCode);
2488 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2489
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002490 clearViewports();
2491 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002492 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2494 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2495 ASSERT_EQ(KEY_UP, args.scanCode);
2496 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2497}
2498
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002499TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2500 // If the keyboard is not orientation aware,
2501 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002502 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002503
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002504 KeyboardInputMapper& mapper =
2505 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2506 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002507 NotifyKeyArgs args;
2508
2509 // Display id should be ADISPLAY_ID_NONE without any display configuration.
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(ADISPLAY_ID_NONE, args.displayId);
2515
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002516 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002517 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002519 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2521 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2522}
2523
2524TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2525 // If the keyboard is orientation aware,
2526 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002527 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002528
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002529 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002530 KeyboardInputMapper& mapper =
2531 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2532 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002533 NotifyKeyArgs args;
2534
2535 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2536 // ^--- already checked by the previous test
2537
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002538 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002539 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002540 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002542 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2544 ASSERT_EQ(DISPLAY_ID, args.displayId);
2545
2546 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002547 clearViewports();
2548 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002549 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002550 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002552 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2554 ASSERT_EQ(newDisplayId, args.displayId);
2555}
2556
Michael Wrightd02c5b62014-02-10 15:10:22 -08002557TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002558 KeyboardInputMapper& mapper =
2559 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2560 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002562 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002563 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002565 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002566 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567}
2568
2569TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002570 KeyboardInputMapper& mapper =
2571 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2572 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002573
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002574 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002575 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002577 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002578 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579}
2580
2581TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002582 KeyboardInputMapper& mapper =
2583 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2584 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002586 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587
2588 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2589 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002590 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591 ASSERT_TRUE(flags[0]);
2592 ASSERT_FALSE(flags[1]);
2593}
2594
2595TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002596 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2597 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2598 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2599 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2600 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2601 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002603 KeyboardInputMapper& mapper =
2604 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2605 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606
2607 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002608 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2609 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2610 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611
2612 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002613 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2614 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002615 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2616 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2617 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002618 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619
2620 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002621 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2622 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002623 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2624 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2625 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002626 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002627
2628 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002629 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2630 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002631 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2632 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2633 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002634 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635
2636 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002637 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2638 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002639 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2640 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2641 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002642 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643
2644 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002645 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2646 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002647 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2648 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2649 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002650 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651
2652 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002653 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2654 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002655 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2656 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2657 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002658 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002659}
2660
Arthur Hung2c9a3342019-07-23 14:18:59 +08002661TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2662 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002663 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2664 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2665 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002667
2668 // keyboard 2.
2669 const std::string USB2 = "USB2";
2670 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002671 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002672 InputDeviceIdentifier identifier;
2673 identifier.name = "KEYBOARD2";
2674 identifier.location = USB2;
2675 std::unique_ptr<InputDevice> device2 =
2676 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002677 identifier);
2678 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2679 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2680 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2681 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2682 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002684 KeyboardInputMapper& mapper =
2685 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2686 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002687
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002688 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002689 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002690 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002691 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2692 device2->reset(ARBITRARY_TIME);
2693
2694 // Prepared displays and associated info.
2695 constexpr uint8_t hdmi1 = 0;
2696 constexpr uint8_t hdmi2 = 1;
2697 const std::string SECONDARY_UNIQUE_ID = "local:1";
2698
2699 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2700 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2701
2702 // No associated display viewport found, should disable the device.
2703 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2704 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2705 ASSERT_FALSE(device2->isEnabled());
2706
2707 // Prepare second display.
2708 constexpr int32_t newDisplayId = 2;
2709 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2710 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2711 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2712 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2713 // Default device will reconfigure above, need additional reconfiguration for another device.
2714 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2715 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2716
2717 // Device should be enabled after the associated display is found.
2718 ASSERT_TRUE(mDevice->isEnabled());
2719 ASSERT_TRUE(device2->isEnabled());
2720
2721 // Test pad key events
2722 ASSERT_NO_FATAL_FAILURE(
2723 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2724 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2725 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2726 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2727 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2728 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2729 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2730
2731 ASSERT_NO_FATAL_FAILURE(
2732 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2733 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2734 AKEYCODE_DPAD_RIGHT, newDisplayId));
2735 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2736 AKEYCODE_DPAD_DOWN, newDisplayId));
2737 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2738 AKEYCODE_DPAD_LEFT, newDisplayId));
2739}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002740
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002741// --- KeyboardInputMapperTest_ExternalDevice ---
2742
2743class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2744protected:
2745 virtual void SetUp() override {
2746 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2747 }
2748};
2749
2750TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002751 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2752 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002753
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002754 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2756 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2757 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002758
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002759 KeyboardInputMapper& mapper =
2760 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2761 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002762
2763 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2764 NotifyKeyArgs args;
2765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2766 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2767
2768 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2770 ASSERT_EQ(uint32_t(0), args.policyFlags);
2771
2772 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2774 ASSERT_EQ(uint32_t(0), args.policyFlags);
2775
2776 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2778 ASSERT_EQ(uint32_t(0), args.policyFlags);
2779
2780 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2782 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2783
2784 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2786 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2787}
2788
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002789TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002790 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002791
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002792 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2793 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2794 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002795
Powei Fengd041c5d2019-05-03 17:11:33 -07002796 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002797 KeyboardInputMapper& mapper =
2798 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2799 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002800
2801 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2802 NotifyKeyArgs args;
2803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2804 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2805
2806 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2808 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2809
2810 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2812 ASSERT_EQ(uint32_t(0), args.policyFlags);
2813
2814 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2816 ASSERT_EQ(uint32_t(0), args.policyFlags);
2817
2818 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2820 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2821
2822 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2824 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2825}
2826
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827// --- CursorInputMapperTest ---
2828
2829class CursorInputMapperTest : public InputMapperTest {
2830protected:
2831 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2832
2833 sp<FakePointerController> mFakePointerController;
2834
Prabir Pradhan28efc192019-11-05 01:10:04 +00002835 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836 InputMapperTest::SetUp();
2837
2838 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002839 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002840 }
2841
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002842 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
2843 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002844
2845 void prepareDisplay(int32_t orientation) {
2846 const std::string uniqueId = "local:0";
2847 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2848 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2849 orientation, uniqueId, NO_PORT, viewportType);
2850 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851};
2852
2853const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002855void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
2856 int32_t originalY, int32_t rotatedX,
2857 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858 NotifyMotionArgs args;
2859
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002860 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2861 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2862 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2866 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2867 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2868 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2869}
2870
2871TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002873 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002875 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002876}
2877
2878TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002880 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002882 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002883}
2884
2885TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002887 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888
2889 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002890 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002891
2892 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002893 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2894 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002895 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2896 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2897
2898 // When the bounds are set, then there should be a valid motion range.
2899 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2900
2901 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002902 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903
2904 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2905 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2906 1, 800 - 1, 0.0f, 0.0f));
2907 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2908 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2909 2, 480 - 1, 0.0f, 0.0f));
2910 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2911 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2912 0.0f, 1.0f, 0.0f, 0.0f));
2913}
2914
2915TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002917 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918
2919 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002920 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921
2922 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2923 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2924 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2925 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2926 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2927 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2928 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2929 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2930 0.0f, 1.0f, 0.0f, 0.0f));
2931}
2932
2933TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002935 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936
2937 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2938
2939 NotifyMotionArgs args;
2940
2941 // Button press.
2942 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002943 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2944 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2947 ASSERT_EQ(DEVICE_ID, args.deviceId);
2948 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2949 ASSERT_EQ(uint32_t(0), args.policyFlags);
2950 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2951 ASSERT_EQ(0, args.flags);
2952 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2953 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2954 ASSERT_EQ(0, args.edgeFlags);
2955 ASSERT_EQ(uint32_t(1), args.pointerCount);
2956 ASSERT_EQ(0, args.pointerProperties[0].id);
2957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2959 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2960 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2961 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2962 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2963
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2965 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2966 ASSERT_EQ(DEVICE_ID, args.deviceId);
2967 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2968 ASSERT_EQ(uint32_t(0), args.policyFlags);
2969 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2970 ASSERT_EQ(0, args.flags);
2971 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2972 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2973 ASSERT_EQ(0, args.edgeFlags);
2974 ASSERT_EQ(uint32_t(1), args.pointerCount);
2975 ASSERT_EQ(0, args.pointerProperties[0].id);
2976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2978 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2979 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2980 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2981 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2982
Michael Wrightd02c5b62014-02-10 15:10:22 -08002983 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002984 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2985 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2987 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2988 ASSERT_EQ(DEVICE_ID, args.deviceId);
2989 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2990 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2992 ASSERT_EQ(0, args.flags);
2993 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2994 ASSERT_EQ(0, args.buttonState);
2995 ASSERT_EQ(0, args.edgeFlags);
2996 ASSERT_EQ(uint32_t(1), args.pointerCount);
2997 ASSERT_EQ(0, args.pointerProperties[0].id);
2998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3000 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3001 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3002 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3003 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3004
3005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3006 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3007 ASSERT_EQ(DEVICE_ID, args.deviceId);
3008 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3009 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3011 ASSERT_EQ(0, args.flags);
3012 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3013 ASSERT_EQ(0, args.buttonState);
3014 ASSERT_EQ(0, args.edgeFlags);
3015 ASSERT_EQ(uint32_t(1), args.pointerCount);
3016 ASSERT_EQ(0, args.pointerProperties[0].id);
3017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3019 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3020 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3021 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3022 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3023}
3024
3025TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003027 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003028
3029 NotifyMotionArgs args;
3030
3031 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003032 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3033 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3037 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3038
3039 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003040 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3041 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3045 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3046}
3047
3048TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003050 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051
3052 NotifyMotionArgs args;
3053
3054 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003055 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3056 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3058 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3059 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3060 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3061
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3065 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3066
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003068 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3069 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3073 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3074
3075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3078 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3079}
3080
3081TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003083 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084
3085 NotifyMotionArgs args;
3086
3087 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003088 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3089 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3090 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3091 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3093 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3095 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3096 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3097
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3099 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3101 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3102 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3103
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003105 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3106 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3107 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3111 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3112 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3113
3114 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003115 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3116 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3120 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3121
3122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3125 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3126}
3127
3128TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003130 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003132 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3141}
3142
3143TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 addConfigurationProperty("cursor.mode", "navigation");
3145 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003146 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003148 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3157
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003158 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3167
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003168 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3177
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003178 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3187}
3188
3189TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003191 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192
3193 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3194 mFakePointerController->setPosition(100, 200);
3195 mFakePointerController->setButtonState(0);
3196
3197 NotifyMotionArgs motionArgs;
3198 NotifyKeyArgs keyArgs;
3199
3200 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003201 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3202 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3204 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3205 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3206 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3208 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3209
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3212 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3213 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3215 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3216
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003217 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3218 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221 ASSERT_EQ(0, motionArgs.buttonState);
3222 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3224 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3225
3226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003227 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228 ASSERT_EQ(0, motionArgs.buttonState);
3229 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3231 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3232
3233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003235 ASSERT_EQ(0, motionArgs.buttonState);
3236 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3238 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3239
3240 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003241 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3242 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3243 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3245 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3246 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3247 motionArgs.buttonState);
3248 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3249 mFakePointerController->getButtonState());
3250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3251 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3252
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3255 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3256 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3257 mFakePointerController->getButtonState());
3258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3259 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3260
3261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3263 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3264 motionArgs.buttonState);
3265 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3266 mFakePointerController->getButtonState());
3267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3268 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3269
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003270 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3271 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3275 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3277 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3278
3279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003281 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3282 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3284 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3285
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003286 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3287 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3290 ASSERT_EQ(0, motionArgs.buttonState);
3291 ASSERT_EQ(0, mFakePointerController->getButtonState());
3292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3293 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 -08003294 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3295 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003296
3297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298 ASSERT_EQ(0, motionArgs.buttonState);
3299 ASSERT_EQ(0, mFakePointerController->getButtonState());
3300 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3302 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 -08003303
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3305 ASSERT_EQ(0, motionArgs.buttonState);
3306 ASSERT_EQ(0, mFakePointerController->getButtonState());
3307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3309 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3310
3311 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003312 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3313 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3316 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003317
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3321 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3323 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3324
3325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3327 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3328 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3330 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3331
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003332 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3333 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336 ASSERT_EQ(0, motionArgs.buttonState);
3337 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3339 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3340
3341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003343 ASSERT_EQ(0, motionArgs.buttonState);
3344 ASSERT_EQ(0, mFakePointerController->getButtonState());
3345
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3347 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3350 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3351
3352 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003353 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3354 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3356 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3357 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003358
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003360 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3362 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3364 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3365
3366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3368 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3369 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3371 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3372
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003373 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3374 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377 ASSERT_EQ(0, motionArgs.buttonState);
3378 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3380 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 -08003381
3382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3383 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3384 ASSERT_EQ(0, motionArgs.buttonState);
3385 ASSERT_EQ(0, mFakePointerController->getButtonState());
3386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3387 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3388
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3390 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3391 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3392
3393 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003394 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3395 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3397 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3398 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003399
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003401 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3403 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3405 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3406
3407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3409 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3410 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3412 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3413
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003414 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3415 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003417 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418 ASSERT_EQ(0, motionArgs.buttonState);
3419 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3421 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 -08003422
3423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3424 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3425 ASSERT_EQ(0, motionArgs.buttonState);
3426 ASSERT_EQ(0, mFakePointerController->getButtonState());
3427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3428 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3429
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3431 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3432 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3433
3434 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003435 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3436 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3438 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3439 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003440
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003442 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3444 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3446 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3447
3448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3450 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3451 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3453 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3454
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003455 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3456 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459 ASSERT_EQ(0, motionArgs.buttonState);
3460 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3462 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003463
3464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3465 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3466 ASSERT_EQ(0, motionArgs.buttonState);
3467 ASSERT_EQ(0, mFakePointerController->getButtonState());
3468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3469 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3470
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3472 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3473 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3474}
3475
3476TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003478 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479
3480 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3481 mFakePointerController->setPosition(100, 200);
3482 mFakePointerController->setButtonState(0);
3483
3484 NotifyMotionArgs args;
3485
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003486 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3487 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3488 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003490 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3493 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3494 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3495}
3496
3497TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003498 addConfigurationProperty("cursor.mode", "pointer");
3499 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003500 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003501
3502 NotifyDeviceResetArgs resetArgs;
3503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3504 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3505 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3506
3507 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3508 mFakePointerController->setPosition(100, 200);
3509 mFakePointerController->setButtonState(0);
3510
3511 NotifyMotionArgs args;
3512
3513 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003514 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3515 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
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 10.0f, 20.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 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003525 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3526 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3528 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3531 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3533 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3534 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3536 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3537
3538 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003539 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3540 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3542 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3545 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3547 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3548 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3550 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3551
3552 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003553 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3554 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3555 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3557 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3560 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3561 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3562
3563 // Disable pointer capture and check that the device generation got bumped
3564 // and events are generated the usual way.
3565 const uint32_t generation = mFakeContext->getGeneration();
3566 mFakePolicy->setPointerCapture(false);
3567 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3568 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3569
3570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3571 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3572 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3573
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003574 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3575 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3576 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3578 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3581 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3582 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3583}
3584
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003585TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003586 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003587
Garfield Tan888a6a42020-01-09 11:39:16 -08003588 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003589 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003590 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3591 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3592 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3593 ViewportType::VIEWPORT_EXTERNAL);
3594 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3595 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3596
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003597 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3598 mFakePointerController->setPosition(100, 200);
3599 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003600
3601 NotifyMotionArgs args;
3602 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3603 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3604 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3606 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3607 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3609 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3610 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3611 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3612}
3613
Michael Wrightd02c5b62014-02-10 15:10:22 -08003614// --- TouchInputMapperTest ---
3615
3616class TouchInputMapperTest : public InputMapperTest {
3617protected:
3618 static const int32_t RAW_X_MIN;
3619 static const int32_t RAW_X_MAX;
3620 static const int32_t RAW_Y_MIN;
3621 static const int32_t RAW_Y_MAX;
3622 static const int32_t RAW_TOUCH_MIN;
3623 static const int32_t RAW_TOUCH_MAX;
3624 static const int32_t RAW_TOOL_MIN;
3625 static const int32_t RAW_TOOL_MAX;
3626 static const int32_t RAW_PRESSURE_MIN;
3627 static const int32_t RAW_PRESSURE_MAX;
3628 static const int32_t RAW_ORIENTATION_MIN;
3629 static const int32_t RAW_ORIENTATION_MAX;
3630 static const int32_t RAW_DISTANCE_MIN;
3631 static const int32_t RAW_DISTANCE_MAX;
3632 static const int32_t RAW_TILT_MIN;
3633 static const int32_t RAW_TILT_MAX;
3634 static const int32_t RAW_ID_MIN;
3635 static const int32_t RAW_ID_MAX;
3636 static const int32_t RAW_SLOT_MIN;
3637 static const int32_t RAW_SLOT_MAX;
3638 static const float X_PRECISION;
3639 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003640 static const float X_PRECISION_VIRTUAL;
3641 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003642
3643 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003644 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645
3646 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3647
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003648 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003649 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003650
Michael Wrightd02c5b62014-02-10 15:10:22 -08003651 enum Axes {
3652 POSITION = 1 << 0,
3653 TOUCH = 1 << 1,
3654 TOOL = 1 << 2,
3655 PRESSURE = 1 << 3,
3656 ORIENTATION = 1 << 4,
3657 MINOR = 1 << 5,
3658 ID = 1 << 6,
3659 DISTANCE = 1 << 7,
3660 TILT = 1 << 8,
3661 SLOT = 1 << 9,
3662 TOOL_TYPE = 1 << 10,
3663 };
3664
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003665 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3666 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003667 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003669 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670 int32_t toRawX(float displayX);
3671 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003672 float toCookedX(float rawX, float rawY);
3673 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003675 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003677 float toDisplayY(int32_t rawY, int32_t displayHeight);
3678
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679};
3680
3681const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3682const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3683const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3684const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3685const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3686const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3687const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3688const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003689const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3690const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3692const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3693const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3694const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3695const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3696const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3697const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3698const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3699const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3700const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3701const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3702const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003703const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3704 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3705const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3706 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003707const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3708 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709
3710const float TouchInputMapperTest::GEOMETRIC_SCALE =
3711 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3712 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3713
3714const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3715 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3716 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3717};
3718
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003719void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003720 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003721 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3722}
3723
3724void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3725 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3726 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727}
3728
Santos Cordonfa5cf462017-04-05 10:37:00 -07003729void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003730 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3731 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003732 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003733}
3734
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003736 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3737 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3738 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3739 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740}
3741
Jason Gerecke489fda82012-09-07 17:19:40 -07003742void TouchInputMapperTest::prepareLocationCalibration() {
3743 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3744}
3745
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746int32_t TouchInputMapperTest::toRawX(float displayX) {
3747 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3748}
3749
3750int32_t TouchInputMapperTest::toRawY(float displayY) {
3751 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3752}
3753
Jason Gerecke489fda82012-09-07 17:19:40 -07003754float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3755 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3756 return rawX;
3757}
3758
3759float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3760 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3761 return rawY;
3762}
3763
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003765 return toDisplayX(rawX, DISPLAY_WIDTH);
3766}
3767
3768float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3769 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770}
3771
3772float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003773 return toDisplayY(rawY, DISPLAY_HEIGHT);
3774}
3775
3776float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3777 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778}
3779
3780
3781// --- SingleTouchInputMapperTest ---
3782
3783class SingleTouchInputMapperTest : public TouchInputMapperTest {
3784protected:
3785 void prepareButtons();
3786 void prepareAxes(int axes);
3787
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003788 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3789 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3790 void processUp(SingleTouchInputMapper& mappery);
3791 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3792 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3793 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3794 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3795 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3796 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797};
3798
3799void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003800 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801}
3802
3803void SingleTouchInputMapperTest::prepareAxes(int axes) {
3804 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003805 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
3806 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807 }
3808 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003809 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
3810 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811 }
3812 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003813 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
3814 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003815 }
3816 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003817 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
3818 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819 }
3820 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003821 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3822 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823 }
3824}
3825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003826void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003827 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3828 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3829 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830}
3831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003832void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003833 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3834 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835}
3836
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003837void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003838 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839}
3840
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003841void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003842 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843}
3844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003845void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
3846 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003847 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848}
3849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003850void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003851 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852}
3853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003854void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
3855 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003856 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3857 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858}
3859
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003860void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
3861 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003862 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863}
3864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003866 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867}
3868
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 prepareButtons();
3871 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003872 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003874 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003875}
3876
3877TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003878 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
3879 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880 prepareButtons();
3881 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003884 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003885}
3886
3887TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888 prepareButtons();
3889 prepareAxes(POSITION);
3890 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003891 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003893 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003894}
3895
3896TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897 prepareButtons();
3898 prepareAxes(POSITION);
3899 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003900 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003902 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903}
3904
3905TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906 addConfigurationProperty("touch.deviceType", "touchScreen");
3907 prepareDisplay(DISPLAY_ORIENTATION_0);
3908 prepareButtons();
3909 prepareAxes(POSITION);
3910 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003911 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003912
3913 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003914 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003915
3916 // Virtual key is down.
3917 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3918 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3919 processDown(mapper, x, y);
3920 processSync(mapper);
3921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3922
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003923 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924
3925 // Virtual key is up.
3926 processUp(mapper);
3927 processSync(mapper);
3928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3929
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003930 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931}
3932
3933TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003934 addConfigurationProperty("touch.deviceType", "touchScreen");
3935 prepareDisplay(DISPLAY_ORIENTATION_0);
3936 prepareButtons();
3937 prepareAxes(POSITION);
3938 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003939 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940
3941 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003942 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943
3944 // Virtual key is down.
3945 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3946 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3947 processDown(mapper, x, y);
3948 processSync(mapper);
3949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3950
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003951 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003952
3953 // Virtual key is up.
3954 processUp(mapper);
3955 processSync(mapper);
3956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3957
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003958 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959}
3960
3961TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962 addConfigurationProperty("touch.deviceType", "touchScreen");
3963 prepareDisplay(DISPLAY_ORIENTATION_0);
3964 prepareButtons();
3965 prepareAxes(POSITION);
3966 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003967 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968
3969 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3970 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003971 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003972 ASSERT_TRUE(flags[0]);
3973 ASSERT_FALSE(flags[1]);
3974}
3975
3976TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977 addConfigurationProperty("touch.deviceType", "touchScreen");
3978 prepareDisplay(DISPLAY_ORIENTATION_0);
3979 prepareButtons();
3980 prepareAxes(POSITION);
3981 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003982 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983
3984 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3985
3986 NotifyKeyArgs args;
3987
3988 // Press virtual key.
3989 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3990 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3991 processDown(mapper, x, y);
3992 processSync(mapper);
3993
3994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3995 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3996 ASSERT_EQ(DEVICE_ID, args.deviceId);
3997 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3998 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3999 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4000 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4001 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4002 ASSERT_EQ(KEY_HOME, args.scanCode);
4003 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4004 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4005
4006 // Release virtual key.
4007 processUp(mapper);
4008 processSync(mapper);
4009
4010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4011 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4012 ASSERT_EQ(DEVICE_ID, args.deviceId);
4013 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4014 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4015 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4016 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4017 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4018 ASSERT_EQ(KEY_HOME, args.scanCode);
4019 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4020 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4021
4022 // Should not have sent any motions.
4023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4024}
4025
4026TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027 addConfigurationProperty("touch.deviceType", "touchScreen");
4028 prepareDisplay(DISPLAY_ORIENTATION_0);
4029 prepareButtons();
4030 prepareAxes(POSITION);
4031 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004032 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033
4034 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4035
4036 NotifyKeyArgs keyArgs;
4037
4038 // Press virtual key.
4039 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4040 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4041 processDown(mapper, x, y);
4042 processSync(mapper);
4043
4044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4045 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4046 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4047 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4048 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4049 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4050 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4051 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4052 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4053 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4054 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4055
4056 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4057 // into the display area.
4058 y -= 100;
4059 processMove(mapper, x, y);
4060 processSync(mapper);
4061
4062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4063 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4064 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4065 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4066 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4067 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4068 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4069 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4070 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4071 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4072 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4073 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4074
4075 NotifyMotionArgs motionArgs;
4076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4077 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4078 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4079 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4080 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4081 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4082 ASSERT_EQ(0, motionArgs.flags);
4083 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4084 ASSERT_EQ(0, motionArgs.buttonState);
4085 ASSERT_EQ(0, motionArgs.edgeFlags);
4086 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4087 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4088 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4090 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4091 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4092 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4093 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4094
4095 // Keep moving out of bounds. Should generate a pointer move.
4096 y -= 50;
4097 processMove(mapper, x, y);
4098 processSync(mapper);
4099
4100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4102 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4103 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4104 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4106 ASSERT_EQ(0, motionArgs.flags);
4107 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4108 ASSERT_EQ(0, motionArgs.buttonState);
4109 ASSERT_EQ(0, motionArgs.edgeFlags);
4110 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4111 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4114 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4115 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4116 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4117 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4118
4119 // Release out of bounds. Should generate a pointer up.
4120 processUp(mapper);
4121 processSync(mapper);
4122
4123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4124 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4125 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4126 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4127 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4128 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4129 ASSERT_EQ(0, motionArgs.flags);
4130 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4131 ASSERT_EQ(0, motionArgs.buttonState);
4132 ASSERT_EQ(0, motionArgs.edgeFlags);
4133 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4134 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4137 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4138 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4139 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4140 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4141
4142 // Should not have sent any more keys or motions.
4143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4145}
4146
4147TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148 addConfigurationProperty("touch.deviceType", "touchScreen");
4149 prepareDisplay(DISPLAY_ORIENTATION_0);
4150 prepareButtons();
4151 prepareAxes(POSITION);
4152 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004153 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154
4155 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4156
4157 NotifyMotionArgs motionArgs;
4158
4159 // Initially go down out of bounds.
4160 int32_t x = -10;
4161 int32_t y = -10;
4162 processDown(mapper, x, y);
4163 processSync(mapper);
4164
4165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4166
4167 // Move into the display area. Should generate a pointer down.
4168 x = 50;
4169 y = 75;
4170 processMove(mapper, x, y);
4171 processSync(mapper);
4172
4173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4174 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4175 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4176 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4177 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4178 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4179 ASSERT_EQ(0, motionArgs.flags);
4180 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4181 ASSERT_EQ(0, motionArgs.buttonState);
4182 ASSERT_EQ(0, motionArgs.edgeFlags);
4183 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4184 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4187 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4188 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4189 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4190 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4191
4192 // Release. Should generate a pointer up.
4193 processUp(mapper);
4194 processSync(mapper);
4195
4196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4197 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4198 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4199 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4200 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4201 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4202 ASSERT_EQ(0, motionArgs.flags);
4203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4204 ASSERT_EQ(0, motionArgs.buttonState);
4205 ASSERT_EQ(0, motionArgs.edgeFlags);
4206 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4210 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4211 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4212 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4214
4215 // Should not have sent any more keys or motions.
4216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4218}
4219
Santos Cordonfa5cf462017-04-05 10:37:00 -07004220TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004221 addConfigurationProperty("touch.deviceType", "touchScreen");
4222 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4223
4224 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4225 prepareButtons();
4226 prepareAxes(POSITION);
4227 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004228 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004229
4230 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4231
4232 NotifyMotionArgs motionArgs;
4233
4234 // Down.
4235 int32_t x = 100;
4236 int32_t y = 125;
4237 processDown(mapper, x, y);
4238 processSync(mapper);
4239
4240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4241 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4242 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4243 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4244 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4245 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4247 ASSERT_EQ(0, motionArgs.flags);
4248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4249 ASSERT_EQ(0, motionArgs.buttonState);
4250 ASSERT_EQ(0, motionArgs.edgeFlags);
4251 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4252 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4255 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4256 1, 0, 0, 0, 0, 0, 0, 0));
4257 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4258 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4260
4261 // Move.
4262 x += 50;
4263 y += 75;
4264 processMove(mapper, x, y);
4265 processSync(mapper);
4266
4267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4269 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4270 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4274 ASSERT_EQ(0, motionArgs.flags);
4275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4276 ASSERT_EQ(0, motionArgs.buttonState);
4277 ASSERT_EQ(0, motionArgs.edgeFlags);
4278 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4279 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4282 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4283 1, 0, 0, 0, 0, 0, 0, 0));
4284 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4285 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4287
4288 // Up.
4289 processUp(mapper);
4290 processSync(mapper);
4291
4292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4295 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4296 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4297 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4298 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4299 ASSERT_EQ(0, motionArgs.flags);
4300 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4301 ASSERT_EQ(0, motionArgs.buttonState);
4302 ASSERT_EQ(0, motionArgs.edgeFlags);
4303 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4304 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4307 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4308 1, 0, 0, 0, 0, 0, 0, 0));
4309 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4310 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4312
4313 // Should not have sent any more keys or motions.
4314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4316}
4317
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 addConfigurationProperty("touch.deviceType", "touchScreen");
4320 prepareDisplay(DISPLAY_ORIENTATION_0);
4321 prepareButtons();
4322 prepareAxes(POSITION);
4323 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004324 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325
4326 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4327
4328 NotifyMotionArgs motionArgs;
4329
4330 // Down.
4331 int32_t x = 100;
4332 int32_t y = 125;
4333 processDown(mapper, x, y);
4334 processSync(mapper);
4335
4336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4338 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4339 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4340 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4341 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4342 ASSERT_EQ(0, motionArgs.flags);
4343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4344 ASSERT_EQ(0, motionArgs.buttonState);
4345 ASSERT_EQ(0, motionArgs.edgeFlags);
4346 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4347 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4350 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4351 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4352 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4354
4355 // Move.
4356 x += 50;
4357 y += 75;
4358 processMove(mapper, x, y);
4359 processSync(mapper);
4360
4361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4362 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4363 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4364 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4365 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4367 ASSERT_EQ(0, motionArgs.flags);
4368 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4369 ASSERT_EQ(0, motionArgs.buttonState);
4370 ASSERT_EQ(0, motionArgs.edgeFlags);
4371 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4372 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4375 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4376 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4377 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4379
4380 // Up.
4381 processUp(mapper);
4382 processSync(mapper);
4383
4384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4385 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4386 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4387 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4388 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4389 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4390 ASSERT_EQ(0, motionArgs.flags);
4391 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4392 ASSERT_EQ(0, motionArgs.buttonState);
4393 ASSERT_EQ(0, motionArgs.edgeFlags);
4394 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4395 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4398 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4399 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4400 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4401 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4402
4403 // Should not have sent any more keys or motions.
4404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4406}
4407
4408TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 addConfigurationProperty("touch.deviceType", "touchScreen");
4410 prepareButtons();
4411 prepareAxes(POSITION);
4412 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004413 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414
4415 NotifyMotionArgs args;
4416
4417 // Rotation 90.
4418 prepareDisplay(DISPLAY_ORIENTATION_90);
4419 processDown(mapper, toRawX(50), toRawY(75));
4420 processSync(mapper);
4421
4422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4423 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4424 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4425
4426 processUp(mapper);
4427 processSync(mapper);
4428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4429}
4430
4431TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 addConfigurationProperty("touch.deviceType", "touchScreen");
4433 prepareButtons();
4434 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004435 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436
4437 NotifyMotionArgs args;
4438
4439 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004440 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 prepareDisplay(DISPLAY_ORIENTATION_0);
4442 processDown(mapper, toRawX(50), toRawY(75));
4443 processSync(mapper);
4444
4445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4446 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4447 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4448
4449 processUp(mapper);
4450 processSync(mapper);
4451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4452
4453 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004454 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 prepareDisplay(DISPLAY_ORIENTATION_90);
4456 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4457 processSync(mapper);
4458
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4460 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4461 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4462
4463 processUp(mapper);
4464 processSync(mapper);
4465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4466
4467 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004468 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 prepareDisplay(DISPLAY_ORIENTATION_180);
4470 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4471 processSync(mapper);
4472
4473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4474 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4475 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4476
4477 processUp(mapper);
4478 processSync(mapper);
4479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4480
4481 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004482 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 prepareDisplay(DISPLAY_ORIENTATION_270);
4484 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4485 processSync(mapper);
4486
4487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4488 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4489 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4490
4491 processUp(mapper);
4492 processSync(mapper);
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4494}
4495
4496TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 addConfigurationProperty("touch.deviceType", "touchScreen");
4498 prepareDisplay(DISPLAY_ORIENTATION_0);
4499 prepareButtons();
4500 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004501 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502
4503 // These calculations are based on the input device calibration documentation.
4504 int32_t rawX = 100;
4505 int32_t rawY = 200;
4506 int32_t rawPressure = 10;
4507 int32_t rawToolMajor = 12;
4508 int32_t rawDistance = 2;
4509 int32_t rawTiltX = 30;
4510 int32_t rawTiltY = 110;
4511
4512 float x = toDisplayX(rawX);
4513 float y = toDisplayY(rawY);
4514 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4515 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4516 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4517 float distance = float(rawDistance);
4518
4519 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4520 float tiltScale = M_PI / 180;
4521 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4522 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4523 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4524 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4525
4526 processDown(mapper, rawX, rawY);
4527 processPressure(mapper, rawPressure);
4528 processToolMajor(mapper, rawToolMajor);
4529 processDistance(mapper, rawDistance);
4530 processTilt(mapper, rawTiltX, rawTiltY);
4531 processSync(mapper);
4532
4533 NotifyMotionArgs args;
4534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4536 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4537 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4538}
4539
Jason Gerecke489fda82012-09-07 17:19:40 -07004540TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004541 addConfigurationProperty("touch.deviceType", "touchScreen");
4542 prepareDisplay(DISPLAY_ORIENTATION_0);
4543 prepareLocationCalibration();
4544 prepareButtons();
4545 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004546 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004547
4548 int32_t rawX = 100;
4549 int32_t rawY = 200;
4550
4551 float x = toDisplayX(toCookedX(rawX, rawY));
4552 float y = toDisplayY(toCookedY(rawX, rawY));
4553
4554 processDown(mapper, rawX, rawY);
4555 processSync(mapper);
4556
4557 NotifyMotionArgs args;
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4560 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4561}
4562
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564 addConfigurationProperty("touch.deviceType", "touchScreen");
4565 prepareDisplay(DISPLAY_ORIENTATION_0);
4566 prepareButtons();
4567 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004568 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569
4570 NotifyMotionArgs motionArgs;
4571 NotifyKeyArgs keyArgs;
4572
4573 processDown(mapper, 100, 200);
4574 processSync(mapper);
4575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4576 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4577 ASSERT_EQ(0, motionArgs.buttonState);
4578
4579 // press BTN_LEFT, release BTN_LEFT
4580 processKey(mapper, BTN_LEFT, 1);
4581 processSync(mapper);
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4584 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4585
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4587 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4588 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4589
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 processKey(mapper, BTN_LEFT, 0);
4591 processSync(mapper);
4592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004595
4596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004598 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599
4600 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4601 processKey(mapper, BTN_RIGHT, 1);
4602 processKey(mapper, BTN_MIDDLE, 1);
4603 processSync(mapper);
4604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4606 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4607 motionArgs.buttonState);
4608
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4610 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4611 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4612
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4615 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4616 motionArgs.buttonState);
4617
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 processKey(mapper, BTN_RIGHT, 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(AMOTION_EVENT_BUTTON_TERTIARY, 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(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627
4628 processKey(mapper, BTN_MIDDLE, 0);
4629 processSync(mapper);
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004631 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633
4634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004636 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637
4638 // press BTN_BACK, release BTN_BACK
4639 processKey(mapper, BTN_BACK, 1);
4640 processSync(mapper);
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4642 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4643 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004644
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004647 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4648
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4651 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652
4653 processKey(mapper, BTN_BACK, 0);
4654 processSync(mapper);
4655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661 ASSERT_EQ(0, motionArgs.buttonState);
4662
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4664 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4665 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4666
4667 // press BTN_SIDE, release BTN_SIDE
4668 processKey(mapper, BTN_SIDE, 1);
4669 processSync(mapper);
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4671 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4672 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004673
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004676 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4677
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4680 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681
4682 processKey(mapper, BTN_SIDE, 0);
4683 processSync(mapper);
4684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004687
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004690 ASSERT_EQ(0, motionArgs.buttonState);
4691
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4693 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4694 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4695
4696 // press BTN_FORWARD, release BTN_FORWARD
4697 processKey(mapper, BTN_FORWARD, 1);
4698 processSync(mapper);
4699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4700 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4701 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004702
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004705 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4706
4707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4709 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710
4711 processKey(mapper, BTN_FORWARD, 0);
4712 processSync(mapper);
4713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004716
4717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004719 ASSERT_EQ(0, motionArgs.buttonState);
4720
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4722 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4723 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4724
4725 // press BTN_EXTRA, release BTN_EXTRA
4726 processKey(mapper, BTN_EXTRA, 1);
4727 processSync(mapper);
4728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4729 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4730 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004731
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004734 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4735
4736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4737 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4738 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739
4740 processKey(mapper, BTN_EXTRA, 0);
4741 processSync(mapper);
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004743 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004745
4746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004748 ASSERT_EQ(0, motionArgs.buttonState);
4749
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4751 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4752 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4753
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4755
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756 // press BTN_STYLUS, release BTN_STYLUS
4757 processKey(mapper, BTN_STYLUS, 1);
4758 processSync(mapper);
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004761 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4762
4763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4764 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4765 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766
4767 processKey(mapper, BTN_STYLUS, 0);
4768 processSync(mapper);
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004772
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004775 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776
4777 // press BTN_STYLUS2, release BTN_STYLUS2
4778 processKey(mapper, BTN_STYLUS2, 1);
4779 processSync(mapper);
4780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004782 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4783
4784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4785 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4786 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787
4788 processKey(mapper, BTN_STYLUS2, 0);
4789 processSync(mapper);
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004791 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004793
4794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004796 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797
4798 // release touch
4799 processUp(mapper);
4800 processSync(mapper);
4801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4802 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4803 ASSERT_EQ(0, motionArgs.buttonState);
4804}
4805
4806TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807 addConfigurationProperty("touch.deviceType", "touchScreen");
4808 prepareDisplay(DISPLAY_ORIENTATION_0);
4809 prepareButtons();
4810 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004811 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812
4813 NotifyMotionArgs motionArgs;
4814
4815 // default tool type is finger
4816 processDown(mapper, 100, 200);
4817 processSync(mapper);
4818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4819 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4821
4822 // eraser
4823 processKey(mapper, BTN_TOOL_RUBBER, 1);
4824 processSync(mapper);
4825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4828
4829 // stylus
4830 processKey(mapper, BTN_TOOL_RUBBER, 0);
4831 processKey(mapper, BTN_TOOL_PEN, 1);
4832 processSync(mapper);
4833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4836
4837 // brush
4838 processKey(mapper, BTN_TOOL_PEN, 0);
4839 processKey(mapper, BTN_TOOL_BRUSH, 1);
4840 processSync(mapper);
4841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4844
4845 // pencil
4846 processKey(mapper, BTN_TOOL_BRUSH, 0);
4847 processKey(mapper, BTN_TOOL_PENCIL, 1);
4848 processSync(mapper);
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4852
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004853 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 processKey(mapper, BTN_TOOL_PENCIL, 0);
4855 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4856 processSync(mapper);
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4860
4861 // mouse
4862 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4863 processKey(mapper, BTN_TOOL_MOUSE, 1);
4864 processSync(mapper);
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4868
4869 // lens
4870 processKey(mapper, BTN_TOOL_MOUSE, 0);
4871 processKey(mapper, BTN_TOOL_LENS, 1);
4872 processSync(mapper);
4873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4876
4877 // double-tap
4878 processKey(mapper, BTN_TOOL_LENS, 0);
4879 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4880 processSync(mapper);
4881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4884
4885 // triple-tap
4886 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4887 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4888 processSync(mapper);
4889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4892
4893 // quad-tap
4894 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4895 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4896 processSync(mapper);
4897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4900
4901 // finger
4902 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4903 processKey(mapper, BTN_TOOL_FINGER, 1);
4904 processSync(mapper);
4905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4908
4909 // stylus trumps finger
4910 processKey(mapper, BTN_TOOL_PEN, 1);
4911 processSync(mapper);
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4915
4916 // eraser trumps stylus
4917 processKey(mapper, BTN_TOOL_RUBBER, 1);
4918 processSync(mapper);
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4921 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4922
4923 // mouse trumps eraser
4924 processKey(mapper, BTN_TOOL_MOUSE, 1);
4925 processSync(mapper);
4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4929
4930 // back to default tool type
4931 processKey(mapper, BTN_TOOL_MOUSE, 0);
4932 processKey(mapper, BTN_TOOL_RUBBER, 0);
4933 processKey(mapper, BTN_TOOL_PEN, 0);
4934 processKey(mapper, BTN_TOOL_FINGER, 0);
4935 processSync(mapper);
4936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4939}
4940
4941TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942 addConfigurationProperty("touch.deviceType", "touchScreen");
4943 prepareDisplay(DISPLAY_ORIENTATION_0);
4944 prepareButtons();
4945 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004946 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004947 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948
4949 NotifyMotionArgs motionArgs;
4950
4951 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4952 processKey(mapper, BTN_TOOL_FINGER, 1);
4953 processMove(mapper, 100, 200);
4954 processSync(mapper);
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4956 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4958 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4959
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4961 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4963 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4964
4965 // move a little
4966 processMove(mapper, 150, 250);
4967 processSync(mapper);
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4969 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4971 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4972
4973 // down when BTN_TOUCH is pressed, pressure defaults to 1
4974 processKey(mapper, BTN_TOUCH, 1);
4975 processSync(mapper);
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4977 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4979 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4980
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4982 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4984 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4985
4986 // up when BTN_TOUCH is released, hover restored
4987 processKey(mapper, BTN_TOUCH, 0);
4988 processSync(mapper);
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4990 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4992 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4993
4994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4995 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4997 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4998
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5002 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5003
5004 // exit hover when pointer goes away
5005 processKey(mapper, BTN_TOOL_FINGER, 0);
5006 processSync(mapper);
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5008 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5010 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5011}
5012
5013TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 addConfigurationProperty("touch.deviceType", "touchScreen");
5015 prepareDisplay(DISPLAY_ORIENTATION_0);
5016 prepareButtons();
5017 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005018 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019
5020 NotifyMotionArgs motionArgs;
5021
5022 // initially hovering because pressure is 0
5023 processDown(mapper, 100, 200);
5024 processPressure(mapper, 0);
5025 processSync(mapper);
5026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5027 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5029 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5030
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5032 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5034 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5035
5036 // move a little
5037 processMove(mapper, 150, 250);
5038 processSync(mapper);
5039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5040 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5042 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5043
5044 // down when pressure is non-zero
5045 processPressure(mapper, RAW_PRESSURE_MAX);
5046 processSync(mapper);
5047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5050 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5051
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5053 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5055 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5056
5057 // up when pressure becomes 0, hover restored
5058 processPressure(mapper, 0);
5059 processSync(mapper);
5060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5061 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5063 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5064
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5068 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5069
5070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5071 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5073 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5074
5075 // exit hover when pointer goes away
5076 processUp(mapper);
5077 processSync(mapper);
5078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5079 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5081 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5082}
5083
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084// --- MultiTouchInputMapperTest ---
5085
5086class MultiTouchInputMapperTest : public TouchInputMapperTest {
5087protected:
5088 void prepareAxes(int axes);
5089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005090 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5091 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5092 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5093 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5094 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5095 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5096 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5097 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5098 void processId(MultiTouchInputMapper& mapper, int32_t id);
5099 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5100 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5101 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5102 void processMTSync(MultiTouchInputMapper& mapper);
5103 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104};
5105
5106void MultiTouchInputMapperTest::prepareAxes(int axes) {
5107 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005108 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 }
5111 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5113 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005115 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5116 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117 }
5118 }
5119 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005120 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5121 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005123 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5124 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125 }
5126 }
5127 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005128 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5129 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130 }
5131 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005132 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5133 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134 }
5135 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005136 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5137 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138 }
5139 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005140 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5141 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 }
5143 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5145 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146 }
5147 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005148 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149 }
5150}
5151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005152void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5153 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005154 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5155 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156}
5157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005158void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5159 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005160 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161}
5162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005163void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5164 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005165 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166}
5167
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005168void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005169 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170}
5171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005172void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005173 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174}
5175
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005176void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5177 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005178 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005179}
5180
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005181void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005182 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183}
5184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005185void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005186 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005187}
5188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005189void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005190 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191}
5192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005193void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005194 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195}
5196
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005197void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005198 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199}
5200
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005201void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5202 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005203 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204}
5205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005206void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005207 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208}
5209
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005210void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005211 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212}
5213
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215 addConfigurationProperty("touch.deviceType", "touchScreen");
5216 prepareDisplay(DISPLAY_ORIENTATION_0);
5217 prepareAxes(POSITION);
5218 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005219 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220
5221 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5222
5223 NotifyMotionArgs motionArgs;
5224
5225 // Two fingers down at once.
5226 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5227 processPosition(mapper, x1, y1);
5228 processMTSync(mapper);
5229 processPosition(mapper, x2, y2);
5230 processMTSync(mapper);
5231 processSync(mapper);
5232
5233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5234 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5235 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5236 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5237 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5238 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5239 ASSERT_EQ(0, motionArgs.flags);
5240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5241 ASSERT_EQ(0, motionArgs.buttonState);
5242 ASSERT_EQ(0, motionArgs.edgeFlags);
5243 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5244 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5247 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5248 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5249 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5250 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5251
5252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5253 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5254 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5255 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5256 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5257 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5258 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 // Move.
5277 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5278 processPosition(mapper, x1, y1);
5279 processMTSync(mapper);
5280 processPosition(mapper, x2, y2);
5281 processMTSync(mapper);
5282 processSync(mapper);
5283
5284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5286 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5287 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5288 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5290 ASSERT_EQ(0, motionArgs.flags);
5291 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5292 ASSERT_EQ(0, motionArgs.buttonState);
5293 ASSERT_EQ(0, motionArgs.edgeFlags);
5294 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5295 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5297 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5300 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5302 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5303 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5304 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5305 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5306
5307 // First finger up.
5308 x2 += 15; y2 -= 20;
5309 processPosition(mapper, x2, y2);
5310 processMTSync(mapper);
5311 processSync(mapper);
5312
5313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5314 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5315 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5316 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5317 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5318 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5319 motionArgs.action);
5320 ASSERT_EQ(0, motionArgs.flags);
5321 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5322 ASSERT_EQ(0, motionArgs.buttonState);
5323 ASSERT_EQ(0, motionArgs.edgeFlags);
5324 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5325 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5327 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5330 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5332 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5333 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5334 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5335 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5336
5337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5339 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5340 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5341 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5343 ASSERT_EQ(0, motionArgs.flags);
5344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5345 ASSERT_EQ(0, motionArgs.buttonState);
5346 ASSERT_EQ(0, motionArgs.edgeFlags);
5347 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5348 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5351 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5352 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5353 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5354 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5355
5356 // Move.
5357 x2 += 20; y2 -= 25;
5358 processPosition(mapper, x2, y2);
5359 processMTSync(mapper);
5360 processSync(mapper);
5361
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5364 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5365 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5366 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5368 ASSERT_EQ(0, motionArgs.flags);
5369 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5370 ASSERT_EQ(0, motionArgs.buttonState);
5371 ASSERT_EQ(0, motionArgs.edgeFlags);
5372 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5373 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5376 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5377 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5378 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5380
5381 // New finger down.
5382 int32_t x3 = 700, y3 = 300;
5383 processPosition(mapper, x2, y2);
5384 processMTSync(mapper);
5385 processPosition(mapper, x3, y3);
5386 processMTSync(mapper);
5387 processSync(mapper);
5388
5389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5390 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5391 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5392 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5393 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5394 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5395 motionArgs.action);
5396 ASSERT_EQ(0, motionArgs.flags);
5397 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5398 ASSERT_EQ(0, motionArgs.buttonState);
5399 ASSERT_EQ(0, motionArgs.edgeFlags);
5400 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5401 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5403 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5406 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5408 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5409 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5410 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5411 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5412
5413 // Second finger up.
5414 x3 += 30; y3 -= 20;
5415 processPosition(mapper, x3, y3);
5416 processMTSync(mapper);
5417 processSync(mapper);
5418
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5421 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5422 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5423 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5424 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5425 motionArgs.action);
5426 ASSERT_EQ(0, motionArgs.flags);
5427 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5428 ASSERT_EQ(0, motionArgs.buttonState);
5429 ASSERT_EQ(0, motionArgs.edgeFlags);
5430 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5431 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5433 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5436 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5438 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5439 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5440 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5442
5443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5444 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5445 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5446 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5447 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5449 ASSERT_EQ(0, motionArgs.flags);
5450 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5451 ASSERT_EQ(0, motionArgs.buttonState);
5452 ASSERT_EQ(0, motionArgs.edgeFlags);
5453 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5454 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5457 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5458 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5459 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5460 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5461
5462 // Last finger up.
5463 processMTSync(mapper);
5464 processSync(mapper);
5465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5468 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5469 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5470 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5471 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5472 ASSERT_EQ(0, motionArgs.flags);
5473 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5474 ASSERT_EQ(0, motionArgs.buttonState);
5475 ASSERT_EQ(0, motionArgs.edgeFlags);
5476 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5477 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5480 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5481 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5482 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5484
5485 // Should not have sent any more keys or motions.
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5488}
5489
5490TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 addConfigurationProperty("touch.deviceType", "touchScreen");
5492 prepareDisplay(DISPLAY_ORIENTATION_0);
5493 prepareAxes(POSITION | ID);
5494 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005495 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496
5497 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5498
5499 NotifyMotionArgs motionArgs;
5500
5501 // Two fingers down at once.
5502 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
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_DOWN, motionArgs.action);
5513 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5517 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5518
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5520 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5521 motionArgs.action);
5522 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5523 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5525 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5528 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5530 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5531
5532 // Move.
5533 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5534 processPosition(mapper, x1, y1);
5535 processId(mapper, 1);
5536 processMTSync(mapper);
5537 processPosition(mapper, x2, y2);
5538 processId(mapper, 2);
5539 processMTSync(mapper);
5540 processSync(mapper);
5541
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5544 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5545 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5547 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5550 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5552 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5553
5554 // First finger up.
5555 x2 += 15; y2 -= 20;
5556 processPosition(mapper, x2, y2);
5557 processId(mapper, 2);
5558 processMTSync(mapper);
5559 processSync(mapper);
5560
5561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5562 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5563 motionArgs.action);
5564 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5565 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5567 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5570 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5572 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5573
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5576 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5577 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5580 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5581
5582 // Move.
5583 x2 += 20; y2 -= 25;
5584 processPosition(mapper, x2, y2);
5585 processId(mapper, 2);
5586 processMTSync(mapper);
5587 processSync(mapper);
5588
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5591 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5592 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5595 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5596
5597 // New finger down.
5598 int32_t x3 = 700, y3 = 300;
5599 processPosition(mapper, x2, y2);
5600 processId(mapper, 2);
5601 processMTSync(mapper);
5602 processPosition(mapper, x3, y3);
5603 processId(mapper, 3);
5604 processMTSync(mapper);
5605 processSync(mapper);
5606
5607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5608 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5609 motionArgs.action);
5610 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5611 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5613 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5616 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5618 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5619
5620 // Second finger up.
5621 x3 += 30; y3 -= 20;
5622 processPosition(mapper, x3, y3);
5623 processId(mapper, 3);
5624 processMTSync(mapper);
5625 processSync(mapper);
5626
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5628 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5629 motionArgs.action);
5630 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5631 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5633 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5636 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5638 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5639
5640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5642 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5643 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5646 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5647
5648 // Last finger up.
5649 processMTSync(mapper);
5650 processSync(mapper);
5651
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5653 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5654 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5655 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5658 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5659
5660 // Should not have sent any more keys or motions.
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5663}
5664
5665TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666 addConfigurationProperty("touch.deviceType", "touchScreen");
5667 prepareDisplay(DISPLAY_ORIENTATION_0);
5668 prepareAxes(POSITION | ID | SLOT);
5669 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005670 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671
5672 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5673
5674 NotifyMotionArgs motionArgs;
5675
5676 // Two fingers down at once.
5677 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5678 processPosition(mapper, x1, y1);
5679 processId(mapper, 1);
5680 processSlot(mapper, 1);
5681 processPosition(mapper, x2, y2);
5682 processId(mapper, 2);
5683 processSync(mapper);
5684
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5686 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5687 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5688 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5691 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5692
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5694 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5695 motionArgs.action);
5696 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5697 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5699 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5702 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5704 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5705
5706 // Move.
5707 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5708 processSlot(mapper, 0);
5709 processPosition(mapper, x1, y1);
5710 processSlot(mapper, 1);
5711 processPosition(mapper, x2, y2);
5712 processSync(mapper);
5713
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5716 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5717 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5719 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5722 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5724 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5725
5726 // First finger up.
5727 x2 += 15; y2 -= 20;
5728 processSlot(mapper, 0);
5729 processId(mapper, -1);
5730 processSlot(mapper, 1);
5731 processPosition(mapper, x2, y2);
5732 processSync(mapper);
5733
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5735 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5736 motionArgs.action);
5737 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5738 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5740 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5743 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5745 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5746
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5749 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5750 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5753 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5754
5755 // Move.
5756 x2 += 20; y2 -= 25;
5757 processPosition(mapper, x2, y2);
5758 processSync(mapper);
5759
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5762 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5763 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5766 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5767
5768 // New finger down.
5769 int32_t x3 = 700, y3 = 300;
5770 processPosition(mapper, x2, y2);
5771 processSlot(mapper, 0);
5772 processId(mapper, 3);
5773 processPosition(mapper, x3, y3);
5774 processSync(mapper);
5775
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5777 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5778 motionArgs.action);
5779 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5780 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5782 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5785 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5787 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5788
5789 // Second finger up.
5790 x3 += 30; y3 -= 20;
5791 processSlot(mapper, 1);
5792 processId(mapper, -1);
5793 processSlot(mapper, 0);
5794 processPosition(mapper, x3, y3);
5795 processSync(mapper);
5796
5797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5798 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5799 motionArgs.action);
5800 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5801 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5803 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5804 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5806 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5808 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5809
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5812 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5813 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5816 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5817
5818 // Last finger up.
5819 processId(mapper, -1);
5820 processSync(mapper);
5821
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5824 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5825 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5828 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5829
5830 // Should not have sent any more keys or motions.
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5833}
5834
5835TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005836 addConfigurationProperty("touch.deviceType", "touchScreen");
5837 prepareDisplay(DISPLAY_ORIENTATION_0);
5838 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005839 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005840
5841 // These calculations are based on the input device calibration documentation.
5842 int32_t rawX = 100;
5843 int32_t rawY = 200;
5844 int32_t rawTouchMajor = 7;
5845 int32_t rawTouchMinor = 6;
5846 int32_t rawToolMajor = 9;
5847 int32_t rawToolMinor = 8;
5848 int32_t rawPressure = 11;
5849 int32_t rawDistance = 0;
5850 int32_t rawOrientation = 3;
5851 int32_t id = 5;
5852
5853 float x = toDisplayX(rawX);
5854 float y = toDisplayY(rawY);
5855 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5856 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5857 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5858 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5859 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5860 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5861 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5862 float distance = float(rawDistance);
5863
5864 processPosition(mapper, rawX, rawY);
5865 processTouchMajor(mapper, rawTouchMajor);
5866 processTouchMinor(mapper, rawTouchMinor);
5867 processToolMajor(mapper, rawToolMajor);
5868 processToolMinor(mapper, rawToolMinor);
5869 processPressure(mapper, rawPressure);
5870 processOrientation(mapper, rawOrientation);
5871 processDistance(mapper, rawDistance);
5872 processId(mapper, id);
5873 processMTSync(mapper);
5874 processSync(mapper);
5875
5876 NotifyMotionArgs args;
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5878 ASSERT_EQ(0, args.pointerProperties[0].id);
5879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5880 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5881 orientation, distance));
5882}
5883
5884TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885 addConfigurationProperty("touch.deviceType", "touchScreen");
5886 prepareDisplay(DISPLAY_ORIENTATION_0);
5887 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5888 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005889 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890
5891 // These calculations are based on the input device calibration documentation.
5892 int32_t rawX = 100;
5893 int32_t rawY = 200;
5894 int32_t rawTouchMajor = 140;
5895 int32_t rawTouchMinor = 120;
5896 int32_t rawToolMajor = 180;
5897 int32_t rawToolMinor = 160;
5898
5899 float x = toDisplayX(rawX);
5900 float y = toDisplayY(rawY);
5901 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5902 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5903 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5904 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5905 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5906
5907 processPosition(mapper, rawX, rawY);
5908 processTouchMajor(mapper, rawTouchMajor);
5909 processTouchMinor(mapper, rawTouchMinor);
5910 processToolMajor(mapper, rawToolMajor);
5911 processToolMinor(mapper, rawToolMinor);
5912 processMTSync(mapper);
5913 processSync(mapper);
5914
5915 NotifyMotionArgs args;
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5918 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5919}
5920
5921TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922 addConfigurationProperty("touch.deviceType", "touchScreen");
5923 prepareDisplay(DISPLAY_ORIENTATION_0);
5924 prepareAxes(POSITION | TOUCH | TOOL);
5925 addConfigurationProperty("touch.size.calibration", "diameter");
5926 addConfigurationProperty("touch.size.scale", "10");
5927 addConfigurationProperty("touch.size.bias", "160");
5928 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005929 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930
5931 // These calculations are based on the input device calibration documentation.
5932 // Note: We only provide a single common touch/tool value because the device is assumed
5933 // not to emit separate values for each pointer (isSummed = 1).
5934 int32_t rawX = 100;
5935 int32_t rawY = 200;
5936 int32_t rawX2 = 150;
5937 int32_t rawY2 = 250;
5938 int32_t rawTouchMajor = 5;
5939 int32_t rawToolMajor = 8;
5940
5941 float x = toDisplayX(rawX);
5942 float y = toDisplayY(rawY);
5943 float x2 = toDisplayX(rawX2);
5944 float y2 = toDisplayY(rawY2);
5945 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5946 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5947 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5948
5949 processPosition(mapper, rawX, rawY);
5950 processTouchMajor(mapper, rawTouchMajor);
5951 processToolMajor(mapper, rawToolMajor);
5952 processMTSync(mapper);
5953 processPosition(mapper, rawX2, rawY2);
5954 processTouchMajor(mapper, rawTouchMajor);
5955 processToolMajor(mapper, rawToolMajor);
5956 processMTSync(mapper);
5957 processSync(mapper);
5958
5959 NotifyMotionArgs args;
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5961 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5962
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5964 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5965 args.action);
5966 ASSERT_EQ(size_t(2), args.pointerCount);
5967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5968 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5970 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5971}
5972
5973TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974 addConfigurationProperty("touch.deviceType", "touchScreen");
5975 prepareDisplay(DISPLAY_ORIENTATION_0);
5976 prepareAxes(POSITION | TOUCH | TOOL);
5977 addConfigurationProperty("touch.size.calibration", "area");
5978 addConfigurationProperty("touch.size.scale", "43");
5979 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005980 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981
5982 // These calculations are based on the input device calibration documentation.
5983 int32_t rawX = 100;
5984 int32_t rawY = 200;
5985 int32_t rawTouchMajor = 5;
5986 int32_t rawToolMajor = 8;
5987
5988 float x = toDisplayX(rawX);
5989 float y = toDisplayY(rawY);
5990 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5991 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5992 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5993
5994 processPosition(mapper, rawX, rawY);
5995 processTouchMajor(mapper, rawTouchMajor);
5996 processToolMajor(mapper, rawToolMajor);
5997 processMTSync(mapper);
5998 processSync(mapper);
5999
6000 NotifyMotionArgs args;
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6003 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6004}
6005
6006TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007 addConfigurationProperty("touch.deviceType", "touchScreen");
6008 prepareDisplay(DISPLAY_ORIENTATION_0);
6009 prepareAxes(POSITION | PRESSURE);
6010 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6011 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006012 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013
Michael Wrightaa449c92017-12-13 21:21:43 +00006014 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006015 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006016 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6017 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6018 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6019
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020 // These calculations are based on the input device calibration documentation.
6021 int32_t rawX = 100;
6022 int32_t rawY = 200;
6023 int32_t rawPressure = 60;
6024
6025 float x = toDisplayX(rawX);
6026 float y = toDisplayY(rawY);
6027 float pressure = float(rawPressure) * 0.01f;
6028
6029 processPosition(mapper, rawX, rawY);
6030 processPressure(mapper, rawPressure);
6031 processMTSync(mapper);
6032 processSync(mapper);
6033
6034 NotifyMotionArgs args;
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6037 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6038}
6039
6040TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041 addConfigurationProperty("touch.deviceType", "touchScreen");
6042 prepareDisplay(DISPLAY_ORIENTATION_0);
6043 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006044 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045
6046 NotifyMotionArgs motionArgs;
6047 NotifyKeyArgs keyArgs;
6048
6049 processId(mapper, 1);
6050 processPosition(mapper, 100, 200);
6051 processSync(mapper);
6052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6053 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6054 ASSERT_EQ(0, motionArgs.buttonState);
6055
6056 // press BTN_LEFT, release BTN_LEFT
6057 processKey(mapper, BTN_LEFT, 1);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6061 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6062
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6065 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6066
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067 processKey(mapper, BTN_LEFT, 0);
6068 processSync(mapper);
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006070 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072
6073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006075 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076
6077 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6078 processKey(mapper, BTN_RIGHT, 1);
6079 processKey(mapper, BTN_MIDDLE, 1);
6080 processSync(mapper);
6081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6083 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6084 motionArgs.buttonState);
6085
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6087 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6088 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6089
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6091 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6092 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6093 motionArgs.buttonState);
6094
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095 processKey(mapper, BTN_RIGHT, 0);
6096 processSync(mapper);
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006100
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006103 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104
6105 processKey(mapper, BTN_MIDDLE, 0);
6106 processSync(mapper);
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006108 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006110
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006113 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114
6115 // press BTN_BACK, release BTN_BACK
6116 processKey(mapper, BTN_BACK, 1);
6117 processSync(mapper);
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6119 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6120 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006121
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006124 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6125
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6128 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129
6130 processKey(mapper, BTN_BACK, 0);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006135
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006138 ASSERT_EQ(0, motionArgs.buttonState);
6139
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6141 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6142 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6143
6144 // press BTN_SIDE, release BTN_SIDE
6145 processKey(mapper, BTN_SIDE, 1);
6146 processSync(mapper);
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6148 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6149 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006150
Michael Wrightd02c5b62014-02-10 15:10:22 -08006151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006153 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6154
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6157 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158
6159 processKey(mapper, BTN_SIDE, 0);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006167 ASSERT_EQ(0, motionArgs.buttonState);
6168
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6170 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6171 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6172
6173 // press BTN_FORWARD, release BTN_FORWARD
6174 processKey(mapper, BTN_FORWARD, 1);
6175 processSync(mapper);
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6177 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6178 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006179
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006182 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6183
6184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6186 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187
6188 processKey(mapper, BTN_FORWARD, 0);
6189 processSync(mapper);
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006191 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006193
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006196 ASSERT_EQ(0, motionArgs.buttonState);
6197
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6199 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6200 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6201
6202 // press BTN_EXTRA, release BTN_EXTRA
6203 processKey(mapper, BTN_EXTRA, 1);
6204 processSync(mapper);
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6206 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6207 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006208
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006211 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6212
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6215 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216
6217 processKey(mapper, BTN_EXTRA, 0);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006222
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006225 ASSERT_EQ(0, motionArgs.buttonState);
6226
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6228 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6229 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6230
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6232
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 // press BTN_STYLUS, release BTN_STYLUS
6234 processKey(mapper, BTN_STYLUS, 1);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006238 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6239
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6242 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243
6244 processKey(mapper, BTN_STYLUS, 0);
6245 processSync(mapper);
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006249
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006252 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253
6254 // press BTN_STYLUS2, release BTN_STYLUS2
6255 processKey(mapper, BTN_STYLUS2, 1);
6256 processSync(mapper);
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6260
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6263 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264
6265 processKey(mapper, BTN_STYLUS2, 0);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006269 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006270
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006273 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274
6275 // release touch
6276 processId(mapper, -1);
6277 processSync(mapper);
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6279 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6280 ASSERT_EQ(0, motionArgs.buttonState);
6281}
6282
6283TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284 addConfigurationProperty("touch.deviceType", "touchScreen");
6285 prepareDisplay(DISPLAY_ORIENTATION_0);
6286 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006287 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288
6289 NotifyMotionArgs motionArgs;
6290
6291 // default tool type is finger
6292 processId(mapper, 1);
6293 processPosition(mapper, 100, 200);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6298
6299 // eraser
6300 processKey(mapper, BTN_TOOL_RUBBER, 1);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6305
6306 // stylus
6307 processKey(mapper, BTN_TOOL_RUBBER, 0);
6308 processKey(mapper, BTN_TOOL_PEN, 1);
6309 processSync(mapper);
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6313
6314 // brush
6315 processKey(mapper, BTN_TOOL_PEN, 0);
6316 processKey(mapper, BTN_TOOL_BRUSH, 1);
6317 processSync(mapper);
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6321
6322 // pencil
6323 processKey(mapper, BTN_TOOL_BRUSH, 0);
6324 processKey(mapper, BTN_TOOL_PENCIL, 1);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6329
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006330 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 processKey(mapper, BTN_TOOL_PENCIL, 0);
6332 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6337
6338 // mouse
6339 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6340 processKey(mapper, BTN_TOOL_MOUSE, 1);
6341 processSync(mapper);
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6345
6346 // lens
6347 processKey(mapper, BTN_TOOL_MOUSE, 0);
6348 processKey(mapper, BTN_TOOL_LENS, 1);
6349 processSync(mapper);
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6353
6354 // double-tap
6355 processKey(mapper, BTN_TOOL_LENS, 0);
6356 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6361
6362 // triple-tap
6363 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6364 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6365 processSync(mapper);
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6369
6370 // quad-tap
6371 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6372 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6377
6378 // finger
6379 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6380 processKey(mapper, BTN_TOOL_FINGER, 1);
6381 processSync(mapper);
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6385
6386 // stylus trumps finger
6387 processKey(mapper, BTN_TOOL_PEN, 1);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6392
6393 // eraser trumps stylus
6394 processKey(mapper, BTN_TOOL_RUBBER, 1);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6399
6400 // mouse trumps eraser
6401 processKey(mapper, BTN_TOOL_MOUSE, 1);
6402 processSync(mapper);
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6406
6407 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6408 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6413
6414 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6415 processToolType(mapper, MT_TOOL_PEN);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6420
6421 // back to default tool type
6422 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6423 processKey(mapper, BTN_TOOL_MOUSE, 0);
6424 processKey(mapper, BTN_TOOL_RUBBER, 0);
6425 processKey(mapper, BTN_TOOL_PEN, 0);
6426 processKey(mapper, BTN_TOOL_FINGER, 0);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6431}
6432
6433TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434 addConfigurationProperty("touch.deviceType", "touchScreen");
6435 prepareDisplay(DISPLAY_ORIENTATION_0);
6436 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006437 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006438 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439
6440 NotifyMotionArgs motionArgs;
6441
6442 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6443 processId(mapper, 1);
6444 processPosition(mapper, 100, 200);
6445 processSync(mapper);
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6447 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6449 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6450
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6452 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6454 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6455
6456 // move a little
6457 processPosition(mapper, 150, 250);
6458 processSync(mapper);
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 // down when BTN_TOUCH is pressed, pressure defaults to 1
6465 processKey(mapper, BTN_TOUCH, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6475 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6476
6477 // up when BTN_TOUCH is released, hover restored
6478 processKey(mapper, BTN_TOUCH, 0);
6479 processSync(mapper);
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6481 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6483 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6484
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6488 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6489
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6493 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6494
6495 // exit hover when pointer goes away
6496 processId(mapper, -1);
6497 processSync(mapper);
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6501 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6502}
6503
6504TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006505 addConfigurationProperty("touch.deviceType", "touchScreen");
6506 prepareDisplay(DISPLAY_ORIENTATION_0);
6507 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006508 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006509
6510 NotifyMotionArgs motionArgs;
6511
6512 // initially hovering because pressure is 0
6513 processId(mapper, 1);
6514 processPosition(mapper, 100, 200);
6515 processPressure(mapper, 0);
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6520 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6521
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6525 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6526
6527 // move a little
6528 processPosition(mapper, 150, 250);
6529 processSync(mapper);
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 // down when pressure becomes non-zero
6536 processPressure(mapper, RAW_PRESSURE_MAX);
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6546 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6547
6548 // up when pressure becomes 0, hover restored
6549 processPressure(mapper, 0);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6554 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6555
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6559 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6560
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6564 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6565
6566 // exit hover when pointer goes away
6567 processId(mapper, -1);
6568 processSync(mapper);
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6572 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6573}
6574
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006575/**
6576 * Set the input device port <--> display port associations, and check that the
6577 * events are routed to the display that matches the display port.
6578 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6579 */
6580TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006581 const std::string usb2 = "USB2";
6582 const uint8_t hdmi1 = 0;
6583 const uint8_t hdmi2 = 1;
6584 const std::string secondaryUniqueId = "uniqueId2";
6585 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6586
6587 addConfigurationProperty("touch.deviceType", "touchScreen");
6588 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006589 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006590
6591 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6592 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6593
6594 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6595 // for this input device is specified, and the matching viewport is not present,
6596 // the input device should be disabled (at the mapper level).
6597
6598 // Add viewport for display 2 on hdmi2
6599 prepareSecondaryDisplay(type, hdmi2);
6600 // Send a touch event
6601 processPosition(mapper, 100, 100);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6604
6605 // Add viewport for display 1 on hdmi1
6606 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6607 // Send a touch event again
6608 processPosition(mapper, 100, 100);
6609 processSync(mapper);
6610
6611 NotifyMotionArgs args;
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6613 ASSERT_EQ(DISPLAY_ID, args.displayId);
6614}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006615
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006616TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006617 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006618 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006619 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006620 fakePointerController->setPosition(100, 200);
6621 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006622 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6623
Garfield Tan888a6a42020-01-09 11:39:16 -08006624 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6625 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6626
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006627 prepareDisplay(DISPLAY_ORIENTATION_0);
6628 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006629 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006630
6631 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006632 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006633
6634 NotifyMotionArgs motionArgs;
6635 processPosition(mapper, 100, 100);
6636 processSync(mapper);
6637
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6640 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6641}
6642
Arthur Hung7c645402019-01-25 17:45:42 +08006643TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6644 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006645 prepareAxes(POSITION | ID | SLOT);
6646 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006647 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006648
6649 // Create the second touch screen device, and enable multi fingers.
6650 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006651 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006652 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006653 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006654 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006655 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006656 std::unique_ptr<InputDevice> device2 =
6657 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006658 identifier);
6659 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6660 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6661 0 /*flat*/, 0 /*fuzz*/);
6662 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6663 0 /*flat*/, 0 /*fuzz*/);
6664 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6665 0 /*flat*/, 0 /*fuzz*/);
6666 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6667 0 /*flat*/, 0 /*fuzz*/);
6668 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6669 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6670 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006671
6672 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006673 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006674 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6675 device2->reset(ARBITRARY_TIME);
6676
6677 // Setup PointerController.
6678 sp<FakePointerController> fakePointerController = new FakePointerController();
6679 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6680 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6681
6682 // Setup policy for associated displays and show touches.
6683 const uint8_t hdmi1 = 0;
6684 const uint8_t hdmi2 = 1;
6685 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6686 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6687 mFakePolicy->setShowTouches(true);
6688
6689 // Create displays.
6690 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6691 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6692
6693 // Default device will reconfigure above, need additional reconfiguration for another device.
6694 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6695 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6696
6697 // Two fingers down at default display.
6698 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6699 processPosition(mapper, x1, y1);
6700 processId(mapper, 1);
6701 processSlot(mapper, 1);
6702 processPosition(mapper, x2, y2);
6703 processId(mapper, 2);
6704 processSync(mapper);
6705
6706 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6707 fakePointerController->getSpots().find(DISPLAY_ID);
6708 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6709 ASSERT_EQ(size_t(2), iter->second.size());
6710
6711 // Two fingers down at second display.
6712 processPosition(mapper2, x1, y1);
6713 processId(mapper2, 1);
6714 processSlot(mapper2, 1);
6715 processPosition(mapper2, x2, y2);
6716 processId(mapper2, 2);
6717 processSync(mapper2);
6718
6719 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6720 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6721 ASSERT_EQ(size_t(2), iter->second.size());
6722}
6723
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006724TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006725 prepareAxes(POSITION);
6726 addConfigurationProperty("touch.deviceType", "touchScreen");
6727 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006728 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006729
6730 NotifyMotionArgs motionArgs;
6731 // Unrotated video frame
6732 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6733 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006734 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006735 processPosition(mapper, 100, 200);
6736 processSync(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6738 ASSERT_EQ(frames, motionArgs.videoFrames);
6739
6740 // Subsequent touch events should not have any videoframes
6741 // This is implemented separately in FakeEventHub,
6742 // but that should match the behaviour of TouchVideoDevice.
6743 processPosition(mapper, 200, 200);
6744 processSync(mapper);
6745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6746 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6747}
6748
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006749TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006750 prepareAxes(POSITION);
6751 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006752 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006753 // Unrotated video frame
6754 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6755 NotifyMotionArgs motionArgs;
6756
6757 // Test all 4 orientations
6758 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6759 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6760 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6761 clearViewports();
6762 prepareDisplay(orientation);
6763 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006764 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006765 processPosition(mapper, 100, 200);
6766 processSync(mapper);
6767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6768 frames[0].rotate(orientation);
6769 ASSERT_EQ(frames, motionArgs.videoFrames);
6770 }
6771}
6772
6773TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006774 prepareAxes(POSITION);
6775 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006776 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006777 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6778 // so mix these.
6779 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6780 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6781 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6782 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6783 NotifyMotionArgs motionArgs;
6784
6785 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006786 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006787 processPosition(mapper, 100, 200);
6788 processSync(mapper);
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6790 std::for_each(frames.begin(), frames.end(),
6791 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6792 ASSERT_EQ(frames, motionArgs.videoFrames);
6793}
6794
Arthur Hung9da14732019-09-02 16:16:58 +08006795/**
6796 * If we had defined port associations, but the viewport is not ready, the touch device would be
6797 * expected to be disabled, and it should be enabled after the viewport has found.
6798 */
6799TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006800 constexpr uint8_t hdmi2 = 1;
6801 const std::string secondaryUniqueId = "uniqueId2";
6802 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6803
6804 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6805
6806 addConfigurationProperty("touch.deviceType", "touchScreen");
6807 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006808 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006809
6810 ASSERT_EQ(mDevice->isEnabled(), false);
6811
6812 // Add display on hdmi2, the device should be enabled and can receive touch event.
6813 prepareSecondaryDisplay(type, hdmi2);
6814 ASSERT_EQ(mDevice->isEnabled(), true);
6815
6816 // Send a touch event.
6817 processPosition(mapper, 100, 100);
6818 processSync(mapper);
6819
6820 NotifyMotionArgs args;
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6822 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6823}
6824
Arthur Hung6cd19a42019-08-30 19:04:12 +08006825/**
6826 * Test touch should not work if outside of surface.
6827 */
6828TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
Arthur Hung6cd19a42019-08-30 19:04:12 +08006829 addConfigurationProperty("touch.deviceType", "touchScreen");
6830 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006831 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006832 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung6cd19a42019-08-30 19:04:12 +08006833
Arthur Hung05de5772019-09-26 18:31:26 +08006834 // Touch on left-top area should work.
6835 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6836 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6837 processPosition(mapper, rawX, rawY);
6838 processSync(mapper);
6839
6840 NotifyMotionArgs args;
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6842
6843 // Reset.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006844 mapper.reset(ARBITRARY_TIME);
Arthur Hung05de5772019-09-26 18:31:26 +08006845
6846 // Let logical display be different to physical display and rotate 90-degrees.
6847 std::optional<DisplayViewport> internalViewport =
6848 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6849 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6850 internalViewport->logicalLeft = 0;
6851 internalViewport->logicalTop = 0;
6852 internalViewport->logicalRight = DISPLAY_HEIGHT;
6853 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6854
6855 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6856 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6857 internalViewport->physicalRight = DISPLAY_HEIGHT;
6858 internalViewport->physicalBottom = DISPLAY_WIDTH;
6859
6860 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6861 internalViewport->deviceHeight = DISPLAY_WIDTH;
6862 mFakePolicy->updateViewport(internalViewport.value());
6863 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6864
6865 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006866 processPosition(mapper, rawX, rawY);
6867 processSync(mapper);
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6869}
6870
Arthur Hung421eb1c2020-01-16 00:09:42 +08006871TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006872 addConfigurationProperty("touch.deviceType", "touchScreen");
6873 prepareDisplay(DISPLAY_ORIENTATION_0);
6874 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006875 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006876
6877 NotifyMotionArgs motionArgs;
6878
6879 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6880 // finger down
6881 processId(mapper, 1);
6882 processPosition(mapper, x1, y1);
6883 processSync(mapper);
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6885 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6887
6888 // finger move
6889 processId(mapper, 1);
6890 processPosition(mapper, x2, y2);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6894 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6895
6896 // finger up.
6897 processId(mapper, -1);
6898 processSync(mapper);
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6900 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6902
6903 // new finger down
6904 processId(mapper, 1);
6905 processPosition(mapper, x3, y3);
6906 processSync(mapper);
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6908 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6910}
6911
6912/**
6913 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6914 * UP events should be ignored.
6915 */
6916TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006917 addConfigurationProperty("touch.deviceType", "touchScreen");
6918 prepareDisplay(DISPLAY_ORIENTATION_0);
6919 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006921
6922 NotifyMotionArgs motionArgs;
6923
6924 // default tool type is finger
6925 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6926 processId(mapper, 1);
6927 processPosition(mapper, x1, y1);
6928 processSync(mapper);
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6930 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6932
6933 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
6934 processToolType(mapper, MT_TOOL_PALM);
6935 processSync(mapper);
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6937 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6938
6939 // Ignore the following MOVE and UP events if had detect a palm event.
6940 processId(mapper, 1);
6941 processPosition(mapper, x2, y2);
6942 processSync(mapper);
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6944
6945 // finger up.
6946 processId(mapper, -1);
6947 processSync(mapper);
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6949
6950 // new finger down
6951 processToolType(mapper, MT_TOOL_FINGER);
6952 processId(mapper, 1);
6953 processPosition(mapper, x3, y3);
6954 processSync(mapper);
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6956 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6958}
6959
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006960// --- MultiTouchInputMapperTest_ExternalDevice ---
6961
6962class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
6963protected:
6964 virtual void SetUp() override {
6965 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
6966 }
6967};
6968
6969/**
6970 * Expect fallback to internal viewport if device is external and external viewport is not present.
6971 */
6972TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
6973 prepareAxes(POSITION);
6974 addConfigurationProperty("touch.deviceType", "touchScreen");
6975 prepareDisplay(DISPLAY_ORIENTATION_0);
6976 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6977
6978 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6979
6980 NotifyMotionArgs motionArgs;
6981
6982 // Expect the event to be sent to the internal viewport,
6983 // because an external viewport is not present.
6984 processPosition(mapper, 100, 100);
6985 processSync(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6987 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6988
6989 // Expect the event to be sent to the external viewport if it is present.
6990 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6991 processPosition(mapper, 100, 100);
6992 processSync(mapper);
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6994 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6995}
6996
Michael Wrightd02c5b62014-02-10 15:10:22 -08006997} // namespace android