blob: 18bd3d06e1097f05c9129aea2b76f93644ce2b93 [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>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070030#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080032#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <math.h>
34
Michael Wright17db18e2020-06-26 20:51:44 +010035#include <memory>
36
Michael Wrightd02c5b62014-02-10 15:10:22 -080037namespace android {
38
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039using std::chrono_literals::operator""ms;
40
41// Timeout for waiting for an expected event
42static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
43
Michael Wrightd02c5b62014-02-10 15:10:22 -080044// An arbitrary time value.
45static const nsecs_t ARBITRARY_TIME = 1234;
46
47// Arbitrary display properties.
48static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070049static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080050static const int32_t DISPLAY_WIDTH = 480;
51static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070052static const int32_t VIRTUAL_DISPLAY_ID = 1;
53static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
54static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070055static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070056static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080057
58// Error tolerance for floating point assertions.
59static const float EPSILON = 0.001f;
60
61template<typename T>
62static inline T min(T a, T b) {
63 return a < b ? a : b;
64}
65
66static inline float avg(float x, float y) {
67 return (x + y) / 2;
68}
69
70
71// --- FakePointerController ---
72
73class FakePointerController : public PointerControllerInterface {
74 bool mHaveBounds;
75 float mMinX, mMinY, mMaxX, mMaxY;
76 float mX, mY;
77 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080078 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080079
Michael Wrightd02c5b62014-02-10 15:10:22 -080080public:
81 FakePointerController() :
82 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080083 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080084 }
85
Michael Wright17db18e2020-06-26 20:51:44 +010086 virtual ~FakePointerController() {}
87
Michael Wrightd02c5b62014-02-10 15:10:22 -080088 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;
Michael Wright17db18e2020-06-26 20:51:44 +0100179 std::unordered_map<int32_t, std::shared_ptr<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 Wright17db18e2020-06-26 20:51:44 +0100259 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
260 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 }
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
Michael Wright17db18e2020-06-26 20:51:44 +0100321 virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
322 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 }
324
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800325 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700326 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700328 mInputDevicesChanged = true;
329 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 }
331
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100332 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700333 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100336 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
337 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800339
340 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
341 std::unique_lock<std::mutex> lock(mLock);
342 base::ScopedLockAssertion assumeLocked(mLock);
343
344 const bool devicesChanged =
345 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
346 return mInputDevicesChanged;
347 });
348 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
349 mInputDevicesChanged = false;
350 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351};
352
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353// --- FakeEventHub ---
354
355class FakeEventHub : public EventHubInterface {
356 struct KeyInfo {
357 int32_t keyCode;
358 uint32_t flags;
359 };
360
361 struct Device {
362 InputDeviceIdentifier identifier;
363 uint32_t classes;
364 PropertyMap configuration;
365 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
366 KeyedVector<int, bool> relativeAxes;
367 KeyedVector<int32_t, int32_t> keyCodeStates;
368 KeyedVector<int32_t, int32_t> scanCodeStates;
369 KeyedVector<int32_t, int32_t> switchStates;
370 KeyedVector<int32_t, int32_t> absoluteAxisValue;
371 KeyedVector<int32_t, KeyInfo> keysByScanCode;
372 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
373 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800374 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700375 bool enabled;
376
377 status_t enable() {
378 enabled = true;
379 return OK;
380 }
381
382 status_t disable() {
383 enabled = false;
384 return OK;
385 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700387 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700388 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 }
390 };
391
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700392 std::mutex mLock;
393 std::condition_variable mEventsCondition;
394
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100396 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700397 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600398 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700400public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 virtual ~FakeEventHub() {
402 for (size_t i = 0; i < mDevices.size(); i++) {
403 delete mDevices.valueAt(i);
404 }
405 }
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 FakeEventHub() { }
408
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100409 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 Device* device = new Device(classes);
411 device->identifier.name = name;
412 mDevices.add(deviceId, device);
413
414 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
415 }
416
417 void removeDevice(int32_t deviceId) {
418 delete mDevices.valueFor(deviceId);
419 mDevices.removeItem(deviceId);
420
421 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
422 }
423
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700424 bool isDeviceEnabled(int32_t deviceId) {
425 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700426 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700427 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
428 return false;
429 }
430 return device->enabled;
431 }
432
433 status_t enableDevice(int32_t deviceId) {
434 status_t result;
435 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700436 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
438 return BAD_VALUE;
439 }
440 if (device->enabled) {
441 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
442 return OK;
443 }
444 result = device->enable();
445 return result;
446 }
447
448 status_t disableDevice(int32_t deviceId) {
449 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700450 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700451 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
452 return BAD_VALUE;
453 }
454 if (!device->enabled) {
455 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
456 return OK;
457 }
458 return device->disable();
459 }
460
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 void finishDeviceScan() {
462 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
463 }
464
465 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
466 Device* device = getDevice(deviceId);
467 device->configuration.addProperty(key, value);
468 }
469
470 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
471 Device* device = getDevice(deviceId);
472 device->configuration.addAll(configuration);
473 }
474
475 void addAbsoluteAxis(int32_t deviceId, int axis,
476 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
477 Device* device = getDevice(deviceId);
478
479 RawAbsoluteAxisInfo info;
480 info.valid = true;
481 info.minValue = minValue;
482 info.maxValue = maxValue;
483 info.flat = flat;
484 info.fuzz = fuzz;
485 info.resolution = resolution;
486 device->absoluteAxes.add(axis, info);
487 }
488
489 void addRelativeAxis(int32_t deviceId, int32_t axis) {
490 Device* device = getDevice(deviceId);
491 device->relativeAxes.add(axis, true);
492 }
493
494 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
495 Device* device = getDevice(deviceId);
496 device->keyCodeStates.replaceValueFor(keyCode, state);
497 }
498
499 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
500 Device* device = getDevice(deviceId);
501 device->scanCodeStates.replaceValueFor(scanCode, state);
502 }
503
504 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
505 Device* device = getDevice(deviceId);
506 device->switchStates.replaceValueFor(switchCode, state);
507 }
508
509 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
510 Device* device = getDevice(deviceId);
511 device->absoluteAxisValue.replaceValueFor(axis, value);
512 }
513
514 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
515 int32_t keyCode, uint32_t flags) {
516 Device* device = getDevice(deviceId);
517 KeyInfo info;
518 info.keyCode = keyCode;
519 info.flags = flags;
520 if (scanCode) {
521 device->keysByScanCode.add(scanCode, info);
522 }
523 if (usageCode) {
524 device->keysByUsageCode.add(usageCode, info);
525 }
526 }
527
528 void addLed(int32_t deviceId, int32_t led, bool initialState) {
529 Device* device = getDevice(deviceId);
530 device->leds.add(led, initialState);
531 }
532
533 bool getLedState(int32_t deviceId, int32_t led) {
534 Device* device = getDevice(deviceId);
535 return device->leds.valueFor(led);
536 }
537
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100538 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 return mExcludedDevices;
540 }
541
542 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
543 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800544 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 }
546
547 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
548 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700549 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 RawEvent event;
551 event.when = when;
552 event.deviceId = deviceId;
553 event.type = type;
554 event.code = code;
555 event.value = value;
556 mEvents.push_back(event);
557
558 if (type == EV_ABS) {
559 setAbsoluteAxisValue(deviceId, code, value);
560 }
561 }
562
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600563 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
564 std::vector<TouchVideoFrame>> videoFrames) {
565 mVideoFrames = std::move(videoFrames);
566 }
567
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700569 std::unique_lock<std::mutex> lock(mLock);
570 base::ScopedLockAssertion assumeLocked(mLock);
571 const bool queueIsEmpty =
572 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
573 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
574 if (!queueIsEmpty) {
575 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
576 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
579private:
580 Device* getDevice(int32_t deviceId) const {
581 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100582 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 }
584
585 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
586 Device* device = getDevice(deviceId);
587 return device ? device->classes : 0;
588 }
589
590 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
591 Device* device = getDevice(deviceId);
592 return device ? device->identifier : InputDeviceIdentifier();
593 }
594
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100595 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 return 0;
597 }
598
599 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
600 Device* device = getDevice(deviceId);
601 if (device) {
602 *outConfiguration = device->configuration;
603 }
604 }
605
606 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
607 RawAbsoluteAxisInfo* outAxisInfo) const {
608 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800609 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 ssize_t index = device->absoluteAxes.indexOfKey(axis);
611 if (index >= 0) {
612 *outAxisInfo = device->absoluteAxes.valueAt(index);
613 return OK;
614 }
615 }
616 outAxisInfo->clear();
617 return -1;
618 }
619
620 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
621 Device* device = getDevice(deviceId);
622 if (device) {
623 return device->relativeAxes.indexOfKey(axis) >= 0;
624 }
625 return false;
626 }
627
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100628 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 return false;
630 }
631
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700632 virtual status_t mapKey(int32_t deviceId,
633 int32_t scanCode, int32_t usageCode, int32_t metaState,
634 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 Device* device = getDevice(deviceId);
636 if (device) {
637 const KeyInfo* key = getKey(device, scanCode, usageCode);
638 if (key) {
639 if (outKeycode) {
640 *outKeycode = key->keyCode;
641 }
642 if (outFlags) {
643 *outFlags = key->flags;
644 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700645 if (outMetaState) {
646 *outMetaState = metaState;
647 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 return OK;
649 }
650 }
651 return NAME_NOT_FOUND;
652 }
653
654 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
655 if (usageCode) {
656 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
657 if (index >= 0) {
658 return &device->keysByUsageCode.valueAt(index);
659 }
660 }
661 if (scanCode) {
662 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
663 if (index >= 0) {
664 return &device->keysByScanCode.valueAt(index);
665 }
666 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700667 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 }
669
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100670 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 return NAME_NOT_FOUND;
672 }
673
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100674 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 mExcludedDevices = devices;
676 }
677
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100678 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700679 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 if (mEvents.empty()) {
681 return 0;
682 }
683
684 *buffer = *mEvents.begin();
685 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700686 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 return 1;
688 }
689
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800690 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600691 auto it = mVideoFrames.find(deviceId);
692 if (it != mVideoFrames.end()) {
693 std::vector<TouchVideoFrame> frames = std::move(it->second);
694 mVideoFrames.erase(deviceId);
695 return frames;
696 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800697 return {};
698 }
699
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
701 Device* device = getDevice(deviceId);
702 if (device) {
703 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
704 if (index >= 0) {
705 return device->scanCodeStates.valueAt(index);
706 }
707 }
708 return AKEY_STATE_UNKNOWN;
709 }
710
711 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
712 Device* device = getDevice(deviceId);
713 if (device) {
714 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
715 if (index >= 0) {
716 return device->keyCodeStates.valueAt(index);
717 }
718 }
719 return AKEY_STATE_UNKNOWN;
720 }
721
722 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
723 Device* device = getDevice(deviceId);
724 if (device) {
725 ssize_t index = device->switchStates.indexOfKey(sw);
726 if (index >= 0) {
727 return device->switchStates.valueAt(index);
728 }
729 }
730 return AKEY_STATE_UNKNOWN;
731 }
732
733 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
734 int32_t* outValue) const {
735 Device* device = getDevice(deviceId);
736 if (device) {
737 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
738 if (index >= 0) {
739 *outValue = device->absoluteAxisValue.valueAt(index);
740 return OK;
741 }
742 }
743 *outValue = 0;
744 return -1;
745 }
746
747 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
748 uint8_t* outFlags) const {
749 bool result = false;
750 Device* device = getDevice(deviceId);
751 if (device) {
752 for (size_t i = 0; i < numCodes; i++) {
753 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
754 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
755 outFlags[i] = 1;
756 result = true;
757 }
758 }
759 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
760 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
761 outFlags[i] = 1;
762 result = true;
763 }
764 }
765 }
766 }
767 return result;
768 }
769
770 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
771 Device* device = getDevice(deviceId);
772 if (device) {
773 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
774 return index >= 0;
775 }
776 return false;
777 }
778
779 virtual bool hasLed(int32_t deviceId, int32_t led) const {
780 Device* device = getDevice(deviceId);
781 return device && device->leds.indexOfKey(led) >= 0;
782 }
783
784 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
785 Device* device = getDevice(deviceId);
786 if (device) {
787 ssize_t index = device->leds.indexOfKey(led);
788 if (index >= 0) {
789 device->leds.replaceValueAt(led, on);
790 } else {
791 ADD_FAILURE()
792 << "Attempted to set the state of an LED that the EventHub declared "
793 "was not present. led=" << led;
794 }
795 }
796 }
797
798 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800799 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 outVirtualKeys.clear();
801
802 Device* device = getDevice(deviceId);
803 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800804 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 }
806 }
807
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100808 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700809 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100812 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 return false;
814 }
815
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100816 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 }
818
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100819 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 }
821
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100822 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 return false;
824 }
825
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800826 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 }
828
829 virtual void monitor() {
830 }
831
832 virtual void requestReopenDevices() {
833 }
834
835 virtual void wake() {
836 }
837};
838
839
840// --- FakeInputReaderContext ---
841
842class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700843 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 sp<InputReaderPolicyInterface> mPolicy;
845 sp<InputListenerInterface> mListener;
846 int32_t mGlobalMetaState;
847 bool mUpdateGlobalMetaStateWasCalled;
848 int32_t mGeneration;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800849 int32_t mNextId;
Michael Wright17db18e2020-06-26 20:51:44 +0100850 std::weak_ptr<PointerControllerInterface> mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700853 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
854 const sp<InputReaderPolicyInterface>& policy,
855 const sp<InputListenerInterface>& listener)
856 : mEventHub(eventHub),
857 mPolicy(policy),
858 mListener(listener),
859 mGlobalMetaState(0),
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800860 mNextId(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861
862 virtual ~FakeInputReaderContext() { }
863
864 void assertUpdateGlobalMetaStateWasCalled() {
865 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
866 << "Expected updateGlobalMetaState() to have been called.";
867 mUpdateGlobalMetaStateWasCalled = false;
868 }
869
870 void setGlobalMetaState(int32_t state) {
871 mGlobalMetaState = state;
872 }
873
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800874 uint32_t getGeneration() {
875 return mGeneration;
876 }
877
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800878 void updatePointerDisplay() {
Michael Wright17db18e2020-06-26 20:51:44 +0100879 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800880 if (controller != nullptr) {
881 InputReaderConfiguration config;
882 mPolicy->getReaderConfiguration(&config);
883 auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
884 if (viewport) {
885 controller->setDisplayViewport(*viewport);
886 }
887 }
888 }
889
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890private:
891 virtual void updateGlobalMetaState() {
892 mUpdateGlobalMetaStateWasCalled = true;
893 }
894
895 virtual int32_t getGlobalMetaState() {
896 return mGlobalMetaState;
897 }
898
899 virtual EventHubInterface* getEventHub() {
900 return mEventHub.get();
901 }
902
903 virtual InputReaderPolicyInterface* getPolicy() {
904 return mPolicy.get();
905 }
906
907 virtual InputListenerInterface* getListener() {
908 return mListener.get();
909 }
910
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100911 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 }
913
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800914 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
Michael Wright17db18e2020-06-26 20:51:44 +0100916 virtual std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) {
917 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800918 if (controller == nullptr) {
919 controller = mPolicy->obtainPointerController(deviceId);
920 mPointerController = controller;
921 updatePointerDisplay();
922 }
923 return controller;
924 }
925
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 virtual void fadePointer() {
927 }
928
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100929 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 }
931
932 virtual int32_t bumpGeneration() {
933 return ++mGeneration;
934 }
Michael Wright842500e2015-03-13 17:32:02 -0700935
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800936 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700937
938 }
939
940 virtual void dispatchExternalStylusState(const StylusState&) {
941
942 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800943
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800944 virtual int32_t getNextId() { return mNextId++; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945};
946
947
948// --- FakeInputMapper ---
949
950class FakeInputMapper : public InputMapper {
951 uint32_t mSources;
952 int32_t mKeyboardType;
953 int32_t mMetaState;
954 KeyedVector<int32_t, int32_t> mKeyCodeStates;
955 KeyedVector<int32_t, int32_t> mScanCodeStates;
956 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800957 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700959 std::mutex mLock;
960 std::condition_variable mStateChangedCondition;
961 bool mConfigureWasCalled GUARDED_BY(mLock);
962 bool mResetWasCalled GUARDED_BY(mLock);
963 bool mProcessWasCalled GUARDED_BY(mLock);
964 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
Arthur Hungc23540e2018-11-29 20:42:11 +0800966 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800968 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
969 : InputMapper(deviceContext),
970 mSources(sources),
971 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800973 mConfigureWasCalled(false),
974 mResetWasCalled(false),
975 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976
977 virtual ~FakeInputMapper() { }
978
979 void setKeyboardType(int32_t keyboardType) {
980 mKeyboardType = keyboardType;
981 }
982
983 void setMetaState(int32_t metaState) {
984 mMetaState = metaState;
985 }
986
987 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700988 std::unique_lock<std::mutex> lock(mLock);
989 base::ScopedLockAssertion assumeLocked(mLock);
990 const bool configureCalled =
991 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
992 return mConfigureWasCalled;
993 });
994 if (!configureCalled) {
995 FAIL() << "Expected configure() to have been called.";
996 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 mConfigureWasCalled = false;
998 }
999
1000 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001001 std::unique_lock<std::mutex> lock(mLock);
1002 base::ScopedLockAssertion assumeLocked(mLock);
1003 const bool resetCalled =
1004 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1005 return mResetWasCalled;
1006 });
1007 if (!resetCalled) {
1008 FAIL() << "Expected reset() to have been called.";
1009 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010 mResetWasCalled = false;
1011 }
1012
Yi Kong9b14ac62018-07-17 13:48:38 -07001013 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001014 std::unique_lock<std::mutex> lock(mLock);
1015 base::ScopedLockAssertion assumeLocked(mLock);
1016 const bool processCalled =
1017 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1018 return mProcessWasCalled;
1019 });
1020 if (!processCalled) {
1021 FAIL() << "Expected process() to have been called.";
1022 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 if (outLastEvent) {
1024 *outLastEvent = mLastEvent;
1025 }
1026 mProcessWasCalled = false;
1027 }
1028
1029 void setKeyCodeState(int32_t keyCode, int32_t state) {
1030 mKeyCodeStates.replaceValueFor(keyCode, state);
1031 }
1032
1033 void setScanCodeState(int32_t scanCode, int32_t state) {
1034 mScanCodeStates.replaceValueFor(scanCode, state);
1035 }
1036
1037 void setSwitchState(int32_t switchCode, int32_t state) {
1038 mSwitchStates.replaceValueFor(switchCode, state);
1039 }
1040
1041 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001042 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 }
1044
1045private:
1046 virtual uint32_t getSources() {
1047 return mSources;
1048 }
1049
1050 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1051 InputMapper::populateDeviceInfo(deviceInfo);
1052
1053 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1054 deviceInfo->setKeyboardType(mKeyboardType);
1055 }
1056 }
1057
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001059 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001060 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001061
1062 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001063 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001064 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1065 mViewport = config->getDisplayViewportByPort(*displayPort);
1066 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001067
1068 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069 }
1070
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001071 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001072 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001074 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075 }
1076
1077 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001078 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079 mLastEvent = *rawEvent;
1080 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001081 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 }
1083
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001084 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1086 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1087 }
1088
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001089 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1091 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1092 }
1093
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001094 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1096 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1097 }
1098
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001099 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100 const int32_t* keyCodes, uint8_t* outFlags) {
1101 bool result = false;
1102 for (size_t i = 0; i < numCodes; i++) {
1103 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1104 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1105 outFlags[i] = 1;
1106 result = true;
1107 }
1108 }
1109 }
1110 return result;
1111 }
1112
1113 virtual int32_t getMetaState() {
1114 return mMetaState;
1115 }
1116
1117 virtual void fadePointer() {
1118 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001119
1120 virtual std::optional<int32_t> getAssociatedDisplay() {
1121 if (mViewport) {
1122 return std::make_optional(mViewport->displayId);
1123 }
1124 return std::nullopt;
1125 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126};
1127
1128
1129// --- InstrumentedInputReader ---
1130
1131class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001132 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133
1134public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001135 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1136 const sp<InputReaderPolicyInterface>& policy,
1137 const sp<InputListenerInterface>& listener)
1138 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001139
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001140 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001142 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001144 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001145 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146 InputDeviceIdentifier identifier;
1147 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001148 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001150 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 }
1152
Prabir Pradhan28efc192019-11-05 01:10:04 +00001153 // Make the protected loopOnce method accessible to tests.
1154 using InputReader::loopOnce;
1155
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001157 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1158 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001160 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001161 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 return device;
1163 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001164 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165 }
1166
1167 friend class InputReaderTest;
1168};
1169
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001170// --- InputReaderPolicyTest ---
1171class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001172protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001173 sp<FakeInputReaderPolicy> mFakePolicy;
1174
Prabir Pradhan28efc192019-11-05 01:10:04 +00001175 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1176 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177};
1178
1179/**
1180 * Check that empty set of viewports is an acceptable configuration.
1181 * Also try to get internal viewport two different ways - by type and by uniqueId.
1182 *
1183 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1184 * Such configuration is not currently allowed.
1185 */
1186TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001187 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001188
1189 // We didn't add any viewports yet, so there shouldn't be any.
1190 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192 ASSERT_FALSE(internalViewport);
1193
1194 // Add an internal viewport, then clear it
1195 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001196 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001197
1198 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001199 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001200 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001201 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202
1203 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001204 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001205 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001206 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001207
1208 mFakePolicy->clearViewports();
1209 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001210 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001211 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001212 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213 ASSERT_FALSE(internalViewport);
1214}
1215
1216TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1217 const std::string internalUniqueId = "local:0";
1218 const std::string externalUniqueId = "local:1";
1219 const std::string virtualUniqueId1 = "virtual:2";
1220 const std::string virtualUniqueId2 = "virtual:3";
1221 constexpr int32_t virtualDisplayId1 = 2;
1222 constexpr int32_t virtualDisplayId2 = 3;
1223
1224 // Add an internal viewport
1225 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001226 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001227 // Add an external viewport
1228 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001229 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001230 // Add an virtual viewport
1231 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001232 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001233 // Add another virtual viewport
1234 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001235 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001236
1237 // Check matching by type for internal
1238 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001239 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001240 ASSERT_TRUE(internalViewport);
1241 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1242
1243 // Check matching by type for external
1244 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001245 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001246 ASSERT_TRUE(externalViewport);
1247 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1248
1249 // Check matching by uniqueId for virtual viewport #1
1250 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001251 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001252 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001253 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001254 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1255 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1256
1257 // Check matching by uniqueId for virtual viewport #2
1258 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001259 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001260 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001261 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001262 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1263 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1264}
1265
1266
1267/**
1268 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1269 * that lookup works by checking display id.
1270 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1271 */
1272TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1273 const std::string uniqueId1 = "uniqueId1";
1274 const std::string uniqueId2 = "uniqueId2";
1275 constexpr int32_t displayId1 = 2;
1276 constexpr int32_t displayId2 = 3;
1277
1278 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1279 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1280 for (const ViewportType& type : types) {
1281 mFakePolicy->clearViewports();
1282 // Add a viewport
1283 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001284 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001285 // Add another viewport
1286 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001287 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001288
1289 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001290 std::optional<DisplayViewport> viewport1 =
1291 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001292 ASSERT_TRUE(viewport1);
1293 ASSERT_EQ(displayId1, viewport1->displayId);
1294 ASSERT_EQ(type, viewport1->type);
1295
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001296 std::optional<DisplayViewport> viewport2 =
1297 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001298 ASSERT_TRUE(viewport2);
1299 ASSERT_EQ(displayId2, viewport2->displayId);
1300 ASSERT_EQ(type, viewport2->type);
1301
1302 // When there are multiple viewports of the same kind, and uniqueId is not specified
1303 // in the call to getDisplayViewport, then that situation is not supported.
1304 // The viewports can be stored in any order, so we cannot rely on the order, since that
1305 // is just implementation detail.
1306 // However, we can check that it still returns *a* viewport, we just cannot assert
1307 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001308 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001309 ASSERT_TRUE(someViewport);
1310 }
1311}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001313/**
1314 * Check getDisplayViewportByPort
1315 */
1316TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1317 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1318 const std::string uniqueId1 = "uniqueId1";
1319 const std::string uniqueId2 = "uniqueId2";
1320 constexpr int32_t displayId1 = 1;
1321 constexpr int32_t displayId2 = 2;
1322 const uint8_t hdmi1 = 0;
1323 const uint8_t hdmi2 = 1;
1324 const uint8_t hdmi3 = 2;
1325
1326 mFakePolicy->clearViewports();
1327 // Add a viewport that's associated with some display port that's not of interest.
1328 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1329 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1330 // Add another viewport, connected to HDMI1 port
1331 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1332 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1333
1334 // Check that correct display viewport was returned by comparing the display ports.
1335 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1336 ASSERT_TRUE(hdmi1Viewport);
1337 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1338 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1339
1340 // Check that we can still get the same viewport using the uniqueId
1341 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1342 ASSERT_TRUE(hdmi1Viewport);
1343 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1344 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1345 ASSERT_EQ(type, hdmi1Viewport->type);
1346
1347 // Check that we cannot find a port with "HDMI2", because we never added one
1348 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1349 ASSERT_FALSE(hdmi2Viewport);
1350}
1351
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352// --- InputReaderTest ---
1353
1354class InputReaderTest : public testing::Test {
1355protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001356 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001357 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001358 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001359 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001360
Prabir Pradhan28efc192019-11-05 01:10:04 +00001361 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001362 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001363 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001364 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365
Prabir Pradhan28efc192019-11-05 01:10:04 +00001366 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1367 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001368 }
1369
Prabir Pradhan28efc192019-11-05 01:10:04 +00001370 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001371 mFakeListener.clear();
1372 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373 }
1374
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001375 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1376 const PropertyMap* configuration) {
1377 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378
1379 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001380 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 }
1382 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001383 mReader->loopOnce();
1384 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001385 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1386 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001387 }
1388
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001389 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001390 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001391 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001392 }
1393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001394 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001395 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001396 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001397 }
1398
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001399 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001400 const std::string& name, uint32_t classes,
1401 uint32_t sources,
1402 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001403 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1404 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001405 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001406 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001407 return mapper;
1408 }
1409};
1410
1411TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001412 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001413 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001414 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001415 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001417 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001419 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001420 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001421 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001422 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1423 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1424 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1425
1426 // Should also have received a notification describing the new input devices.
1427 inputDevices = mFakePolicy->getInputDevices();
1428 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001429 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001430 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001431 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1432 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1433 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1434}
1435
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001436TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001437 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001438 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001439 constexpr int32_t eventHubId = 1;
1440 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001441 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001442 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001443 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001444 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001445
Yi Kong9b14ac62018-07-17 13:48:38 -07001446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001447
1448 NotifyDeviceResetArgs resetArgs;
1449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001450 ASSERT_EQ(deviceId, resetArgs.deviceId);
1451
1452 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001453 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001454 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001455
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001457 ASSERT_EQ(deviceId, resetArgs.deviceId);
1458 ASSERT_EQ(device->isEnabled(), false);
1459
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001460 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001461 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001464 ASSERT_EQ(device->isEnabled(), false);
1465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001466 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001467 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001469 ASSERT_EQ(deviceId, resetArgs.deviceId);
1470 ASSERT_EQ(device->isEnabled(), true);
1471}
1472
Michael Wrightd02c5b62014-02-10 15:10:22 -08001473TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001474 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1475 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1476 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001477 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001478 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001479 AINPUT_SOURCE_KEYBOARD, nullptr);
1480 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481
1482 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1483 AINPUT_SOURCE_ANY, AKEYCODE_A))
1484 << "Should return unknown when the device id is >= 0 but unknown.";
1485
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001486 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1487 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1488 << "Should return unknown when the device id is valid but the sources are not "
1489 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001490
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001491 ASSERT_EQ(AKEY_STATE_DOWN,
1492 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1493 AKEYCODE_A))
1494 << "Should return value provided by mapper when device id is valid and the device "
1495 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001496
1497 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1498 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1499 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1500
1501 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1502 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1503 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1504}
1505
1506TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001507 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1508 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1509 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001510 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001511 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001512 AINPUT_SOURCE_KEYBOARD, nullptr);
1513 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514
1515 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1516 AINPUT_SOURCE_ANY, KEY_A))
1517 << "Should return unknown when the device id is >= 0 but unknown.";
1518
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1520 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1521 << "Should return unknown when the device id is valid but the sources are not "
1522 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001523
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001524 ASSERT_EQ(AKEY_STATE_DOWN,
1525 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1526 KEY_A))
1527 << "Should return value provided by mapper when device id is valid and the device "
1528 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529
1530 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1531 AINPUT_SOURCE_TRACKBALL, KEY_A))
1532 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1533
1534 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1535 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1536 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1537}
1538
1539TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001540 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1541 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1542 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001543 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001544 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001545 AINPUT_SOURCE_KEYBOARD, nullptr);
1546 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547
1548 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1549 AINPUT_SOURCE_ANY, SW_LID))
1550 << "Should return unknown when the device id is >= 0 but unknown.";
1551
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1553 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1554 << "Should return unknown when the device id is valid but the sources are not "
1555 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001556
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001557 ASSERT_EQ(AKEY_STATE_DOWN,
1558 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1559 SW_LID))
1560 << "Should return value provided by mapper when device id is valid and the device "
1561 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562
1563 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1564 AINPUT_SOURCE_TRACKBALL, SW_LID))
1565 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1566
1567 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1568 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1569 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1570}
1571
1572TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001573 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1574 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1575 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001576 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001577 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001578 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001580 mapper.addSupportedKeyCode(AKEYCODE_A);
1581 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582
1583 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1584 uint8_t flags[4] = { 0, 0, 0, 1 };
1585
1586 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1587 << "Should return false when device id is >= 0 but unknown.";
1588 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1589
1590 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1592 << "Should return false when device id is valid but the sources are not supported by "
1593 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1595
1596 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001597 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1598 keyCodes, flags))
1599 << "Should return value provided by mapper when device id is valid and the device "
1600 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1602
1603 flags[3] = 1;
1604 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1605 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1606 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1607
1608 flags[3] = 1;
1609 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1610 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1611 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1612}
1613
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001614TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 constexpr int32_t eventHubId = 1;
1616 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617
1618 NotifyConfigurationChangedArgs args;
1619
1620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1621 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1622}
1623
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001624TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1626 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1627 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001628 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001629 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001630 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001633 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1635
1636 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001637 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001638 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640 ASSERT_EQ(EV_KEY, event.type);
1641 ASSERT_EQ(KEY_A, event.code);
1642 ASSERT_EQ(1, event.value);
1643}
1644
Garfield Tan1c7bc862020-01-28 13:24:04 -08001645TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001647 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 constexpr int32_t eventHubId = 1;
1649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001650 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001651 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001652 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001654
1655 NotifyDeviceResetArgs resetArgs;
1656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001657 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001658
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001659 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001660 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001662 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001663 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001664
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001665 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001666 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001668 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001669 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001670
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001671 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001672 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001674 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001675 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001676}
1677
Garfield Tan1c7bc862020-01-28 13:24:04 -08001678TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1679 constexpr int32_t deviceId = 1;
1680 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1681 constexpr int32_t eventHubId = 1;
1682 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1683 // Must add at least one mapper or the device will be ignored!
1684 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1685 mReader->setNextDevice(device);
1686 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1687
1688 NotifyDeviceResetArgs resetArgs;
1689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1690 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1691}
1692
Arthur Hungc23540e2018-11-29 20:42:11 +08001693TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001694 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001695 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001697 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001698 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1699 FakeInputMapper& mapper =
1700 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001701 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001702
1703 const uint8_t hdmi1 = 1;
1704
1705 // Associated touch screen with second display.
1706 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1707
1708 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001709 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001710 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1711 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1712 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1713 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1714 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001715 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001716
1717 // Add the device, and make sure all of the callbacks are triggered.
1718 // The device is added after the input port associations are processed since
1719 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001723 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001724
Arthur Hung2c9a3342019-07-23 14:18:59 +08001725 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001726 ASSERT_EQ(deviceId, device->getId());
1727 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1728 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001729
1730 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001731 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001732 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001733 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001734}
1735
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001736// --- InputReaderIntegrationTest ---
1737
1738// These tests create and interact with the InputReader only through its interface.
1739// The InputReader is started during SetUp(), which starts its processing in its own
1740// thread. The tests use linux uinput to emulate input devices.
1741// NOTE: Interacting with the physical device while these tests are running may cause
1742// the tests to fail.
1743class InputReaderIntegrationTest : public testing::Test {
1744protected:
1745 sp<TestInputListener> mTestListener;
1746 sp<FakeInputReaderPolicy> mFakePolicy;
1747 sp<InputReaderInterface> mReader;
1748
1749 virtual void SetUp() override {
1750 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001751 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1752 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001753
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001754 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001755 ASSERT_EQ(mReader->start(), OK);
1756
1757 // Since this test is run on a real device, all the input devices connected
1758 // to the test device will show up in mReader. We wait for those input devices to
1759 // show up before beginning the tests.
1760 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1761 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1762 }
1763
1764 virtual void TearDown() override {
1765 ASSERT_EQ(mReader->stop(), OK);
1766 mTestListener.clear();
1767 mFakePolicy.clear();
1768 }
1769};
1770
1771TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1772 // An invalid input device that is only used for this test.
1773 class InvalidUinputDevice : public UinputDevice {
1774 public:
1775 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1776
1777 private:
1778 void configureDevice(int fd, uinput_user_dev* device) override {}
1779 };
1780
1781 const size_t numDevices = mFakePolicy->getInputDevices().size();
1782
1783 // UinputDevice does not set any event or key bits, so InputReader should not
1784 // consider it as a valid device.
1785 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1786 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1787 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1788 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1789
1790 invalidDevice.reset();
1791 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1792 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1793 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1794}
1795
1796TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1797 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1798
1799 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1800 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1801 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1802 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1803
1804 // Find the test device by its name.
1805 std::vector<InputDeviceInfo> inputDevices;
1806 mReader->getInputDevices(inputDevices);
1807 InputDeviceInfo* keyboardInfo = nullptr;
1808 const char* keyboardName = keyboard->getName();
1809 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1810 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1811 keyboardInfo = &inputDevices[i];
1812 break;
1813 }
1814 }
1815 ASSERT_NE(keyboardInfo, nullptr);
1816 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1817 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1818 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1819
1820 keyboard.reset();
1821 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1822 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1823 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1824}
1825
1826TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1827 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1828 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1829
1830 NotifyConfigurationChangedArgs configChangedArgs;
1831 ASSERT_NO_FATAL_FAILURE(
1832 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001833 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001834 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1835
1836 NotifyKeyArgs keyArgs;
1837 keyboard->pressAndReleaseHomeKey();
1838 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1839 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001840 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001841 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001842 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1843 prevTimestamp = keyArgs.eventTime;
1844
1845 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1846 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001847 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001848 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1849}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001851/**
1852 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1853 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1854 * are passed to the listener.
1855 */
1856static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1857TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1858 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1859 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1860 NotifyKeyArgs keyArgs;
1861
1862 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1863 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1864 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1865 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1866
1867 controller->pressAndReleaseKey(BTN_GEAR_UP);
1868 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1869 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1870 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1871}
1872
Arthur Hungaab25622020-01-16 11:22:11 +08001873// --- TouchProcessTest ---
1874class TouchIntegrationTest : public InputReaderIntegrationTest {
1875protected:
1876 static const int32_t FIRST_SLOT = 0;
1877 static const int32_t SECOND_SLOT = 1;
1878 static const int32_t FIRST_TRACKING_ID = 0;
1879 static const int32_t SECOND_TRACKING_ID = 1;
1880 const std::string UNIQUE_ID = "local:0";
1881
1882 virtual void SetUp() override {
1883 InputReaderIntegrationTest::SetUp();
1884 // At least add an internal display.
1885 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1886 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
1887 ViewportType::VIEWPORT_INTERNAL);
1888
1889 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1890 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1891 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1892 }
1893
1894 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1895 int32_t orientation, const std::string& uniqueId,
1896 std::optional<uint8_t> physicalPort,
1897 ViewportType viewportType) {
1898 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1899 physicalPort, viewportType);
1900 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1901 }
1902
1903 std::unique_ptr<UinputTouchScreen> mDevice;
1904};
1905
1906TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1907 NotifyMotionArgs args;
1908 const Point centerPoint = mDevice->getCenterPoint();
1909
1910 // ACTION_DOWN
1911 mDevice->sendDown(centerPoint);
1912 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1913 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1914
1915 // ACTION_MOVE
1916 mDevice->sendMove(centerPoint + Point(1, 1));
1917 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1919
1920 // ACTION_UP
1921 mDevice->sendUp();
1922 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1923 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1924}
1925
1926TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1927 NotifyMotionArgs args;
1928 const Point centerPoint = mDevice->getCenterPoint();
1929
1930 // ACTION_DOWN
1931 mDevice->sendDown(centerPoint);
1932 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1933 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1934
1935 // ACTION_POINTER_DOWN (Second slot)
1936 const Point secondPoint = centerPoint + Point(100, 100);
1937 mDevice->sendSlot(SECOND_SLOT);
1938 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1939 mDevice->sendDown(secondPoint + Point(1, 1));
1940 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1941 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1942 args.action);
1943
1944 // ACTION_MOVE (Second slot)
1945 mDevice->sendMove(secondPoint);
1946 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1948
1949 // ACTION_POINTER_UP (Second slot)
1950 mDevice->sendUp();
1951 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1952 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1953 args.action);
1954
1955 // ACTION_UP
1956 mDevice->sendSlot(FIRST_SLOT);
1957 mDevice->sendUp();
1958 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1959 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1960}
1961
1962TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1963 NotifyMotionArgs args;
1964 const Point centerPoint = mDevice->getCenterPoint();
1965
1966 // ACTION_DOWN
1967 mDevice->sendDown(centerPoint);
1968 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1969 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1970
1971 // ACTION_POINTER_DOWN (Second slot)
1972 const Point secondPoint = centerPoint + Point(100, 100);
1973 mDevice->sendSlot(SECOND_SLOT);
1974 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1975 mDevice->sendDown(secondPoint);
1976 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1977 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1978 args.action);
1979
1980 // ACTION_MOVE (Second slot)
1981 mDevice->sendMove(secondPoint + Point(1, 1));
1982 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1984
1985 // Send MT_TOOL_PALM, which indicates that the touch IC has determined this to be a grip event.
1986 // Expect to receive ACTION_CANCEL, to abort the entire gesture.
1987 mDevice->sendToolType(MT_TOOL_PALM);
1988 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1989 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, args.action);
1990
1991 // ACTION_POINTER_UP (Second slot)
1992 mDevice->sendUp();
1993
1994 // ACTION_UP
1995 mDevice->sendSlot(FIRST_SLOT);
1996 mDevice->sendUp();
1997
1998 // Expect no event received after abort the entire gesture.
1999 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2000}
2001
Michael Wrightd02c5b62014-02-10 15:10:22 -08002002// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002003class InputDeviceTest : public testing::Test {
2004protected:
2005 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002006 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002007 static const int32_t DEVICE_ID;
2008 static const int32_t DEVICE_GENERATION;
2009 static const int32_t DEVICE_CONTROLLER_NUMBER;
2010 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002011 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002012
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002013 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002014 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002015 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002016 FakeInputReaderContext* mFakeContext;
2017
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002018 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002019
Prabir Pradhan28efc192019-11-05 01:10:04 +00002020 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002021 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002022 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002023 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002024 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2025
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002026 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002027 InputDeviceIdentifier identifier;
2028 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002029 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002030 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2031 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002032 }
2033
Prabir Pradhan28efc192019-11-05 01:10:04 +00002034 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002035 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002036 delete mFakeContext;
2037 mFakeListener.clear();
2038 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002039 }
2040};
2041
2042const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002043const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002044const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002045const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2046const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
2047const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
2048 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002049const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002050
2051TEST_F(InputDeviceTest, ImmutableProperties) {
2052 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002053 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002054 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002055}
2056
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002057TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2058 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002059}
2060
Michael Wrightd02c5b62014-02-10 15:10:22 -08002061TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2062 // Configuration.
2063 InputReaderConfiguration config;
2064 mDevice->configure(ARBITRARY_TIME, &config, 0);
2065
2066 // Reset.
2067 mDevice->reset(ARBITRARY_TIME);
2068
2069 NotifyDeviceResetArgs resetArgs;
2070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2071 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2072 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2073
2074 // Metadata.
2075 ASSERT_TRUE(mDevice->isIgnored());
2076 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2077
2078 InputDeviceInfo info;
2079 mDevice->getDeviceInfo(&info);
2080 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002081 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002082 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2083 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2084
2085 // State queries.
2086 ASSERT_EQ(0, mDevice->getMetaState());
2087
2088 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2089 << "Ignored device should return unknown key code state.";
2090 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2091 << "Ignored device should return unknown scan code state.";
2092 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2093 << "Ignored device should return unknown switch state.";
2094
2095 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2096 uint8_t flags[2] = { 0, 1 };
2097 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2098 << "Ignored device should never mark any key codes.";
2099 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2100 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2101}
2102
2103TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2104 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002105 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002106
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002107 FakeInputMapper& mapper1 =
2108 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002109 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2110 mapper1.setMetaState(AMETA_ALT_ON);
2111 mapper1.addSupportedKeyCode(AKEYCODE_A);
2112 mapper1.addSupportedKeyCode(AKEYCODE_B);
2113 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2114 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2115 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2116 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2117 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002118
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002119 FakeInputMapper& mapper2 =
2120 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002121 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002122
2123 InputReaderConfiguration config;
2124 mDevice->configure(ARBITRARY_TIME, &config, 0);
2125
2126 String8 propertyValue;
2127 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2128 << "Device should have read configuration during configuration phase.";
2129 ASSERT_STREQ("value", propertyValue.string());
2130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002131 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2132 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002133
2134 // Reset
2135 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002136 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2137 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002138
2139 NotifyDeviceResetArgs resetArgs;
2140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2141 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2142 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2143
2144 // Metadata.
2145 ASSERT_FALSE(mDevice->isIgnored());
2146 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2147
2148 InputDeviceInfo info;
2149 mDevice->getDeviceInfo(&info);
2150 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002151 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002152 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2153 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2154
2155 // State queries.
2156 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2157 << "Should query mappers and combine meta states.";
2158
2159 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2160 << "Should return unknown key code state when source not supported.";
2161 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2162 << "Should return unknown scan code state when source not supported.";
2163 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2164 << "Should return unknown switch state when source not supported.";
2165
2166 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2167 << "Should query mapper when source is supported.";
2168 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2169 << "Should query mapper when source is supported.";
2170 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2171 << "Should query mapper when source is supported.";
2172
2173 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2174 uint8_t flags[4] = { 0, 0, 0, 1 };
2175 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2176 << "Should do nothing when source is unsupported.";
2177 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2178 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2179 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2180 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2181
2182 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2183 << "Should query mapper when source is supported.";
2184 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2185 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2186 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2187 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2188
2189 // Event handling.
2190 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002191 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002192 mDevice->process(&event, 1);
2193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002194 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2195 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002196}
2197
Arthur Hung2c9a3342019-07-23 14:18:59 +08002198// A single input device is associated with a specific display. Check that:
2199// 1. Device is disabled if the viewport corresponding to the associated display is not found
2200// 2. Device is disabled when setEnabled API is called
2201TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002202 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002203
2204 // First Configuration.
2205 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2206
2207 // Device should be enabled by default.
2208 ASSERT_TRUE(mDevice->isEnabled());
2209
2210 // Prepare associated info.
2211 constexpr uint8_t hdmi = 1;
2212 const std::string UNIQUE_ID = "local:1";
2213
2214 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2215 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2216 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2217 // Device should be disabled because it is associated with a specific display via
2218 // input port <-> display port association, but the corresponding display is not found
2219 ASSERT_FALSE(mDevice->isEnabled());
2220
2221 // Prepare displays.
2222 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2223 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2224 ViewportType::VIEWPORT_INTERNAL);
2225 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2226 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2227 ASSERT_TRUE(mDevice->isEnabled());
2228
2229 // Device should be disabled after set disable.
2230 mFakePolicy->addDisabledDevice(mDevice->getId());
2231 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2232 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2233 ASSERT_FALSE(mDevice->isEnabled());
2234
2235 // Device should still be disabled even found the associated display.
2236 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2237 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2238 ASSERT_FALSE(mDevice->isEnabled());
2239}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002240
2241// --- InputMapperTest ---
2242
2243class InputMapperTest : public testing::Test {
2244protected:
2245 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002246 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002247 static const int32_t DEVICE_ID;
2248 static const int32_t DEVICE_GENERATION;
2249 static const int32_t DEVICE_CONTROLLER_NUMBER;
2250 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002251 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002252
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002253 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002254 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002255 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256 FakeInputReaderContext* mFakeContext;
2257 InputDevice* mDevice;
2258
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002259 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002260 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002261 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002262 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002263 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2264 InputDeviceIdentifier identifier;
2265 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002266 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002267 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002268
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002269 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270 }
2271
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002272 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2273
Prabir Pradhan28efc192019-11-05 01:10:04 +00002274 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002275 delete mDevice;
2276 delete mFakeContext;
2277 mFakeListener.clear();
2278 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279 }
2280
2281 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002282 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283 }
2284
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002285 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002286 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2287 mFakeContext->updatePointerDisplay();
2288 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002289 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2290 }
2291
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002292 template <class T, typename... Args>
2293 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002294 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002295 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002296 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002297 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002298 }
2299
2300 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002301 int32_t orientation, const std::string& uniqueId,
2302 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002303 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002304 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002305 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2306 }
2307
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002308 void clearViewports() {
2309 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002310 }
2311
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002312 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2313 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002314 RawEvent event;
2315 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002316 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002317 event.type = type;
2318 event.code = code;
2319 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002320 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002321 }
2322
2323 static void assertMotionRange(const InputDeviceInfo& info,
2324 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2325 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002326 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002327 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2328 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2329 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2330 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2331 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2332 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2333 }
2334
2335 static void assertPointerCoords(const PointerCoords& coords,
2336 float x, float y, float pressure, float size,
2337 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2338 float orientation, float distance) {
2339 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2340 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2341 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2342 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2343 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2344 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2345 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2346 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2347 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2348 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2349 }
2350
Michael Wright17db18e2020-06-26 20:51:44 +01002351 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002353 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002354 ASSERT_NEAR(x, actualX, 1);
2355 ASSERT_NEAR(y, actualY, 1);
2356 }
2357};
2358
2359const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002360const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002361const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002362const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2363const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2364const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002365const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002366
2367// --- SwitchInputMapperTest ---
2368
2369class SwitchInputMapperTest : public InputMapperTest {
2370protected:
2371};
2372
2373TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002374 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002375
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002376 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002377}
2378
2379TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002380 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002382 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002383 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002385 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002386 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387}
2388
2389TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002390 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002391
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002392 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2393 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2394 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2395 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396
2397 NotifySwitchArgs args;
2398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2399 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002400 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2401 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 args.switchMask);
2403 ASSERT_EQ(uint32_t(0), args.policyFlags);
2404}
2405
2406
2407// --- KeyboardInputMapperTest ---
2408
2409class KeyboardInputMapperTest : public InputMapperTest {
2410protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002411 const std::string UNIQUE_ID = "local:0";
2412
2413 void prepareDisplay(int32_t orientation);
2414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002415 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002416 int32_t originalKeyCode, int32_t rotatedKeyCode,
2417 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002418};
2419
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002420/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2421 * orientation.
2422 */
2423void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2424 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002425 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002426}
2427
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002428void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002429 int32_t originalScanCode, int32_t originalKeyCode,
2430 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431 NotifyKeyArgs args;
2432
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002433 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2435 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2436 ASSERT_EQ(originalScanCode, args.scanCode);
2437 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002438 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002440 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2442 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2443 ASSERT_EQ(originalScanCode, args.scanCode);
2444 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002445 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446}
2447
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002449 KeyboardInputMapper& mapper =
2450 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2451 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002453 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454}
2455
2456TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2457 const int32_t USAGE_A = 0x070004;
2458 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2460 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002462 KeyboardInputMapper& mapper =
2463 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2464 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465
2466 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002467 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468 NotifyKeyArgs args;
2469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2470 ASSERT_EQ(DEVICE_ID, args.deviceId);
2471 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2472 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2473 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2474 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2475 ASSERT_EQ(KEY_HOME, args.scanCode);
2476 ASSERT_EQ(AMETA_NONE, args.metaState);
2477 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2478 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2479 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2480
2481 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002482 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2484 ASSERT_EQ(DEVICE_ID, args.deviceId);
2485 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2486 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2487 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2488 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2489 ASSERT_EQ(KEY_HOME, args.scanCode);
2490 ASSERT_EQ(AMETA_NONE, args.metaState);
2491 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2492 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2493 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2494
2495 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002496 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2497 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2499 ASSERT_EQ(DEVICE_ID, args.deviceId);
2500 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2501 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2502 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2503 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2504 ASSERT_EQ(0, args.scanCode);
2505 ASSERT_EQ(AMETA_NONE, args.metaState);
2506 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2507 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2508 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2509
2510 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002511 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2512 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2514 ASSERT_EQ(DEVICE_ID, args.deviceId);
2515 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2516 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2517 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2518 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2519 ASSERT_EQ(0, args.scanCode);
2520 ASSERT_EQ(AMETA_NONE, args.metaState);
2521 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2522 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2523 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2524
2525 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002526 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2527 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2529 ASSERT_EQ(DEVICE_ID, args.deviceId);
2530 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2531 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2532 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2533 ASSERT_EQ(0, args.keyCode);
2534 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2535 ASSERT_EQ(AMETA_NONE, args.metaState);
2536 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2537 ASSERT_EQ(0U, args.policyFlags);
2538 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2539
2540 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002541 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2542 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2544 ASSERT_EQ(DEVICE_ID, args.deviceId);
2545 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2546 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2547 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2548 ASSERT_EQ(0, args.keyCode);
2549 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2550 ASSERT_EQ(AMETA_NONE, args.metaState);
2551 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2552 ASSERT_EQ(0U, args.policyFlags);
2553 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2554}
2555
2556TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002559
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002560 KeyboardInputMapper& mapper =
2561 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2562 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563
2564 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002565 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566
2567 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002568 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002569 NotifyKeyArgs args;
2570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2571 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002572 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002573 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2574
2575 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002576 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2578 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002579 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580
2581 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002582 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2584 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002585 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002586
2587 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002588 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2590 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002591 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002592 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2593}
2594
2595TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002596 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2597 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2599 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002601 KeyboardInputMapper& mapper =
2602 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2603 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002605 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2607 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2608 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2609 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2610 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2611 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2613 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2614}
2615
2616TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002617 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2618 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2619 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2620 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002623 KeyboardInputMapper& mapper =
2624 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2625 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002627 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002628 ASSERT_NO_FATAL_FAILURE(
2629 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2630 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2631 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2633 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2635 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002637 clearViewports();
2638 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002639 ASSERT_NO_FATAL_FAILURE(
2640 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2641 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2642 AKEYCODE_DPAD_UP, DISPLAY_ID));
2643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2644 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2646 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002648 clearViewports();
2649 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002650 ASSERT_NO_FATAL_FAILURE(
2651 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2652 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2653 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2654 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2655 AKEYCODE_DPAD_UP, DISPLAY_ID));
2656 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2657 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002659 clearViewports();
2660 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002661 ASSERT_NO_FATAL_FAILURE(
2662 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2663 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2664 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2665 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2666 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2667 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2668 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669
2670 // Special case: if orientation changes while key is down, we still emit the same keycode
2671 // in the key up as we did in the key down.
2672 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002673 clearViewports();
2674 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002675 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2677 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2678 ASSERT_EQ(KEY_UP, args.scanCode);
2679 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2680
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002681 clearViewports();
2682 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002683 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2685 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2686 ASSERT_EQ(KEY_UP, args.scanCode);
2687 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2688}
2689
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002690TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2691 // If the keyboard is not orientation aware,
2692 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002693 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002694
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002695 KeyboardInputMapper& mapper =
2696 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2697 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002698 NotifyKeyArgs args;
2699
2700 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002701 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002703 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2705 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2706
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002707 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002708 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002710 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2712 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2713}
2714
2715TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2716 // If the keyboard is orientation aware,
2717 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002718 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002719
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002720 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002721 KeyboardInputMapper& mapper =
2722 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2723 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002724 NotifyKeyArgs args;
2725
2726 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2727 // ^--- already checked by the previous test
2728
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002729 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002730 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002731 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002733 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2735 ASSERT_EQ(DISPLAY_ID, args.displayId);
2736
2737 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002738 clearViewports();
2739 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002740 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002741 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002743 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2745 ASSERT_EQ(newDisplayId, args.displayId);
2746}
2747
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002749 KeyboardInputMapper& mapper =
2750 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2751 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002753 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002754 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002756 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758}
2759
2760TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002761 KeyboardInputMapper& mapper =
2762 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2763 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002765 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002766 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002768 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002769 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770}
2771
2772TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002773 KeyboardInputMapper& mapper =
2774 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2775 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002777 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778
2779 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2780 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002781 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 ASSERT_TRUE(flags[0]);
2783 ASSERT_FALSE(flags[1]);
2784}
2785
2786TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002787 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2788 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2789 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2790 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2791 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2792 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002794 KeyboardInputMapper& mapper =
2795 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2796 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002797
2798 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002799 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2800 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802
2803 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002804 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2805 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002806 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2807 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2808 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002809 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810
2811 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002812 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2813 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002814 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2815 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002817 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818
2819 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002820 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2821 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002822 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2823 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002825 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826
2827 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002828 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2829 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002830 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2831 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2832 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002833 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834
2835 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002836 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2837 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002838 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2839 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2840 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002841 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842
2843 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002844 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2845 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002846 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2847 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2848 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002849 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850}
2851
Arthur Hung2c9a3342019-07-23 14:18:59 +08002852TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2853 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002854 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2855 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2856 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2857 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002858
2859 // keyboard 2.
2860 const std::string USB2 = "USB2";
2861 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002862 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002863 InputDeviceIdentifier identifier;
2864 identifier.name = "KEYBOARD2";
2865 identifier.location = USB2;
2866 std::unique_ptr<InputDevice> device2 =
2867 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002868 identifier);
2869 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2870 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2871 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2872 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2873 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002875 KeyboardInputMapper& mapper =
2876 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2877 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002879 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002880 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002881 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2883 device2->reset(ARBITRARY_TIME);
2884
2885 // Prepared displays and associated info.
2886 constexpr uint8_t hdmi1 = 0;
2887 constexpr uint8_t hdmi2 = 1;
2888 const std::string SECONDARY_UNIQUE_ID = "local:1";
2889
2890 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2891 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2892
2893 // No associated display viewport found, should disable the device.
2894 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2895 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2896 ASSERT_FALSE(device2->isEnabled());
2897
2898 // Prepare second display.
2899 constexpr int32_t newDisplayId = 2;
2900 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2901 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2902 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2903 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2904 // Default device will reconfigure above, need additional reconfiguration for another device.
2905 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2906 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2907
2908 // Device should be enabled after the associated display is found.
2909 ASSERT_TRUE(mDevice->isEnabled());
2910 ASSERT_TRUE(device2->isEnabled());
2911
2912 // Test pad key events
2913 ASSERT_NO_FATAL_FAILURE(
2914 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2915 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2916 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2917 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2918 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2919 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2920 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2921
2922 ASSERT_NO_FATAL_FAILURE(
2923 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2924 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2925 AKEYCODE_DPAD_RIGHT, newDisplayId));
2926 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2927 AKEYCODE_DPAD_DOWN, newDisplayId));
2928 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2929 AKEYCODE_DPAD_LEFT, newDisplayId));
2930}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002932// --- KeyboardInputMapperTest_ExternalDevice ---
2933
2934class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2935protected:
2936 virtual void SetUp() override {
2937 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2938 }
2939};
2940
2941TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002942 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2943 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002944
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002945 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2946 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2947 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2948 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002950 KeyboardInputMapper& mapper =
2951 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2952 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002953
2954 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2955 NotifyKeyArgs args;
2956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2957 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2958
2959 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2961 ASSERT_EQ(uint32_t(0), args.policyFlags);
2962
2963 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2965 ASSERT_EQ(uint32_t(0), args.policyFlags);
2966
2967 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2969 ASSERT_EQ(uint32_t(0), args.policyFlags);
2970
2971 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2973 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2974
2975 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2977 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2978}
2979
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002980TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002981 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002982
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002983 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2984 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2985 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002986
Powei Fengd041c5d2019-05-03 17:11:33 -07002987 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002988 KeyboardInputMapper& mapper =
2989 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2990 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002991
2992 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2993 NotifyKeyArgs args;
2994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2996
2997 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2999 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3000
3001 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3003 ASSERT_EQ(uint32_t(0), args.policyFlags);
3004
3005 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3007 ASSERT_EQ(uint32_t(0), args.policyFlags);
3008
3009 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3011 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3012
3013 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3015 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3016}
3017
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018// --- CursorInputMapperTest ---
3019
3020class CursorInputMapperTest : public InputMapperTest {
3021protected:
3022 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3023
Michael Wright17db18e2020-06-26 20:51:44 +01003024 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025
Prabir Pradhan28efc192019-11-05 01:10:04 +00003026 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 InputMapperTest::SetUp();
3028
Michael Wright17db18e2020-06-26 20:51:44 +01003029 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003030 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 }
3032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003033 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3034 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003035
3036 void prepareDisplay(int32_t orientation) {
3037 const std::string uniqueId = "local:0";
3038 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
3039 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3040 orientation, uniqueId, NO_PORT, viewportType);
3041 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042};
3043
3044const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003046void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3047 int32_t originalY, int32_t rotatedX,
3048 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 NotifyMotionArgs args;
3050
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003051 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3052 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3053 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3057 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3058 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3059 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3060}
3061
3062TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003064 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003066 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067}
3068
3069TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003071 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003073 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074}
3075
3076TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003078 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079
3080 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082
3083 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003084 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3085 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3087 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3088
3089 // When the bounds are set, then there should be a valid motion range.
3090 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3091
3092 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003093 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
3095 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3096 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3097 1, 800 - 1, 0.0f, 0.0f));
3098 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3099 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3100 2, 480 - 1, 0.0f, 0.0f));
3101 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3102 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3103 0.0f, 1.0f, 0.0f, 0.0f));
3104}
3105
3106TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003108 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109
3110 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003111 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112
3113 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3114 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3115 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3116 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3117 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3118 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3119 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3120 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3121 0.0f, 1.0f, 0.0f, 0.0f));
3122}
3123
3124TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127
3128 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3129
3130 NotifyMotionArgs args;
3131
3132 // Button press.
3133 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003134 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3135 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3137 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3138 ASSERT_EQ(DEVICE_ID, args.deviceId);
3139 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3140 ASSERT_EQ(uint32_t(0), args.policyFlags);
3141 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3142 ASSERT_EQ(0, args.flags);
3143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3144 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3145 ASSERT_EQ(0, args.edgeFlags);
3146 ASSERT_EQ(uint32_t(1), args.pointerCount);
3147 ASSERT_EQ(0, args.pointerProperties[0].id);
3148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3150 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3151 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3152 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3153 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3154
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3156 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3157 ASSERT_EQ(DEVICE_ID, args.deviceId);
3158 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3159 ASSERT_EQ(uint32_t(0), args.policyFlags);
3160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3161 ASSERT_EQ(0, args.flags);
3162 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3163 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3164 ASSERT_EQ(0, args.edgeFlags);
3165 ASSERT_EQ(uint32_t(1), args.pointerCount);
3166 ASSERT_EQ(0, args.pointerProperties[0].id);
3167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3169 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3170 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3171 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3172 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3173
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003175 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3176 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3178 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3179 ASSERT_EQ(DEVICE_ID, args.deviceId);
3180 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3181 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3183 ASSERT_EQ(0, args.flags);
3184 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3185 ASSERT_EQ(0, args.buttonState);
3186 ASSERT_EQ(0, args.edgeFlags);
3187 ASSERT_EQ(uint32_t(1), args.pointerCount);
3188 ASSERT_EQ(0, args.pointerProperties[0].id);
3189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3191 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3192 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3193 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3194 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3195
3196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3197 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3198 ASSERT_EQ(DEVICE_ID, args.deviceId);
3199 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3200 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3202 ASSERT_EQ(0, args.flags);
3203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3204 ASSERT_EQ(0, args.buttonState);
3205 ASSERT_EQ(0, args.edgeFlags);
3206 ASSERT_EQ(uint32_t(1), args.pointerCount);
3207 ASSERT_EQ(0, args.pointerProperties[0].id);
3208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3210 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3211 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3212 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3213 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3214}
3215
3216TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003217 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003218 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219
3220 NotifyMotionArgs args;
3221
3222 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003223 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3224 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3228 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3229
3230 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003231 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3232 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3236 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3237}
3238
3239TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003241 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242
3243 NotifyMotionArgs args;
3244
3245 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003246 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3247 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3251 0.0f, 0.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(&args));
3254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3256 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3257
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003259 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3260 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3264 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3265
3266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3269 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3270}
3271
3272TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003274 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275
3276 NotifyMotionArgs args;
3277
3278 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003279 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3280 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3281 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3282 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3284 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3286 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3287 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3288
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3290 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3292 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3293 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3294
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003296 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3297 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3298 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3302 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3303 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3304
3305 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003306 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3307 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3311 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3312
3313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3316 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3317}
3318
3319TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003321 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003323 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3325 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3326 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3327 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3328 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3329 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3330 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3331 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3332}
3333
3334TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335 addConfigurationProperty("cursor.mode", "navigation");
3336 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003337 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003339 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3341 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3345 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3346 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3348
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003349 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3351 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3352 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3354 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3355 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3356 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3357 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3358
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003359 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3361 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3362 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3364 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3365 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3366 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3367 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3368
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003369 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3375 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3376 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3377 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3378}
3379
3380TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003382 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383
3384 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3385 mFakePointerController->setPosition(100, 200);
3386 mFakePointerController->setButtonState(0);
3387
3388 NotifyMotionArgs motionArgs;
3389 NotifyKeyArgs keyArgs;
3390
3391 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003392 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3393 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3395 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3396 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3397 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3399 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3400
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3403 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3404 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3406 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3407
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003408 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3409 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412 ASSERT_EQ(0, motionArgs.buttonState);
3413 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3415 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3416
3417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003418 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003419 ASSERT_EQ(0, motionArgs.buttonState);
3420 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3422 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3423
3424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003426 ASSERT_EQ(0, motionArgs.buttonState);
3427 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3429 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3430
3431 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003432 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3433 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3434 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3436 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3437 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3438 motionArgs.buttonState);
3439 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3440 mFakePointerController->getButtonState());
3441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3442 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3443
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3445 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3446 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3447 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3448 mFakePointerController->getButtonState());
3449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3450 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3451
3452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3453 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3454 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3455 motionArgs.buttonState);
3456 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3457 mFakePointerController->getButtonState());
3458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3459 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3460
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003461 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3462 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3466 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3468 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3469
3470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003472 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3473 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3475 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3476
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003477 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3478 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3481 ASSERT_EQ(0, motionArgs.buttonState);
3482 ASSERT_EQ(0, mFakePointerController->getButtonState());
3483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3484 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 -08003485 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3486 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003487
3488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489 ASSERT_EQ(0, motionArgs.buttonState);
3490 ASSERT_EQ(0, mFakePointerController->getButtonState());
3491 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3493 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 -08003494
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3496 ASSERT_EQ(0, motionArgs.buttonState);
3497 ASSERT_EQ(0, mFakePointerController->getButtonState());
3498 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3500 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3501
3502 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003503 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3504 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3506 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3507 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003508
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3512 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3514 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3515
3516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3518 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3519 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3521 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3522
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003523 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3524 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 ASSERT_EQ(0, motionArgs.buttonState);
3528 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3530 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3531
3532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003534 ASSERT_EQ(0, motionArgs.buttonState);
3535 ASSERT_EQ(0, mFakePointerController->getButtonState());
3536
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3538 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3540 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3541 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3542
3543 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003544 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3545 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3547 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3548 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003549
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3553 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3555 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3556
3557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3559 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3560 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3562 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3563
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003564 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3565 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003567 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568 ASSERT_EQ(0, motionArgs.buttonState);
3569 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3571 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 -08003572
3573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3575 ASSERT_EQ(0, motionArgs.buttonState);
3576 ASSERT_EQ(0, mFakePointerController->getButtonState());
3577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3578 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3579
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3581 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3582 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3583
3584 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003585 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3586 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3588 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3589 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003590
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3594 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3596 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3597
3598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3600 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3601 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3603 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3604
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003605 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3606 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003608 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_EQ(0, motionArgs.buttonState);
3610 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3612 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 -08003613
3614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3616 ASSERT_EQ(0, motionArgs.buttonState);
3617 ASSERT_EQ(0, mFakePointerController->getButtonState());
3618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3619 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3620
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3622 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3623 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3624
3625 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003626 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3627 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3629 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3630 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003631
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3635 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3637 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3638
3639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3641 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3642 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3644 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3645
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003646 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3647 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003649 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650 ASSERT_EQ(0, motionArgs.buttonState);
3651 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3653 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 -08003654
3655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3656 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3657 ASSERT_EQ(0, motionArgs.buttonState);
3658 ASSERT_EQ(0, mFakePointerController->getButtonState());
3659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3660 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3661
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3663 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3664 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3665}
3666
3667TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670
3671 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3672 mFakePointerController->setPosition(100, 200);
3673 mFakePointerController->setButtonState(0);
3674
3675 NotifyMotionArgs args;
3676
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003677 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3678 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3679 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003681 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3682 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3684 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003685 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003686}
3687
3688TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003689 addConfigurationProperty("cursor.mode", "pointer");
3690 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003691 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003692
3693 NotifyDeviceResetArgs resetArgs;
3694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3695 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3696 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3697
3698 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3699 mFakePointerController->setPosition(100, 200);
3700 mFakePointerController->setButtonState(0);
3701
3702 NotifyMotionArgs args;
3703
3704 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003705 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3706 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3707 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3709 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3712 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003713 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003714
3715 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003716 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3717 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3719 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3720 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3722 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3724 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3727 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3728
3729 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003730 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3731 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3733 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3734 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3736 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3738 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3739 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3741 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3742
3743 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003744 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3745 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3746 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3748 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3751 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003752 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003753
3754 // Disable pointer capture and check that the device generation got bumped
3755 // and events are generated the usual way.
3756 const uint32_t generation = mFakeContext->getGeneration();
3757 mFakePolicy->setPointerCapture(false);
3758 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3759 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3760
3761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3762 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3763 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3764
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003765 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3766 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3767 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3769 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3772 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003773 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003774}
3775
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003776TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003777 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003778
Garfield Tan888a6a42020-01-09 11:39:16 -08003779 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003780 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003781 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3782 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3783 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3784 ViewportType::VIEWPORT_EXTERNAL);
3785 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3786 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3787
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003788 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3789 mFakePointerController->setPosition(100, 200);
3790 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003791
3792 NotifyMotionArgs args;
3793 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3794 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3795 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3797 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3798 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3800 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003801 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003802 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3803}
3804
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805// --- TouchInputMapperTest ---
3806
3807class TouchInputMapperTest : public InputMapperTest {
3808protected:
3809 static const int32_t RAW_X_MIN;
3810 static const int32_t RAW_X_MAX;
3811 static const int32_t RAW_Y_MIN;
3812 static const int32_t RAW_Y_MAX;
3813 static const int32_t RAW_TOUCH_MIN;
3814 static const int32_t RAW_TOUCH_MAX;
3815 static const int32_t RAW_TOOL_MIN;
3816 static const int32_t RAW_TOOL_MAX;
3817 static const int32_t RAW_PRESSURE_MIN;
3818 static const int32_t RAW_PRESSURE_MAX;
3819 static const int32_t RAW_ORIENTATION_MIN;
3820 static const int32_t RAW_ORIENTATION_MAX;
3821 static const int32_t RAW_DISTANCE_MIN;
3822 static const int32_t RAW_DISTANCE_MAX;
3823 static const int32_t RAW_TILT_MIN;
3824 static const int32_t RAW_TILT_MAX;
3825 static const int32_t RAW_ID_MIN;
3826 static const int32_t RAW_ID_MAX;
3827 static const int32_t RAW_SLOT_MIN;
3828 static const int32_t RAW_SLOT_MAX;
3829 static const float X_PRECISION;
3830 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003831 static const float X_PRECISION_VIRTUAL;
3832 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003833
3834 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003835 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836
3837 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3838
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003839 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003840 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003841
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842 enum Axes {
3843 POSITION = 1 << 0,
3844 TOUCH = 1 << 1,
3845 TOOL = 1 << 2,
3846 PRESSURE = 1 << 3,
3847 ORIENTATION = 1 << 4,
3848 MINOR = 1 << 5,
3849 ID = 1 << 6,
3850 DISTANCE = 1 << 7,
3851 TILT = 1 << 8,
3852 SLOT = 1 << 9,
3853 TOOL_TYPE = 1 << 10,
3854 };
3855
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003856 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3857 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003858 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003860 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861 int32_t toRawX(float displayX);
3862 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003863 float toCookedX(float rawX, float rawY);
3864 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003866 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003868 float toDisplayY(int32_t rawY, int32_t displayHeight);
3869
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870};
3871
3872const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3873const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3874const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3875const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3876const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3877const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3878const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3879const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003880const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3881const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3883const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3884const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3885const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3886const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3887const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3888const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3889const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3890const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3891const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3892const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3893const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003894const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3895 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3896const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3897 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003898const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3899 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900
3901const float TouchInputMapperTest::GEOMETRIC_SCALE =
3902 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3903 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3904
3905const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3906 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3907 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3908};
3909
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003910void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003911 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003912 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3913}
3914
3915void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3916 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3917 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918}
3919
Santos Cordonfa5cf462017-04-05 10:37:00 -07003920void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003921 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3922 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003923 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003924}
3925
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003927 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3928 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3929 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3930 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931}
3932
Jason Gerecke489fda82012-09-07 17:19:40 -07003933void TouchInputMapperTest::prepareLocationCalibration() {
3934 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3935}
3936
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937int32_t TouchInputMapperTest::toRawX(float displayX) {
3938 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3939}
3940
3941int32_t TouchInputMapperTest::toRawY(float displayY) {
3942 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3943}
3944
Jason Gerecke489fda82012-09-07 17:19:40 -07003945float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3946 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3947 return rawX;
3948}
3949
3950float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3951 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3952 return rawY;
3953}
3954
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003956 return toDisplayX(rawX, DISPLAY_WIDTH);
3957}
3958
3959float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3960 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961}
3962
3963float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003964 return toDisplayY(rawY, DISPLAY_HEIGHT);
3965}
3966
3967float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3968 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969}
3970
3971
3972// --- SingleTouchInputMapperTest ---
3973
3974class SingleTouchInputMapperTest : public TouchInputMapperTest {
3975protected:
3976 void prepareButtons();
3977 void prepareAxes(int axes);
3978
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003979 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3980 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3981 void processUp(SingleTouchInputMapper& mappery);
3982 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3983 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3984 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3985 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3986 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3987 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988};
3989
3990void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003991 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992}
3993
3994void SingleTouchInputMapperTest::prepareAxes(int axes) {
3995 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
3997 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998 }
3999 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004000 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4001 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002 }
4003 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4005 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006 }
4007 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004008 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4009 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010 }
4011 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004012 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4013 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014 }
4015}
4016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004017void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004018 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4019 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4020 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021}
4022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004023void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004024 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4025 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026}
4027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004028void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004029 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030}
4031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004032void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004033 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034}
4035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004036void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4037 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004038 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039}
4040
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004041void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004042 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043}
4044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004045void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4046 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004047 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4048 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049}
4050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004051void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4052 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004053 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054}
4055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004056void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004057 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058}
4059
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061 prepareButtons();
4062 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004063 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004065 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066}
4067
4068TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004069 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4070 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071 prepareButtons();
4072 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004073 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004075 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076}
4077
4078TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079 prepareButtons();
4080 prepareAxes(POSITION);
4081 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004082 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004084 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085}
4086
4087TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088 prepareButtons();
4089 prepareAxes(POSITION);
4090 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004091 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004093 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094}
4095
4096TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 addConfigurationProperty("touch.deviceType", "touchScreen");
4098 prepareDisplay(DISPLAY_ORIENTATION_0);
4099 prepareButtons();
4100 prepareAxes(POSITION);
4101 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004102 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103
4104 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004105 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106
4107 // Virtual key is down.
4108 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4109 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4110 processDown(mapper, x, y);
4111 processSync(mapper);
4112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004114 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115
4116 // Virtual key is up.
4117 processUp(mapper);
4118 processSync(mapper);
4119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004121 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122}
4123
4124TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 addConfigurationProperty("touch.deviceType", "touchScreen");
4126 prepareDisplay(DISPLAY_ORIENTATION_0);
4127 prepareButtons();
4128 prepareAxes(POSITION);
4129 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004130 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131
4132 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004133 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134
4135 // Virtual key is down.
4136 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4137 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4138 processDown(mapper, x, y);
4139 processSync(mapper);
4140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004142 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143
4144 // Virtual key is up.
4145 processUp(mapper);
4146 processSync(mapper);
4147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004149 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150}
4151
4152TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153 addConfigurationProperty("touch.deviceType", "touchScreen");
4154 prepareDisplay(DISPLAY_ORIENTATION_0);
4155 prepareButtons();
4156 prepareAxes(POSITION);
4157 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004158 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159
4160 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4161 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004162 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163 ASSERT_TRUE(flags[0]);
4164 ASSERT_FALSE(flags[1]);
4165}
4166
4167TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 addConfigurationProperty("touch.deviceType", "touchScreen");
4169 prepareDisplay(DISPLAY_ORIENTATION_0);
4170 prepareButtons();
4171 prepareAxes(POSITION);
4172 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004173 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174
4175 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4176
4177 NotifyKeyArgs args;
4178
4179 // Press virtual key.
4180 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4181 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4182 processDown(mapper, x, y);
4183 processSync(mapper);
4184
4185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4186 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4187 ASSERT_EQ(DEVICE_ID, args.deviceId);
4188 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4189 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4190 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4191 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4192 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4193 ASSERT_EQ(KEY_HOME, args.scanCode);
4194 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4195 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4196
4197 // Release virtual key.
4198 processUp(mapper);
4199 processSync(mapper);
4200
4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4202 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4203 ASSERT_EQ(DEVICE_ID, args.deviceId);
4204 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4205 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4206 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4207 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4208 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4209 ASSERT_EQ(KEY_HOME, args.scanCode);
4210 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4211 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4212
4213 // Should not have sent any motions.
4214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4215}
4216
4217TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 addConfigurationProperty("touch.deviceType", "touchScreen");
4219 prepareDisplay(DISPLAY_ORIENTATION_0);
4220 prepareButtons();
4221 prepareAxes(POSITION);
4222 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224
4225 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4226
4227 NotifyKeyArgs keyArgs;
4228
4229 // Press virtual key.
4230 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4231 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4232 processDown(mapper, x, y);
4233 processSync(mapper);
4234
4235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4236 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4237 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4238 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4239 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4240 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4241 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4242 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4243 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4244 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4245 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4246
4247 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4248 // into the display area.
4249 y -= 100;
4250 processMove(mapper, x, y);
4251 processSync(mapper);
4252
4253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4254 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4255 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4256 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4257 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4258 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4259 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4260 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4261 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4262 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4263 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4264 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4265
4266 NotifyMotionArgs motionArgs;
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(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4271 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4272 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4273 ASSERT_EQ(0, motionArgs.flags);
4274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4275 ASSERT_EQ(0, motionArgs.buttonState);
4276 ASSERT_EQ(0, motionArgs.edgeFlags);
4277 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4278 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4279 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4281 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4282 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4283 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4284 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4285
4286 // Keep moving out of bounds. Should generate a pointer move.
4287 y -= 50;
4288 processMove(mapper, x, y);
4289 processSync(mapper);
4290
4291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4293 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4294 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4295 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4297 ASSERT_EQ(0, motionArgs.flags);
4298 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4299 ASSERT_EQ(0, motionArgs.buttonState);
4300 ASSERT_EQ(0, motionArgs.edgeFlags);
4301 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4302 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4305 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4306 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4307 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4309
4310 // Release out of bounds. Should generate a pointer up.
4311 processUp(mapper);
4312 processSync(mapper);
4313
4314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4315 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4316 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4317 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4318 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4319 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4320 ASSERT_EQ(0, motionArgs.flags);
4321 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4322 ASSERT_EQ(0, motionArgs.buttonState);
4323 ASSERT_EQ(0, motionArgs.edgeFlags);
4324 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4325 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4328 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4329 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4330 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4331 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4332
4333 // Should not have sent any more keys or motions.
4334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4336}
4337
4338TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 addConfigurationProperty("touch.deviceType", "touchScreen");
4340 prepareDisplay(DISPLAY_ORIENTATION_0);
4341 prepareButtons();
4342 prepareAxes(POSITION);
4343 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004344 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345
4346 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4347
4348 NotifyMotionArgs motionArgs;
4349
4350 // Initially go down out of bounds.
4351 int32_t x = -10;
4352 int32_t y = -10;
4353 processDown(mapper, x, y);
4354 processSync(mapper);
4355
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4357
4358 // Move into the display area. Should generate a pointer down.
4359 x = 50;
4360 y = 75;
4361 processMove(mapper, x, y);
4362 processSync(mapper);
4363
4364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4366 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4367 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4368 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4369 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4370 ASSERT_EQ(0, motionArgs.flags);
4371 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4372 ASSERT_EQ(0, motionArgs.buttonState);
4373 ASSERT_EQ(0, motionArgs.edgeFlags);
4374 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4375 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4378 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4379 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4380 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4381 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4382
4383 // Release. Should generate a pointer up.
4384 processUp(mapper);
4385 processSync(mapper);
4386
4387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4388 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4389 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4390 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4391 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4392 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4393 ASSERT_EQ(0, motionArgs.flags);
4394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4395 ASSERT_EQ(0, motionArgs.buttonState);
4396 ASSERT_EQ(0, motionArgs.edgeFlags);
4397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4398 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4401 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4402 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4403 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4405
4406 // Should not have sent any more keys or motions.
4407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4409}
4410
Santos Cordonfa5cf462017-04-05 10:37:00 -07004411TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004412 addConfigurationProperty("touch.deviceType", "touchScreen");
4413 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4414
4415 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4416 prepareButtons();
4417 prepareAxes(POSITION);
4418 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004419 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004420
4421 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4422
4423 NotifyMotionArgs motionArgs;
4424
4425 // Down.
4426 int32_t x = 100;
4427 int32_t y = 125;
4428 processDown(mapper, x, y);
4429 processSync(mapper);
4430
4431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4432 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4433 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4434 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4435 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4436 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4437 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4438 ASSERT_EQ(0, motionArgs.flags);
4439 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4440 ASSERT_EQ(0, motionArgs.buttonState);
4441 ASSERT_EQ(0, motionArgs.edgeFlags);
4442 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4443 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4446 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4447 1, 0, 0, 0, 0, 0, 0, 0));
4448 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4449 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4451
4452 // Move.
4453 x += 50;
4454 y += 75;
4455 processMove(mapper, x, y);
4456 processSync(mapper);
4457
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4460 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4461 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4462 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4463 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4465 ASSERT_EQ(0, motionArgs.flags);
4466 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4467 ASSERT_EQ(0, motionArgs.buttonState);
4468 ASSERT_EQ(0, motionArgs.edgeFlags);
4469 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4470 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4473 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4474 1, 0, 0, 0, 0, 0, 0, 0));
4475 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4476 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4478
4479 // Up.
4480 processUp(mapper);
4481 processSync(mapper);
4482
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4485 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4486 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4487 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4488 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4489 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4490 ASSERT_EQ(0, motionArgs.flags);
4491 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4492 ASSERT_EQ(0, motionArgs.buttonState);
4493 ASSERT_EQ(0, motionArgs.edgeFlags);
4494 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4495 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4498 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4499 1, 0, 0, 0, 0, 0, 0, 0));
4500 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4501 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4502 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4503
4504 // Should not have sent any more keys or motions.
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4507}
4508
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 addConfigurationProperty("touch.deviceType", "touchScreen");
4511 prepareDisplay(DISPLAY_ORIENTATION_0);
4512 prepareButtons();
4513 prepareAxes(POSITION);
4514 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004515 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516
4517 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4518
4519 NotifyMotionArgs motionArgs;
4520
4521 // Down.
4522 int32_t x = 100;
4523 int32_t y = 125;
4524 processDown(mapper, x, y);
4525 processSync(mapper);
4526
4527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4528 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4529 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4530 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4531 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4532 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4533 ASSERT_EQ(0, motionArgs.flags);
4534 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4535 ASSERT_EQ(0, motionArgs.buttonState);
4536 ASSERT_EQ(0, motionArgs.edgeFlags);
4537 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4538 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4541 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4542 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4543 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4545
4546 // Move.
4547 x += 50;
4548 y += 75;
4549 processMove(mapper, x, y);
4550 processSync(mapper);
4551
4552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4553 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4554 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4555 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4556 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4558 ASSERT_EQ(0, motionArgs.flags);
4559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4560 ASSERT_EQ(0, motionArgs.buttonState);
4561 ASSERT_EQ(0, motionArgs.edgeFlags);
4562 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4563 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4566 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4567 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4568 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4570
4571 // Up.
4572 processUp(mapper);
4573 processSync(mapper);
4574
4575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4576 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4577 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4578 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4579 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4580 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4581 ASSERT_EQ(0, motionArgs.flags);
4582 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4583 ASSERT_EQ(0, motionArgs.buttonState);
4584 ASSERT_EQ(0, motionArgs.edgeFlags);
4585 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4586 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4589 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4590 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4591 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4592 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4593
4594 // Should not have sent any more keys or motions.
4595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4597}
4598
4599TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600 addConfigurationProperty("touch.deviceType", "touchScreen");
4601 prepareButtons();
4602 prepareAxes(POSITION);
4603 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004604 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605
4606 NotifyMotionArgs args;
4607
4608 // Rotation 90.
4609 prepareDisplay(DISPLAY_ORIENTATION_90);
4610 processDown(mapper, toRawX(50), toRawY(75));
4611 processSync(mapper);
4612
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4614 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4615 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4616
4617 processUp(mapper);
4618 processSync(mapper);
4619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4620}
4621
4622TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623 addConfigurationProperty("touch.deviceType", "touchScreen");
4624 prepareButtons();
4625 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004626 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627
4628 NotifyMotionArgs args;
4629
4630 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004631 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 prepareDisplay(DISPLAY_ORIENTATION_0);
4633 processDown(mapper, toRawX(50), toRawY(75));
4634 processSync(mapper);
4635
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4637 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4638 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4639
4640 processUp(mapper);
4641 processSync(mapper);
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4643
4644 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004645 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646 prepareDisplay(DISPLAY_ORIENTATION_90);
4647 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4648 processSync(mapper);
4649
4650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4651 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4652 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4653
4654 processUp(mapper);
4655 processSync(mapper);
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4657
4658 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004659 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 prepareDisplay(DISPLAY_ORIENTATION_180);
4661 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4662 processSync(mapper);
4663
4664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4665 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4666 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4667
4668 processUp(mapper);
4669 processSync(mapper);
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4671
4672 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004673 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 prepareDisplay(DISPLAY_ORIENTATION_270);
4675 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4676 processSync(mapper);
4677
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4679 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4680 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4681
4682 processUp(mapper);
4683 processSync(mapper);
4684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4685}
4686
4687TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688 addConfigurationProperty("touch.deviceType", "touchScreen");
4689 prepareDisplay(DISPLAY_ORIENTATION_0);
4690 prepareButtons();
4691 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004692 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693
4694 // These calculations are based on the input device calibration documentation.
4695 int32_t rawX = 100;
4696 int32_t rawY = 200;
4697 int32_t rawPressure = 10;
4698 int32_t rawToolMajor = 12;
4699 int32_t rawDistance = 2;
4700 int32_t rawTiltX = 30;
4701 int32_t rawTiltY = 110;
4702
4703 float x = toDisplayX(rawX);
4704 float y = toDisplayY(rawY);
4705 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4706 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4707 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4708 float distance = float(rawDistance);
4709
4710 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4711 float tiltScale = M_PI / 180;
4712 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4713 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4714 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4715 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4716
4717 processDown(mapper, rawX, rawY);
4718 processPressure(mapper, rawPressure);
4719 processToolMajor(mapper, rawToolMajor);
4720 processDistance(mapper, rawDistance);
4721 processTilt(mapper, rawTiltX, rawTiltY);
4722 processSync(mapper);
4723
4724 NotifyMotionArgs args;
4725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4727 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4728 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4729}
4730
Jason Gerecke489fda82012-09-07 17:19:40 -07004731TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004732 addConfigurationProperty("touch.deviceType", "touchScreen");
4733 prepareDisplay(DISPLAY_ORIENTATION_0);
4734 prepareLocationCalibration();
4735 prepareButtons();
4736 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004737 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004738
4739 int32_t rawX = 100;
4740 int32_t rawY = 200;
4741
4742 float x = toDisplayX(toCookedX(rawX, rawY));
4743 float y = toDisplayY(toCookedY(rawX, rawY));
4744
4745 processDown(mapper, rawX, rawY);
4746 processSync(mapper);
4747
4748 NotifyMotionArgs args;
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4751 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4752}
4753
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 addConfigurationProperty("touch.deviceType", "touchScreen");
4756 prepareDisplay(DISPLAY_ORIENTATION_0);
4757 prepareButtons();
4758 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004759 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760
4761 NotifyMotionArgs motionArgs;
4762 NotifyKeyArgs keyArgs;
4763
4764 processDown(mapper, 100, 200);
4765 processSync(mapper);
4766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4767 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4768 ASSERT_EQ(0, motionArgs.buttonState);
4769
4770 // press BTN_LEFT, release BTN_LEFT
4771 processKey(mapper, BTN_LEFT, 1);
4772 processSync(mapper);
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4775 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4776
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4778 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4779 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4780
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 processKey(mapper, BTN_LEFT, 0);
4782 processSync(mapper);
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004784 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004786
4787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004789 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790
4791 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4792 processKey(mapper, BTN_RIGHT, 1);
4793 processKey(mapper, BTN_MIDDLE, 1);
4794 processSync(mapper);
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4797 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4798 motionArgs.buttonState);
4799
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4802 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4803
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4805 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4806 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4807 motionArgs.buttonState);
4808
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809 processKey(mapper, BTN_RIGHT, 0);
4810 processSync(mapper);
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004812 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004817 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818
4819 processKey(mapper, BTN_MIDDLE, 0);
4820 processSync(mapper);
4821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004824
4825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004827 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828
4829 // press BTN_BACK, release BTN_BACK
4830 processKey(mapper, BTN_BACK, 1);
4831 processSync(mapper);
4832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4833 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4834 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004835
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004838 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4839
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4842 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843
4844 processKey(mapper, BTN_BACK, 0);
4845 processSync(mapper);
4846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004847 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004849
4850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004852 ASSERT_EQ(0, motionArgs.buttonState);
4853
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4855 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4856 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4857
4858 // press BTN_SIDE, release BTN_SIDE
4859 processKey(mapper, BTN_SIDE, 1);
4860 processSync(mapper);
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4862 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4863 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004864
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004867 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4868
4869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4870 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4871 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872
4873 processKey(mapper, BTN_SIDE, 0);
4874 processSync(mapper);
4875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004876 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004878
4879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004881 ASSERT_EQ(0, motionArgs.buttonState);
4882
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4884 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4885 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4886
4887 // press BTN_FORWARD, release BTN_FORWARD
4888 processKey(mapper, BTN_FORWARD, 1);
4889 processSync(mapper);
4890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4891 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4892 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004893
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004896 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4897
4898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4899 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4900 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901
4902 processKey(mapper, BTN_FORWARD, 0);
4903 processSync(mapper);
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004905 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004907
4908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004910 ASSERT_EQ(0, motionArgs.buttonState);
4911
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4913 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4914 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4915
4916 // press BTN_EXTRA, release BTN_EXTRA
4917 processKey(mapper, BTN_EXTRA, 1);
4918 processSync(mapper);
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4920 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4921 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004922
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004925 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4926
4927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4928 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4929 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930
4931 processKey(mapper, BTN_EXTRA, 0);
4932 processSync(mapper);
4933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004936
4937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004939 ASSERT_EQ(0, motionArgs.buttonState);
4940
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4942 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4943 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4944
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4946
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947 // press BTN_STYLUS, release BTN_STYLUS
4948 processKey(mapper, BTN_STYLUS, 1);
4949 processSync(mapper);
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004952 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4953
4954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4956 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957
4958 processKey(mapper, BTN_STYLUS, 0);
4959 processSync(mapper);
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004963
4964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004966 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967
4968 // press BTN_STYLUS2, release BTN_STYLUS2
4969 processKey(mapper, BTN_STYLUS2, 1);
4970 processSync(mapper);
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004973 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4974
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4976 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4977 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978
4979 processKey(mapper, BTN_STYLUS2, 0);
4980 processSync(mapper);
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004982 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004984
4985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004987 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988
4989 // release touch
4990 processUp(mapper);
4991 processSync(mapper);
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4993 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4994 ASSERT_EQ(0, motionArgs.buttonState);
4995}
4996
4997TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998 addConfigurationProperty("touch.deviceType", "touchScreen");
4999 prepareDisplay(DISPLAY_ORIENTATION_0);
5000 prepareButtons();
5001 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005002 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003
5004 NotifyMotionArgs motionArgs;
5005
5006 // default tool type is finger
5007 processDown(mapper, 100, 200);
5008 processSync(mapper);
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5010 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5012
5013 // eraser
5014 processKey(mapper, BTN_TOOL_RUBBER, 1);
5015 processSync(mapper);
5016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5019
5020 // stylus
5021 processKey(mapper, BTN_TOOL_RUBBER, 0);
5022 processKey(mapper, BTN_TOOL_PEN, 1);
5023 processSync(mapper);
5024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5026 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5027
5028 // brush
5029 processKey(mapper, BTN_TOOL_PEN, 0);
5030 processKey(mapper, BTN_TOOL_BRUSH, 1);
5031 processSync(mapper);
5032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5035
5036 // pencil
5037 processKey(mapper, BTN_TOOL_BRUSH, 0);
5038 processKey(mapper, BTN_TOOL_PENCIL, 1);
5039 processSync(mapper);
5040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5043
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005044 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045 processKey(mapper, BTN_TOOL_PENCIL, 0);
5046 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5047 processSync(mapper);
5048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5051
5052 // mouse
5053 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5054 processKey(mapper, BTN_TOOL_MOUSE, 1);
5055 processSync(mapper);
5056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5059
5060 // lens
5061 processKey(mapper, BTN_TOOL_MOUSE, 0);
5062 processKey(mapper, BTN_TOOL_LENS, 1);
5063 processSync(mapper);
5064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5067
5068 // double-tap
5069 processKey(mapper, BTN_TOOL_LENS, 0);
5070 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5071 processSync(mapper);
5072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5075
5076 // triple-tap
5077 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5078 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5079 processSync(mapper);
5080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5083
5084 // quad-tap
5085 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5086 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5087 processSync(mapper);
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5091
5092 // finger
5093 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5094 processKey(mapper, BTN_TOOL_FINGER, 1);
5095 processSync(mapper);
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5099
5100 // stylus trumps finger
5101 processKey(mapper, BTN_TOOL_PEN, 1);
5102 processSync(mapper);
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5106
5107 // eraser trumps stylus
5108 processKey(mapper, BTN_TOOL_RUBBER, 1);
5109 processSync(mapper);
5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5113
5114 // mouse trumps eraser
5115 processKey(mapper, BTN_TOOL_MOUSE, 1);
5116 processSync(mapper);
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5120
5121 // back to default tool type
5122 processKey(mapper, BTN_TOOL_MOUSE, 0);
5123 processKey(mapper, BTN_TOOL_RUBBER, 0);
5124 processKey(mapper, BTN_TOOL_PEN, 0);
5125 processKey(mapper, BTN_TOOL_FINGER, 0);
5126 processSync(mapper);
5127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5130}
5131
5132TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005133 addConfigurationProperty("touch.deviceType", "touchScreen");
5134 prepareDisplay(DISPLAY_ORIENTATION_0);
5135 prepareButtons();
5136 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005137 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005138 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139
5140 NotifyMotionArgs motionArgs;
5141
5142 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5143 processKey(mapper, BTN_TOOL_FINGER, 1);
5144 processMove(mapper, 100, 200);
5145 processSync(mapper);
5146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5147 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5149 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5150
5151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5152 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5154 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5155
5156 // move a little
5157 processMove(mapper, 150, 250);
5158 processSync(mapper);
5159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5160 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5162 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5163
5164 // down when BTN_TOUCH is pressed, pressure defaults to 1
5165 processKey(mapper, BTN_TOUCH, 1);
5166 processSync(mapper);
5167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5168 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5170 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5171
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5173 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5175 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5176
5177 // up when BTN_TOUCH is released, hover restored
5178 processKey(mapper, BTN_TOUCH, 0);
5179 processSync(mapper);
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5181 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5183 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5184
5185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5186 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5188 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5189
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5191 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5193 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5194
5195 // exit hover when pointer goes away
5196 processKey(mapper, BTN_TOOL_FINGER, 0);
5197 processSync(mapper);
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5199 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5201 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5202}
5203
5204TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205 addConfigurationProperty("touch.deviceType", "touchScreen");
5206 prepareDisplay(DISPLAY_ORIENTATION_0);
5207 prepareButtons();
5208 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005209 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210
5211 NotifyMotionArgs motionArgs;
5212
5213 // initially hovering because pressure is 0
5214 processDown(mapper, 100, 200);
5215 processPressure(mapper, 0);
5216 processSync(mapper);
5217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5220 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5221
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5223 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5225 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5226
5227 // move a little
5228 processMove(mapper, 150, 250);
5229 processSync(mapper);
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5231 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5233 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5234
5235 // down when pressure is non-zero
5236 processPressure(mapper, RAW_PRESSURE_MAX);
5237 processSync(mapper);
5238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5239 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5241 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5242
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5244 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5246 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5247
5248 // up when pressure becomes 0, hover restored
5249 processPressure(mapper, 0);
5250 processSync(mapper);
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5254 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5255
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5257 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5259 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5260
5261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5262 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5264 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5265
5266 // exit hover when pointer goes away
5267 processUp(mapper);
5268 processSync(mapper);
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5270 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5272 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5273}
5274
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275// --- MultiTouchInputMapperTest ---
5276
5277class MultiTouchInputMapperTest : public TouchInputMapperTest {
5278protected:
5279 void prepareAxes(int axes);
5280
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005281 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5282 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5283 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5284 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5285 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5286 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5287 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5288 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5289 void processId(MultiTouchInputMapper& mapper, int32_t id);
5290 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5291 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5292 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5293 void processMTSync(MultiTouchInputMapper& mapper);
5294 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295};
5296
5297void MultiTouchInputMapperTest::prepareAxes(int axes) {
5298 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005299 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5300 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005301 }
5302 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005303 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5304 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005306 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5307 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308 }
5309 }
5310 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005311 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5312 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005314 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5315 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316 }
5317 }
5318 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005319 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5320 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321 }
5322 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005323 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5324 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325 }
5326 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005327 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5328 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329 }
5330 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5332 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 }
5334 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005335 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5336 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337 }
5338 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005339 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 }
5341}
5342
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005343void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5344 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005345 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5346 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347}
5348
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005349void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5350 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005351 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352}
5353
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005354void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5355 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005356 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357}
5358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005359void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005360 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361}
5362
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005363void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005364 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365}
5366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005367void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5368 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005369 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370}
5371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005372void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005373 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374}
5375
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005376void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005377 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005378}
5379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005380void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005381 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382}
5383
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005385 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386}
5387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005388void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005389 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390}
5391
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005392void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5393 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005394 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395}
5396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005397void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005398 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399}
5400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005401void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005402 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403}
5404
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 addConfigurationProperty("touch.deviceType", "touchScreen");
5407 prepareDisplay(DISPLAY_ORIENTATION_0);
5408 prepareAxes(POSITION);
5409 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005410 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411
5412 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5413
5414 NotifyMotionArgs motionArgs;
5415
5416 // Two fingers down at once.
5417 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5418 processPosition(mapper, x1, y1);
5419 processMTSync(mapper);
5420 processPosition(mapper, x2, y2);
5421 processMTSync(mapper);
5422 processSync(mapper);
5423
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5425 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5426 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5427 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5428 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5429 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5430 ASSERT_EQ(0, motionArgs.flags);
5431 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5432 ASSERT_EQ(0, motionArgs.buttonState);
5433 ASSERT_EQ(0, motionArgs.edgeFlags);
5434 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5435 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5438 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5439 ASSERT_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_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5449 motionArgs.action);
5450 ASSERT_EQ(0, motionArgs.flags);
5451 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5452 ASSERT_EQ(0, motionArgs.buttonState);
5453 ASSERT_EQ(0, motionArgs.edgeFlags);
5454 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5455 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5457 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5460 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5462 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5463 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5464 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5466
5467 // Move.
5468 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5469 processPosition(mapper, x1, y1);
5470 processMTSync(mapper);
5471 processPosition(mapper, x2, y2);
5472 processMTSync(mapper);
5473 processSync(mapper);
5474
5475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5476 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5477 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5478 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5479 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5481 ASSERT_EQ(0, motionArgs.flags);
5482 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5483 ASSERT_EQ(0, motionArgs.buttonState);
5484 ASSERT_EQ(0, motionArgs.edgeFlags);
5485 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5486 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5488 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5491 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5493 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5494 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5495 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5496 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5497
5498 // First finger up.
5499 x2 += 15; y2 -= 20;
5500 processPosition(mapper, x2, y2);
5501 processMTSync(mapper);
5502 processSync(mapper);
5503
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5505 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5506 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5507 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5508 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5509 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5510 motionArgs.action);
5511 ASSERT_EQ(0, motionArgs.flags);
5512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5513 ASSERT_EQ(0, motionArgs.buttonState);
5514 ASSERT_EQ(0, motionArgs.edgeFlags);
5515 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5516 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5518 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5521 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5523 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5524 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5525 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5526 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5527
5528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5529 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5530 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5531 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5532 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5534 ASSERT_EQ(0, motionArgs.flags);
5535 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5536 ASSERT_EQ(0, motionArgs.buttonState);
5537 ASSERT_EQ(0, motionArgs.edgeFlags);
5538 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5539 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5542 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5543 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5544 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5545 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5546
5547 // Move.
5548 x2 += 20; y2 -= 25;
5549 processPosition(mapper, x2, y2);
5550 processMTSync(mapper);
5551 processSync(mapper);
5552
5553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5554 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5555 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5556 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5557 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5559 ASSERT_EQ(0, motionArgs.flags);
5560 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5561 ASSERT_EQ(0, motionArgs.buttonState);
5562 ASSERT_EQ(0, motionArgs.edgeFlags);
5563 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5564 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5567 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5568 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5569 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5571
5572 // New finger down.
5573 int32_t x3 = 700, y3 = 300;
5574 processPosition(mapper, x2, y2);
5575 processMTSync(mapper);
5576 processPosition(mapper, x3, y3);
5577 processMTSync(mapper);
5578 processSync(mapper);
5579
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5582 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5583 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5584 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5585 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5586 motionArgs.action);
5587 ASSERT_EQ(0, motionArgs.flags);
5588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5589 ASSERT_EQ(0, motionArgs.buttonState);
5590 ASSERT_EQ(0, motionArgs.edgeFlags);
5591 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5592 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5594 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5597 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5599 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5600 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5601 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5602 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5603
5604 // Second finger up.
5605 x3 += 30; y3 -= 20;
5606 processPosition(mapper, x3, y3);
5607 processMTSync(mapper);
5608 processSync(mapper);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5612 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5613 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5614 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5616 motionArgs.action);
5617 ASSERT_EQ(0, motionArgs.flags);
5618 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5619 ASSERT_EQ(0, motionArgs.buttonState);
5620 ASSERT_EQ(0, motionArgs.edgeFlags);
5621 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5622 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5624 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5627 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5629 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5630 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5631 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5632 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5633
5634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5635 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5636 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5637 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5638 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5640 ASSERT_EQ(0, motionArgs.flags);
5641 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5642 ASSERT_EQ(0, motionArgs.buttonState);
5643 ASSERT_EQ(0, motionArgs.edgeFlags);
5644 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5645 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5648 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5649 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5650 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5651 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5652
5653 // Last finger up.
5654 processMTSync(mapper);
5655 processSync(mapper);
5656
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5659 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5660 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5661 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5662 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5663 ASSERT_EQ(0, motionArgs.flags);
5664 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5665 ASSERT_EQ(0, motionArgs.buttonState);
5666 ASSERT_EQ(0, motionArgs.edgeFlags);
5667 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5668 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5671 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5672 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5673 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5674 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5675
5676 // Should not have sent any more keys or motions.
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5679}
5680
5681TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682 addConfigurationProperty("touch.deviceType", "touchScreen");
5683 prepareDisplay(DISPLAY_ORIENTATION_0);
5684 prepareAxes(POSITION | ID);
5685 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005686 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005687
5688 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5689
5690 NotifyMotionArgs motionArgs;
5691
5692 // Two fingers down at once.
5693 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5694 processPosition(mapper, x1, y1);
5695 processId(mapper, 1);
5696 processMTSync(mapper);
5697 processPosition(mapper, x2, y2);
5698 processId(mapper, 2);
5699 processMTSync(mapper);
5700 processSync(mapper);
5701
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5703 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5704 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5705 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5708 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5712 motionArgs.action);
5713 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5714 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5716 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5719 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5721 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5722
5723 // Move.
5724 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5725 processPosition(mapper, x1, y1);
5726 processId(mapper, 1);
5727 processMTSync(mapper);
5728 processPosition(mapper, x2, y2);
5729 processId(mapper, 2);
5730 processMTSync(mapper);
5731 processSync(mapper);
5732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5735 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5736 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5738 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5741 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5743 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5744
5745 // First finger up.
5746 x2 += 15; y2 -= 20;
5747 processPosition(mapper, x2, y2);
5748 processId(mapper, 2);
5749 processMTSync(mapper);
5750 processSync(mapper);
5751
5752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5753 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5754 motionArgs.action);
5755 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5756 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5758 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5761 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5763 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5764
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5767 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5768 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5771 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5772
5773 // Move.
5774 x2 += 20; y2 -= 25;
5775 processPosition(mapper, x2, y2);
5776 processId(mapper, 2);
5777 processMTSync(mapper);
5778 processSync(mapper);
5779
5780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5782 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5783 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5786 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5787
5788 // New finger down.
5789 int32_t x3 = 700, y3 = 300;
5790 processPosition(mapper, x2, y2);
5791 processId(mapper, 2);
5792 processMTSync(mapper);
5793 processPosition(mapper, x3, y3);
5794 processId(mapper, 3);
5795 processMTSync(mapper);
5796 processSync(mapper);
5797
5798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5799 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5800 motionArgs.action);
5801 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5802 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5804 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5807 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5809 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5810
5811 // Second finger up.
5812 x3 += 30; y3 -= 20;
5813 processPosition(mapper, x3, y3);
5814 processId(mapper, 3);
5815 processMTSync(mapper);
5816 processSync(mapper);
5817
5818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5819 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5820 motionArgs.action);
5821 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5822 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5824 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5827 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5829 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5830
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5833 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5834 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5837 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5838
5839 // Last finger up.
5840 processMTSync(mapper);
5841 processSync(mapper);
5842
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5844 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5845 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5846 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5849 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5850
5851 // Should not have sent any more keys or motions.
5852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5854}
5855
5856TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857 addConfigurationProperty("touch.deviceType", "touchScreen");
5858 prepareDisplay(DISPLAY_ORIENTATION_0);
5859 prepareAxes(POSITION | ID | SLOT);
5860 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005861 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862
5863 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5864
5865 NotifyMotionArgs motionArgs;
5866
5867 // Two fingers down at once.
5868 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5869 processPosition(mapper, x1, y1);
5870 processId(mapper, 1);
5871 processSlot(mapper, 1);
5872 processPosition(mapper, x2, y2);
5873 processId(mapper, 2);
5874 processSync(mapper);
5875
5876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5877 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5878 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5879 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5882 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5883
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5885 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5886 motionArgs.action);
5887 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5888 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5890 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5893 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5895 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5896
5897 // Move.
5898 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5899 processSlot(mapper, 0);
5900 processPosition(mapper, x1, y1);
5901 processSlot(mapper, 1);
5902 processPosition(mapper, x2, y2);
5903 processSync(mapper);
5904
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5907 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5908 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5910 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5913 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5915 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5916
5917 // First finger up.
5918 x2 += 15; y2 -= 20;
5919 processSlot(mapper, 0);
5920 processId(mapper, -1);
5921 processSlot(mapper, 1);
5922 processPosition(mapper, x2, y2);
5923 processSync(mapper);
5924
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5927 motionArgs.action);
5928 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5929 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5931 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5934 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5936 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5937
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5940 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5941 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5942 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5944 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5945
5946 // Move.
5947 x2 += 20; y2 -= 25;
5948 processPosition(mapper, x2, y2);
5949 processSync(mapper);
5950
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5953 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5954 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5957 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5958
5959 // New finger down.
5960 int32_t x3 = 700, y3 = 300;
5961 processPosition(mapper, x2, y2);
5962 processSlot(mapper, 0);
5963 processId(mapper, 3);
5964 processPosition(mapper, x3, y3);
5965 processSync(mapper);
5966
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5968 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5969 motionArgs.action);
5970 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5971 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5972 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5973 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5976 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5978 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5979
5980 // Second finger up.
5981 x3 += 30; y3 -= 20;
5982 processSlot(mapper, 1);
5983 processId(mapper, -1);
5984 processSlot(mapper, 0);
5985 processPosition(mapper, x3, y3);
5986 processSync(mapper);
5987
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5990 motionArgs.action);
5991 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5992 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5994 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5997 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5999 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6000
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6003 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6004 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6007 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6008
6009 // Last finger up.
6010 processId(mapper, -1);
6011 processSync(mapper);
6012
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6014 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6015 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6016 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6019 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6020
6021 // Should not have sent any more keys or motions.
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6024}
6025
6026TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006027 addConfigurationProperty("touch.deviceType", "touchScreen");
6028 prepareDisplay(DISPLAY_ORIENTATION_0);
6029 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006030 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031
6032 // These calculations are based on the input device calibration documentation.
6033 int32_t rawX = 100;
6034 int32_t rawY = 200;
6035 int32_t rawTouchMajor = 7;
6036 int32_t rawTouchMinor = 6;
6037 int32_t rawToolMajor = 9;
6038 int32_t rawToolMinor = 8;
6039 int32_t rawPressure = 11;
6040 int32_t rawDistance = 0;
6041 int32_t rawOrientation = 3;
6042 int32_t id = 5;
6043
6044 float x = toDisplayX(rawX);
6045 float y = toDisplayY(rawY);
6046 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6047 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6048 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6049 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6050 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6051 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6052 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6053 float distance = float(rawDistance);
6054
6055 processPosition(mapper, rawX, rawY);
6056 processTouchMajor(mapper, rawTouchMajor);
6057 processTouchMinor(mapper, rawTouchMinor);
6058 processToolMajor(mapper, rawToolMajor);
6059 processToolMinor(mapper, rawToolMinor);
6060 processPressure(mapper, rawPressure);
6061 processOrientation(mapper, rawOrientation);
6062 processDistance(mapper, rawDistance);
6063 processId(mapper, id);
6064 processMTSync(mapper);
6065 processSync(mapper);
6066
6067 NotifyMotionArgs args;
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6069 ASSERT_EQ(0, args.pointerProperties[0].id);
6070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6071 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6072 orientation, distance));
6073}
6074
6075TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 addConfigurationProperty("touch.deviceType", "touchScreen");
6077 prepareDisplay(DISPLAY_ORIENTATION_0);
6078 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6079 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006080 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081
6082 // These calculations are based on the input device calibration documentation.
6083 int32_t rawX = 100;
6084 int32_t rawY = 200;
6085 int32_t rawTouchMajor = 140;
6086 int32_t rawTouchMinor = 120;
6087 int32_t rawToolMajor = 180;
6088 int32_t rawToolMinor = 160;
6089
6090 float x = toDisplayX(rawX);
6091 float y = toDisplayY(rawY);
6092 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6093 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6094 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6095 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6096 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6097
6098 processPosition(mapper, rawX, rawY);
6099 processTouchMajor(mapper, rawTouchMajor);
6100 processTouchMinor(mapper, rawTouchMinor);
6101 processToolMajor(mapper, rawToolMajor);
6102 processToolMinor(mapper, rawToolMinor);
6103 processMTSync(mapper);
6104 processSync(mapper);
6105
6106 NotifyMotionArgs args;
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6109 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6110}
6111
6112TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006113 addConfigurationProperty("touch.deviceType", "touchScreen");
6114 prepareDisplay(DISPLAY_ORIENTATION_0);
6115 prepareAxes(POSITION | TOUCH | TOOL);
6116 addConfigurationProperty("touch.size.calibration", "diameter");
6117 addConfigurationProperty("touch.size.scale", "10");
6118 addConfigurationProperty("touch.size.bias", "160");
6119 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006120 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121
6122 // These calculations are based on the input device calibration documentation.
6123 // Note: We only provide a single common touch/tool value because the device is assumed
6124 // not to emit separate values for each pointer (isSummed = 1).
6125 int32_t rawX = 100;
6126 int32_t rawY = 200;
6127 int32_t rawX2 = 150;
6128 int32_t rawY2 = 250;
6129 int32_t rawTouchMajor = 5;
6130 int32_t rawToolMajor = 8;
6131
6132 float x = toDisplayX(rawX);
6133 float y = toDisplayY(rawY);
6134 float x2 = toDisplayX(rawX2);
6135 float y2 = toDisplayY(rawY2);
6136 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6137 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6138 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6139
6140 processPosition(mapper, rawX, rawY);
6141 processTouchMajor(mapper, rawTouchMajor);
6142 processToolMajor(mapper, rawToolMajor);
6143 processMTSync(mapper);
6144 processPosition(mapper, rawX2, rawY2);
6145 processTouchMajor(mapper, rawTouchMajor);
6146 processToolMajor(mapper, rawToolMajor);
6147 processMTSync(mapper);
6148 processSync(mapper);
6149
6150 NotifyMotionArgs args;
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6153
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6155 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6156 args.action);
6157 ASSERT_EQ(size_t(2), args.pointerCount);
6158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6159 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6161 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6162}
6163
6164TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 addConfigurationProperty("touch.deviceType", "touchScreen");
6166 prepareDisplay(DISPLAY_ORIENTATION_0);
6167 prepareAxes(POSITION | TOUCH | TOOL);
6168 addConfigurationProperty("touch.size.calibration", "area");
6169 addConfigurationProperty("touch.size.scale", "43");
6170 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006171 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006172
6173 // These calculations are based on the input device calibration documentation.
6174 int32_t rawX = 100;
6175 int32_t rawY = 200;
6176 int32_t rawTouchMajor = 5;
6177 int32_t rawToolMajor = 8;
6178
6179 float x = toDisplayX(rawX);
6180 float y = toDisplayY(rawY);
6181 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6182 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6183 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6184
6185 processPosition(mapper, rawX, rawY);
6186 processTouchMajor(mapper, rawTouchMajor);
6187 processToolMajor(mapper, rawToolMajor);
6188 processMTSync(mapper);
6189 processSync(mapper);
6190
6191 NotifyMotionArgs args;
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6194 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6195}
6196
6197TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198 addConfigurationProperty("touch.deviceType", "touchScreen");
6199 prepareDisplay(DISPLAY_ORIENTATION_0);
6200 prepareAxes(POSITION | PRESSURE);
6201 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6202 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006203 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204
Michael Wrightaa449c92017-12-13 21:21:43 +00006205 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006206 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006207 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6208 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6209 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6210
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211 // These calculations are based on the input device calibration documentation.
6212 int32_t rawX = 100;
6213 int32_t rawY = 200;
6214 int32_t rawPressure = 60;
6215
6216 float x = toDisplayX(rawX);
6217 float y = toDisplayY(rawY);
6218 float pressure = float(rawPressure) * 0.01f;
6219
6220 processPosition(mapper, rawX, rawY);
6221 processPressure(mapper, rawPressure);
6222 processMTSync(mapper);
6223 processSync(mapper);
6224
6225 NotifyMotionArgs args;
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6228 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6229}
6230
6231TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232 addConfigurationProperty("touch.deviceType", "touchScreen");
6233 prepareDisplay(DISPLAY_ORIENTATION_0);
6234 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006235 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236
6237 NotifyMotionArgs motionArgs;
6238 NotifyKeyArgs keyArgs;
6239
6240 processId(mapper, 1);
6241 processPosition(mapper, 100, 200);
6242 processSync(mapper);
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6245 ASSERT_EQ(0, motionArgs.buttonState);
6246
6247 // press BTN_LEFT, release BTN_LEFT
6248 processKey(mapper, BTN_LEFT, 1);
6249 processSync(mapper);
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6252 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6253
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6256 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6257
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258 processKey(mapper, BTN_LEFT, 0);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006266 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267
6268 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6269 processKey(mapper, BTN_RIGHT, 1);
6270 processKey(mapper, BTN_MIDDLE, 1);
6271 processSync(mapper);
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6274 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6275 motionArgs.buttonState);
6276
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6279 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6280
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6282 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6283 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6284 motionArgs.buttonState);
6285
Michael Wrightd02c5b62014-02-10 15:10:22 -08006286 processKey(mapper, BTN_RIGHT, 0);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295
6296 processKey(mapper, BTN_MIDDLE, 0);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305
6306 // press BTN_BACK, release BTN_BACK
6307 processKey(mapper, BTN_BACK, 1);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6310 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6311 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6316
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6319 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320
6321 processKey(mapper, BTN_BACK, 0);
6322 processSync(mapper);
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329 ASSERT_EQ(0, motionArgs.buttonState);
6330
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6332 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6333 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6334
6335 // press BTN_SIDE, release BTN_SIDE
6336 processKey(mapper, BTN_SIDE, 1);
6337 processSync(mapper);
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6339 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6340 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006341
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6345
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6347 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6348 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349
6350 processKey(mapper, BTN_SIDE, 0);
6351 processSync(mapper);
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006355
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358 ASSERT_EQ(0, motionArgs.buttonState);
6359
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6361 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6362 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6363
6364 // press BTN_FORWARD, release BTN_FORWARD
6365 processKey(mapper, BTN_FORWARD, 1);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6368 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6369 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006370
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6374
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6377 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378
6379 processKey(mapper, BTN_FORWARD, 0);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387 ASSERT_EQ(0, motionArgs.buttonState);
6388
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6390 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6391 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6392
6393 // press BTN_EXTRA, release BTN_EXTRA
6394 processKey(mapper, BTN_EXTRA, 1);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6397 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6398 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006399
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006402 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6403
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6406 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407
6408 processKey(mapper, BTN_EXTRA, 0);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006413
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416 ASSERT_EQ(0, motionArgs.buttonState);
6417
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6419 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6420 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6421
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6423
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424 // press BTN_STYLUS, release BTN_STYLUS
6425 processKey(mapper, BTN_STYLUS, 1);
6426 processSync(mapper);
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6430
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6433 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434
6435 processKey(mapper, BTN_STYLUS, 0);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006440
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006443 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444
6445 // press BTN_STYLUS2, release BTN_STYLUS2
6446 processKey(mapper, BTN_STYLUS2, 1);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006450 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6451
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6454 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455
6456 processKey(mapper, BTN_STYLUS2, 0);
6457 processSync(mapper);
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006461
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006464 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465
6466 // release touch
6467 processId(mapper, -1);
6468 processSync(mapper);
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6471 ASSERT_EQ(0, motionArgs.buttonState);
6472}
6473
6474TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 addConfigurationProperty("touch.deviceType", "touchScreen");
6476 prepareDisplay(DISPLAY_ORIENTATION_0);
6477 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006478 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006479
6480 NotifyMotionArgs motionArgs;
6481
6482 // default tool type is finger
6483 processId(mapper, 1);
6484 processPosition(mapper, 100, 200);
6485 processSync(mapper);
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6489
6490 // eraser
6491 processKey(mapper, BTN_TOOL_RUBBER, 1);
6492 processSync(mapper);
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6496
6497 // stylus
6498 processKey(mapper, BTN_TOOL_RUBBER, 0);
6499 processKey(mapper, BTN_TOOL_PEN, 1);
6500 processSync(mapper);
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6504
6505 // brush
6506 processKey(mapper, BTN_TOOL_PEN, 0);
6507 processKey(mapper, BTN_TOOL_BRUSH, 1);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6512
6513 // pencil
6514 processKey(mapper, BTN_TOOL_BRUSH, 0);
6515 processKey(mapper, BTN_TOOL_PENCIL, 1);
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6520
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006521 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006522 processKey(mapper, BTN_TOOL_PENCIL, 0);
6523 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6524 processSync(mapper);
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6528
6529 // mouse
6530 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6531 processKey(mapper, BTN_TOOL_MOUSE, 1);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6536
6537 // lens
6538 processKey(mapper, BTN_TOOL_MOUSE, 0);
6539 processKey(mapper, BTN_TOOL_LENS, 1);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6544
6545 // double-tap
6546 processKey(mapper, BTN_TOOL_LENS, 0);
6547 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6548 processSync(mapper);
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6552
6553 // triple-tap
6554 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6555 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6560
6561 // quad-tap
6562 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6563 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6568
6569 // finger
6570 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6571 processKey(mapper, BTN_TOOL_FINGER, 1);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6576
6577 // stylus trumps finger
6578 processKey(mapper, BTN_TOOL_PEN, 1);
6579 processSync(mapper);
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6583
6584 // eraser trumps stylus
6585 processKey(mapper, BTN_TOOL_RUBBER, 1);
6586 processSync(mapper);
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6590
6591 // mouse trumps eraser
6592 processKey(mapper, BTN_TOOL_MOUSE, 1);
6593 processSync(mapper);
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6597
6598 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6599 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6600 processSync(mapper);
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6604
6605 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6606 processToolType(mapper, MT_TOOL_PEN);
6607 processSync(mapper);
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6611
6612 // back to default tool type
6613 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6614 processKey(mapper, BTN_TOOL_MOUSE, 0);
6615 processKey(mapper, BTN_TOOL_RUBBER, 0);
6616 processKey(mapper, BTN_TOOL_PEN, 0);
6617 processKey(mapper, BTN_TOOL_FINGER, 0);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6622}
6623
6624TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006625 addConfigurationProperty("touch.deviceType", "touchScreen");
6626 prepareDisplay(DISPLAY_ORIENTATION_0);
6627 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006628 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006629 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630
6631 NotifyMotionArgs motionArgs;
6632
6633 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6634 processId(mapper, 1);
6635 processPosition(mapper, 100, 200);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6640 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6641
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6645 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6646
6647 // move a little
6648 processPosition(mapper, 150, 250);
6649 processSync(mapper);
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6653 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6654
6655 // down when BTN_TOUCH is pressed, pressure defaults to 1
6656 processKey(mapper, BTN_TOUCH, 1);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6661 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6662
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6666 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6667
6668 // up when BTN_TOUCH is released, hover restored
6669 processKey(mapper, BTN_TOUCH, 0);
6670 processSync(mapper);
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6672 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6674 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6675
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6679 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6680
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6684 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6685
6686 // exit hover when pointer goes away
6687 processId(mapper, -1);
6688 processSync(mapper);
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6692 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6693}
6694
6695TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006696 addConfigurationProperty("touch.deviceType", "touchScreen");
6697 prepareDisplay(DISPLAY_ORIENTATION_0);
6698 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006699 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006700
6701 NotifyMotionArgs motionArgs;
6702
6703 // initially hovering because pressure is 0
6704 processId(mapper, 1);
6705 processPosition(mapper, 100, 200);
6706 processPressure(mapper, 0);
6707 processSync(mapper);
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6709 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6711 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6712
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6716 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6717
6718 // move a little
6719 processPosition(mapper, 150, 250);
6720 processSync(mapper);
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6722 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6724 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6725
6726 // down when pressure becomes non-zero
6727 processPressure(mapper, RAW_PRESSURE_MAX);
6728 processSync(mapper);
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6732 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6733
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6738
6739 // up when pressure becomes 0, hover restored
6740 processPressure(mapper, 0);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6745 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6746
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6751
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6755 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6756
6757 // exit hover when pointer goes away
6758 processId(mapper, -1);
6759 processSync(mapper);
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6763 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6764}
6765
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006766/**
6767 * Set the input device port <--> display port associations, and check that the
6768 * events are routed to the display that matches the display port.
6769 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6770 */
6771TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006772 const std::string usb2 = "USB2";
6773 const uint8_t hdmi1 = 0;
6774 const uint8_t hdmi2 = 1;
6775 const std::string secondaryUniqueId = "uniqueId2";
6776 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6777
6778 addConfigurationProperty("touch.deviceType", "touchScreen");
6779 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006780 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006781
6782 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6783 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6784
6785 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6786 // for this input device is specified, and the matching viewport is not present,
6787 // the input device should be disabled (at the mapper level).
6788
6789 // Add viewport for display 2 on hdmi2
6790 prepareSecondaryDisplay(type, hdmi2);
6791 // Send a touch event
6792 processPosition(mapper, 100, 100);
6793 processSync(mapper);
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6795
6796 // Add viewport for display 1 on hdmi1
6797 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6798 // Send a touch event again
6799 processPosition(mapper, 100, 100);
6800 processSync(mapper);
6801
6802 NotifyMotionArgs args;
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6804 ASSERT_EQ(DISPLAY_ID, args.displayId);
6805}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006806
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006807TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006808 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006809 std::shared_ptr<FakePointerController> fakePointerController =
6810 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006811 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006812 fakePointerController->setPosition(100, 200);
6813 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006814 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6815
Garfield Tan888a6a42020-01-09 11:39:16 -08006816 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6817 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6818
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006819 prepareDisplay(DISPLAY_ORIENTATION_0);
6820 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006821 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006822
6823 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006824 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006825
6826 NotifyMotionArgs motionArgs;
6827 processPosition(mapper, 100, 100);
6828 processSync(mapper);
6829
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6831 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6832 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6833}
6834
Arthur Hung7c645402019-01-25 17:45:42 +08006835TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6836 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006837 prepareAxes(POSITION | ID | SLOT);
6838 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006839 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006840
6841 // Create the second touch screen device, and enable multi fingers.
6842 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006843 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006844 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006845 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006846 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006847 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006848 std::unique_ptr<InputDevice> device2 =
6849 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006850 identifier);
6851 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6852 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6853 0 /*flat*/, 0 /*fuzz*/);
6854 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6855 0 /*flat*/, 0 /*fuzz*/);
6856 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6857 0 /*flat*/, 0 /*fuzz*/);
6858 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6859 0 /*flat*/, 0 /*fuzz*/);
6860 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6861 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6862 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006863
6864 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006865 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006866 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6867 device2->reset(ARBITRARY_TIME);
6868
6869 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006870 std::shared_ptr<FakePointerController> fakePointerController =
6871 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006872 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6873 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6874
6875 // Setup policy for associated displays and show touches.
6876 const uint8_t hdmi1 = 0;
6877 const uint8_t hdmi2 = 1;
6878 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6879 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6880 mFakePolicy->setShowTouches(true);
6881
6882 // Create displays.
6883 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6884 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6885
6886 // Default device will reconfigure above, need additional reconfiguration for another device.
6887 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6888 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6889
6890 // Two fingers down at default display.
6891 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6892 processPosition(mapper, x1, y1);
6893 processId(mapper, 1);
6894 processSlot(mapper, 1);
6895 processPosition(mapper, x2, y2);
6896 processId(mapper, 2);
6897 processSync(mapper);
6898
6899 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6900 fakePointerController->getSpots().find(DISPLAY_ID);
6901 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6902 ASSERT_EQ(size_t(2), iter->second.size());
6903
6904 // Two fingers down at second display.
6905 processPosition(mapper2, x1, y1);
6906 processId(mapper2, 1);
6907 processSlot(mapper2, 1);
6908 processPosition(mapper2, x2, y2);
6909 processId(mapper2, 2);
6910 processSync(mapper2);
6911
6912 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6913 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6914 ASSERT_EQ(size_t(2), iter->second.size());
6915}
6916
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006917TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006918 prepareAxes(POSITION);
6919 addConfigurationProperty("touch.deviceType", "touchScreen");
6920 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006921 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006922
6923 NotifyMotionArgs motionArgs;
6924 // Unrotated video frame
6925 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6926 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006927 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006928 processPosition(mapper, 100, 200);
6929 processSync(mapper);
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6931 ASSERT_EQ(frames, motionArgs.videoFrames);
6932
6933 // Subsequent touch events should not have any videoframes
6934 // This is implemented separately in FakeEventHub,
6935 // but that should match the behaviour of TouchVideoDevice.
6936 processPosition(mapper, 200, 200);
6937 processSync(mapper);
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6939 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6940}
6941
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006942TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006943 prepareAxes(POSITION);
6944 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006945 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006946 // Unrotated video frame
6947 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6948 NotifyMotionArgs motionArgs;
6949
6950 // Test all 4 orientations
6951 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6952 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6953 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6954 clearViewports();
6955 prepareDisplay(orientation);
6956 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006957 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006958 processPosition(mapper, 100, 200);
6959 processSync(mapper);
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6961 frames[0].rotate(orientation);
6962 ASSERT_EQ(frames, motionArgs.videoFrames);
6963 }
6964}
6965
6966TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006967 prepareAxes(POSITION);
6968 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006969 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006970 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6971 // so mix these.
6972 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6973 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6974 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6975 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6976 NotifyMotionArgs motionArgs;
6977
6978 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006979 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006980 processPosition(mapper, 100, 200);
6981 processSync(mapper);
6982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6983 std::for_each(frames.begin(), frames.end(),
6984 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6985 ASSERT_EQ(frames, motionArgs.videoFrames);
6986}
6987
Arthur Hung9da14732019-09-02 16:16:58 +08006988/**
6989 * If we had defined port associations, but the viewport is not ready, the touch device would be
6990 * expected to be disabled, and it should be enabled after the viewport has found.
6991 */
6992TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006993 constexpr uint8_t hdmi2 = 1;
6994 const std::string secondaryUniqueId = "uniqueId2";
6995 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6996
6997 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6998
6999 addConfigurationProperty("touch.deviceType", "touchScreen");
7000 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007001 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007002
7003 ASSERT_EQ(mDevice->isEnabled(), false);
7004
7005 // Add display on hdmi2, the device should be enabled and can receive touch event.
7006 prepareSecondaryDisplay(type, hdmi2);
7007 ASSERT_EQ(mDevice->isEnabled(), true);
7008
7009 // Send a touch event.
7010 processPosition(mapper, 100, 100);
7011 processSync(mapper);
7012
7013 NotifyMotionArgs args;
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7015 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7016}
7017
Arthur Hung6cd19a42019-08-30 19:04:12 +08007018
Arthur Hung6cd19a42019-08-30 19:04:12 +08007019
Arthur Hung421eb1c2020-01-16 00:09:42 +08007020TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007021 addConfigurationProperty("touch.deviceType", "touchScreen");
7022 prepareDisplay(DISPLAY_ORIENTATION_0);
7023 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007025
7026 NotifyMotionArgs motionArgs;
7027
7028 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7029 // finger down
7030 processId(mapper, 1);
7031 processPosition(mapper, x1, y1);
7032 processSync(mapper);
7033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7034 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7036
7037 // finger move
7038 processId(mapper, 1);
7039 processPosition(mapper, x2, y2);
7040 processSync(mapper);
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7044
7045 // finger up.
7046 processId(mapper, -1);
7047 processSync(mapper);
7048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7049 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7051
7052 // new finger down
7053 processId(mapper, 1);
7054 processPosition(mapper, x3, y3);
7055 processSync(mapper);
7056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7057 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7059}
7060
7061/**
7062 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
7063 * UP events should be ignored.
7064 */
7065TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007066 addConfigurationProperty("touch.deviceType", "touchScreen");
7067 prepareDisplay(DISPLAY_ORIENTATION_0);
7068 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007069 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007070
7071 NotifyMotionArgs motionArgs;
7072
7073 // default tool type is finger
7074 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7075 processId(mapper, 1);
7076 processPosition(mapper, x1, y1);
7077 processSync(mapper);
7078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7079 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7080 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7081
7082 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7083 processToolType(mapper, MT_TOOL_PALM);
7084 processSync(mapper);
7085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7086 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7087
7088 // Ignore the following MOVE and UP events if had detect a palm event.
7089 processId(mapper, 1);
7090 processPosition(mapper, x2, y2);
7091 processSync(mapper);
7092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7093
7094 // finger up.
7095 processId(mapper, -1);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7098
7099 // new finger down
7100 processToolType(mapper, MT_TOOL_FINGER);
7101 processId(mapper, 1);
7102 processPosition(mapper, x3, y3);
7103 processSync(mapper);
7104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7105 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7107}
7108
arthurhungbf89a482020-04-17 17:37:55 +08007109/**
7110 * Test multi-touch should be canceled when received the MT_TOOL_PALM event from some finger,
7111 * and could be allowed again after all non-MT_TOOL_PALM is release and the new point is
7112 * MT_TOOL_FINGER.
7113 */
7114TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType2) {
7115 addConfigurationProperty("touch.deviceType", "touchScreen");
7116 prepareDisplay(DISPLAY_ORIENTATION_0);
7117 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7118 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7119
7120 NotifyMotionArgs motionArgs;
7121
7122 // default tool type is finger
7123 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7124 processId(mapper, 1);
7125 processPosition(mapper, x1, y1);
7126 processSync(mapper);
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7128 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7130
7131 // Second finger down.
7132 processSlot(mapper, 1);
7133 processPosition(mapper, x2, y2);
7134 processId(mapper, 2);
7135 processSync(mapper);
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7137 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7138 motionArgs.action);
7139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7140
7141 // If the tool type of the first pointer changes to MT_TOOL_PALM,
7142 // the entire gesture should be aborted, so we expect to receive ACTION_CANCEL.
7143 processSlot(mapper, 0);
7144 processId(mapper, 1);
7145 processToolType(mapper, MT_TOOL_PALM);
7146 processSync(mapper);
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7148 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7149
7150 // Ignore the following MOVE and UP events if had detect a palm event.
7151 processSlot(mapper, 1);
7152 processId(mapper, 2);
7153 processPosition(mapper, x3, y3);
7154 processSync(mapper);
7155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7156
7157 // second finger up.
7158 processId(mapper, -1);
7159 processSync(mapper);
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7161
7162 // first finger move, but still in palm
7163 processSlot(mapper, 0);
7164 processId(mapper, 1);
7165 processPosition(mapper, x1 - 1, y1 - 1);
7166 processSync(mapper);
7167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7168
7169 // second finger down, expect as new finger down.
7170 processSlot(mapper, 1);
7171 processId(mapper, 2);
7172 processPosition(mapper, x2, y2);
7173 processSync(mapper);
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7175 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7177}
7178
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007179// --- MultiTouchInputMapperTest_ExternalDevice ---
7180
7181class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7182protected:
7183 virtual void SetUp() override {
7184 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7185 }
7186};
7187
7188/**
7189 * Expect fallback to internal viewport if device is external and external viewport is not present.
7190 */
7191TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7192 prepareAxes(POSITION);
7193 addConfigurationProperty("touch.deviceType", "touchScreen");
7194 prepareDisplay(DISPLAY_ORIENTATION_0);
7195 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7196
7197 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7198
7199 NotifyMotionArgs motionArgs;
7200
7201 // Expect the event to be sent to the internal viewport,
7202 // because an external viewport is not present.
7203 processPosition(mapper, 100, 100);
7204 processSync(mapper);
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7206 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7207
7208 // Expect the event to be sent to the external viewport if it is present.
7209 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
7210 processPosition(mapper, 100, 100);
7211 processSync(mapper);
7212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7213 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7214}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007215
7216/**
7217 * Test touch should not work if outside of surface.
7218 */
7219class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7220protected:
7221 void halfDisplayToCenterHorizontal(int32_t orientation) {
7222 std::optional<DisplayViewport> internalViewport =
7223 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
7224
7225 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7226 internalViewport->orientation = orientation;
7227 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7228 internalViewport->logicalLeft = 0;
7229 internalViewport->logicalTop = 0;
7230 internalViewport->logicalRight = DISPLAY_HEIGHT;
7231 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7232
7233 internalViewport->physicalLeft = 0;
7234 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7235 internalViewport->physicalRight = DISPLAY_HEIGHT;
7236 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7237
7238 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7239 internalViewport->deviceHeight = DISPLAY_WIDTH;
7240 } else {
7241 internalViewport->logicalLeft = 0;
7242 internalViewport->logicalTop = 0;
7243 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7244 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7245
7246 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7247 internalViewport->physicalTop = 0;
7248 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7249 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7250
7251 internalViewport->deviceWidth = DISPLAY_WIDTH;
7252 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7253 }
7254
7255 mFakePolicy->updateViewport(internalViewport.value());
7256 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7257 }
7258
7259 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7260 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7261 int32_t yExpected) {
7262 // touch on outside area should not work.
7263 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7264 processSync(mapper);
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7266
7267 // touch on inside area should receive the event.
7268 NotifyMotionArgs args;
7269 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7272 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7273 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7274
7275 // Reset.
7276 mapper.reset(ARBITRARY_TIME);
7277 }
7278};
7279
7280TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7281 addConfigurationProperty("touch.deviceType", "touchScreen");
7282 prepareDisplay(DISPLAY_ORIENTATION_0);
7283 prepareAxes(POSITION);
7284 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7285
7286 // Touch on center of normal display should work.
7287 const int32_t x = DISPLAY_WIDTH / 4;
7288 const int32_t y = DISPLAY_HEIGHT / 2;
7289 processPosition(mapper, toRawX(x), toRawY(y));
7290 processSync(mapper);
7291 NotifyMotionArgs args;
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7294 0.0f, 0.0f, 0.0f, 0.0f));
7295 // Reset.
7296 mapper.reset(ARBITRARY_TIME);
7297
7298 // Let physical display be different to device, and make surface and physical could be 1:1.
7299 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7300
7301 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7302 const int32_t yExpected = y;
7303 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7304}
7305
7306TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7307 addConfigurationProperty("touch.deviceType", "touchScreen");
7308 prepareDisplay(DISPLAY_ORIENTATION_0);
7309 prepareAxes(POSITION);
7310 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7311
7312 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7313 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7314
7315 const int32_t x = DISPLAY_WIDTH / 4;
7316 const int32_t y = DISPLAY_HEIGHT / 2;
7317
7318 // expect x/y = swap x/y then reverse y.
7319 const int32_t xExpected = y;
7320 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7321 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7322}
7323
7324TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7325 addConfigurationProperty("touch.deviceType", "touchScreen");
7326 prepareDisplay(DISPLAY_ORIENTATION_0);
7327 prepareAxes(POSITION);
7328 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7329
7330 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7331 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7332
7333 const int32_t x = DISPLAY_WIDTH / 4;
7334 const int32_t y = DISPLAY_HEIGHT / 2;
7335
7336 // expect x/y = swap x/y then reverse x.
7337 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7338 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7339 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7340}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007341} // namespace android