blob: fae7e64c5e546251ab4e4c1c1e51615a10d5f8bc [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 =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001191 mFakePolicy->getDisplayViewportByType(ViewportType::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,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001196 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1197 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001198
1199 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001200 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001201 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001202 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001203
1204 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001205 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001206 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001207 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001208
1209 mFakePolicy->clearViewports();
1210 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001211 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001212 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001213 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214 ASSERT_FALSE(internalViewport);
1215}
1216
1217TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1218 const std::string internalUniqueId = "local:0";
1219 const std::string externalUniqueId = "local:1";
1220 const std::string virtualUniqueId1 = "virtual:2";
1221 const std::string virtualUniqueId2 = "virtual:3";
1222 constexpr int32_t virtualDisplayId1 = 2;
1223 constexpr int32_t virtualDisplayId2 = 3;
1224
1225 // Add an internal viewport
1226 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001227 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1228 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001229 // Add an external viewport
1230 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001231 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1232 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001233 // Add an virtual viewport
1234 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001235 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1236 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237 // Add another virtual viewport
1238 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001239 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1240 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001241
1242 // Check matching by type for internal
1243 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001244 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001245 ASSERT_TRUE(internalViewport);
1246 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1247
1248 // Check matching by type for external
1249 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001250 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001251 ASSERT_TRUE(externalViewport);
1252 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1253
1254 // Check matching by uniqueId for virtual viewport #1
1255 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001256 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001257 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001258 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001259 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1260 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1261
1262 // Check matching by uniqueId for virtual viewport #2
1263 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001264 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001265 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001266 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001267 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1268 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1269}
1270
1271
1272/**
1273 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1274 * that lookup works by checking display id.
1275 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1276 */
1277TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1278 const std::string uniqueId1 = "uniqueId1";
1279 const std::string uniqueId2 = "uniqueId2";
1280 constexpr int32_t displayId1 = 2;
1281 constexpr int32_t displayId2 = 3;
1282
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001283 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1284 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001285 for (const ViewportType& type : types) {
1286 mFakePolicy->clearViewports();
1287 // Add a viewport
1288 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001289 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001290 // Add another viewport
1291 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001292 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001293
1294 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001295 std::optional<DisplayViewport> viewport1 =
1296 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001297 ASSERT_TRUE(viewport1);
1298 ASSERT_EQ(displayId1, viewport1->displayId);
1299 ASSERT_EQ(type, viewport1->type);
1300
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001301 std::optional<DisplayViewport> viewport2 =
1302 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001303 ASSERT_TRUE(viewport2);
1304 ASSERT_EQ(displayId2, viewport2->displayId);
1305 ASSERT_EQ(type, viewport2->type);
1306
1307 // When there are multiple viewports of the same kind, and uniqueId is not specified
1308 // in the call to getDisplayViewport, then that situation is not supported.
1309 // The viewports can be stored in any order, so we cannot rely on the order, since that
1310 // is just implementation detail.
1311 // However, we can check that it still returns *a* viewport, we just cannot assert
1312 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001313 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001314 ASSERT_TRUE(someViewport);
1315 }
1316}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001317
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001318/**
1319 * Check getDisplayViewportByPort
1320 */
1321TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001322 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001323 const std::string uniqueId1 = "uniqueId1";
1324 const std::string uniqueId2 = "uniqueId2";
1325 constexpr int32_t displayId1 = 1;
1326 constexpr int32_t displayId2 = 2;
1327 const uint8_t hdmi1 = 0;
1328 const uint8_t hdmi2 = 1;
1329 const uint8_t hdmi3 = 2;
1330
1331 mFakePolicy->clearViewports();
1332 // Add a viewport that's associated with some display port that's not of interest.
1333 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1334 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1335 // Add another viewport, connected to HDMI1 port
1336 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1337 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1338
1339 // Check that correct display viewport was returned by comparing the display ports.
1340 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1341 ASSERT_TRUE(hdmi1Viewport);
1342 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1343 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1344
1345 // Check that we can still get the same viewport using the uniqueId
1346 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1347 ASSERT_TRUE(hdmi1Viewport);
1348 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1349 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1350 ASSERT_EQ(type, hdmi1Viewport->type);
1351
1352 // Check that we cannot find a port with "HDMI2", because we never added one
1353 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1354 ASSERT_FALSE(hdmi2Viewport);
1355}
1356
Michael Wrightd02c5b62014-02-10 15:10:22 -08001357// --- InputReaderTest ---
1358
1359class InputReaderTest : public testing::Test {
1360protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001361 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001362 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001363 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001364 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365
Prabir Pradhan28efc192019-11-05 01:10:04 +00001366 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001367 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001368 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001369 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001370
Prabir Pradhan28efc192019-11-05 01:10:04 +00001371 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1372 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373 }
1374
Prabir Pradhan28efc192019-11-05 01:10:04 +00001375 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001376 mFakeListener.clear();
1377 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378 }
1379
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001380 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1381 const PropertyMap* configuration) {
1382 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383
1384 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001385 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 }
1387 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001388 mReader->loopOnce();
1389 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001390 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1391 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001392 }
1393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001394 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001395 mFakePolicy->addDisabledDevice(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. Lewisf4916ef2020-01-14 11:57:18 -08001399 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001400 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001401 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001402 }
1403
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001404 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001405 const std::string& name, uint32_t classes,
1406 uint32_t sources,
1407 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001408 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1409 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001410 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001411 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001412 return mapper;
1413 }
1414};
1415
1416TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001417 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001418 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001419 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001420 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001421
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001422 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001423 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001424 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001425 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001426 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001427 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1428 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1429 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1430
1431 // Should also have received a notification describing the new input devices.
1432 inputDevices = mFakePolicy->getInputDevices();
1433 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001434 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001435 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001436 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1437 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1438 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1439}
1440
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001441TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001442 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001443 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001444 constexpr int32_t eventHubId = 1;
1445 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001446 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001447 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001448 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001449 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001450
Yi Kong9b14ac62018-07-17 13:48:38 -07001451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001452
1453 NotifyDeviceResetArgs resetArgs;
1454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001455 ASSERT_EQ(deviceId, resetArgs.deviceId);
1456
1457 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001458 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001459 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001460
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001462 ASSERT_EQ(deviceId, resetArgs.deviceId);
1463 ASSERT_EQ(device->isEnabled(), false);
1464
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001465 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001466 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001469 ASSERT_EQ(device->isEnabled(), false);
1470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001471 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001472 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001474 ASSERT_EQ(deviceId, resetArgs.deviceId);
1475 ASSERT_EQ(device->isEnabled(), true);
1476}
1477
Michael Wrightd02c5b62014-02-10 15:10:22 -08001478TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001479 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1480 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1481 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001482 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001483 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001484 AINPUT_SOURCE_KEYBOARD, nullptr);
1485 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001486
1487 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1488 AINPUT_SOURCE_ANY, AKEYCODE_A))
1489 << "Should return unknown when the device id is >= 0 but unknown.";
1490
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001491 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1492 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1493 << "Should return unknown when the device id is valid but the sources are not "
1494 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001496 ASSERT_EQ(AKEY_STATE_DOWN,
1497 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1498 AKEYCODE_A))
1499 << "Should return value provided by mapper when device id is valid and the device "
1500 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001501
1502 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1503 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1504 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1505
1506 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1507 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1508 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1509}
1510
1511TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001512 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1513 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1514 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001515 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001516 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001517 AINPUT_SOURCE_KEYBOARD, nullptr);
1518 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519
1520 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1521 AINPUT_SOURCE_ANY, KEY_A))
1522 << "Should return unknown when the device id is >= 0 but unknown.";
1523
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001524 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1525 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1526 << "Should return unknown when the device id is valid but the sources are not "
1527 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001528
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001529 ASSERT_EQ(AKEY_STATE_DOWN,
1530 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1531 KEY_A))
1532 << "Should return value provided by mapper when device id is valid and the device "
1533 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001534
1535 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1536 AINPUT_SOURCE_TRACKBALL, KEY_A))
1537 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1538
1539 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1540 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1541 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1542}
1543
1544TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001545 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1546 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1547 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001548 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001549 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001550 AINPUT_SOURCE_KEYBOARD, nullptr);
1551 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001552
1553 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1554 AINPUT_SOURCE_ANY, SW_LID))
1555 << "Should return unknown when the device id is >= 0 but unknown.";
1556
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001557 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1558 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1559 << "Should return unknown when the device id is valid but the sources are not "
1560 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001562 ASSERT_EQ(AKEY_STATE_DOWN,
1563 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1564 SW_LID))
1565 << "Should return value provided by mapper when device id is valid and the device "
1566 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567
1568 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1569 AINPUT_SOURCE_TRACKBALL, SW_LID))
1570 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1571
1572 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1573 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1574 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1575}
1576
1577TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001578 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1579 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1580 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001581 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001582 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001583 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001584
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001585 mapper.addSupportedKeyCode(AKEYCODE_A);
1586 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587
1588 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1589 uint8_t flags[4] = { 0, 0, 0, 1 };
1590
1591 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1592 << "Should return false when device id is >= 0 but unknown.";
1593 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1594
1595 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001596 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1597 << "Should return false when device id is valid but the sources are not supported by "
1598 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001599 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1600
1601 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001602 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1603 keyCodes, flags))
1604 << "Should return value provided by mapper when device id is valid and the device "
1605 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1607
1608 flags[3] = 1;
1609 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1610 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1611 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1612
1613 flags[3] = 1;
1614 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1615 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1616 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1617}
1618
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001619TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001620 constexpr int32_t eventHubId = 1;
1621 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622
1623 NotifyConfigurationChangedArgs args;
1624
1625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1626 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1627}
1628
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001629TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1631 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1632 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001633 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001634 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001635 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001636
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001637 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001638 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001639 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1640
1641 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001642 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001644 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645 ASSERT_EQ(EV_KEY, event.type);
1646 ASSERT_EQ(KEY_A, event.code);
1647 ASSERT_EQ(1, event.value);
1648}
1649
Garfield Tan1c7bc862020-01-28 13:24:04 -08001650TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001651 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001652 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 constexpr int32_t eventHubId = 1;
1654 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001655 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001656 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001657 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001659
1660 NotifyDeviceResetArgs resetArgs;
1661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001662 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001663
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001664 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001665 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001667 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001668 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001669
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001670 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001671 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001673 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001674 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001675
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001676 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001677 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001679 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001680 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001681}
1682
Garfield Tan1c7bc862020-01-28 13:24:04 -08001683TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1684 constexpr int32_t deviceId = 1;
1685 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1686 constexpr int32_t eventHubId = 1;
1687 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1688 // Must add at least one mapper or the device will be ignored!
1689 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1690 mReader->setNextDevice(device);
1691 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1692
1693 NotifyDeviceResetArgs resetArgs;
1694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1695 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1696}
1697
Arthur Hungc23540e2018-11-29 20:42:11 +08001698TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001700 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001701 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001702 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001703 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1704 FakeInputMapper& mapper =
1705 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001706 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001707
1708 const uint8_t hdmi1 = 1;
1709
1710 // Associated touch screen with second display.
1711 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1712
1713 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001714 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001715 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001716 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1717 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001718 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001719 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1720 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001721 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001722 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001723
1724 // Add the device, and make sure all of the callbacks are triggered.
1725 // The device is added after the input port associations are processed since
1726 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001727 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001730 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001731
Arthur Hung2c9a3342019-07-23 14:18:59 +08001732 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001733 ASSERT_EQ(deviceId, device->getId());
1734 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1735 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001736
1737 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001738 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001739 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001740 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001741}
1742
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001743// --- InputReaderIntegrationTest ---
1744
1745// These tests create and interact with the InputReader only through its interface.
1746// The InputReader is started during SetUp(), which starts its processing in its own
1747// thread. The tests use linux uinput to emulate input devices.
1748// NOTE: Interacting with the physical device while these tests are running may cause
1749// the tests to fail.
1750class InputReaderIntegrationTest : public testing::Test {
1751protected:
1752 sp<TestInputListener> mTestListener;
1753 sp<FakeInputReaderPolicy> mFakePolicy;
1754 sp<InputReaderInterface> mReader;
1755
1756 virtual void SetUp() override {
1757 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001758 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1759 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001760
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001761 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001762 ASSERT_EQ(mReader->start(), OK);
1763
1764 // Since this test is run on a real device, all the input devices connected
1765 // to the test device will show up in mReader. We wait for those input devices to
1766 // show up before beginning the tests.
1767 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1768 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1769 }
1770
1771 virtual void TearDown() override {
1772 ASSERT_EQ(mReader->stop(), OK);
1773 mTestListener.clear();
1774 mFakePolicy.clear();
1775 }
1776};
1777
1778TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1779 // An invalid input device that is only used for this test.
1780 class InvalidUinputDevice : public UinputDevice {
1781 public:
1782 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1783
1784 private:
1785 void configureDevice(int fd, uinput_user_dev* device) override {}
1786 };
1787
1788 const size_t numDevices = mFakePolicy->getInputDevices().size();
1789
1790 // UinputDevice does not set any event or key bits, so InputReader should not
1791 // consider it as a valid device.
1792 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1793 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1794 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1795 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1796
1797 invalidDevice.reset();
1798 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1799 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1800 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1801}
1802
1803TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1804 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1805
1806 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1807 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1808 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1809 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1810
1811 // Find the test device by its name.
1812 std::vector<InputDeviceInfo> inputDevices;
1813 mReader->getInputDevices(inputDevices);
1814 InputDeviceInfo* keyboardInfo = nullptr;
1815 const char* keyboardName = keyboard->getName();
1816 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1817 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1818 keyboardInfo = &inputDevices[i];
1819 break;
1820 }
1821 }
1822 ASSERT_NE(keyboardInfo, nullptr);
1823 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1824 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1825 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1826
1827 keyboard.reset();
1828 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1829 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1830 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1831}
1832
1833TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1834 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1835 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1836
1837 NotifyConfigurationChangedArgs configChangedArgs;
1838 ASSERT_NO_FATAL_FAILURE(
1839 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001840 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001841 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1842
1843 NotifyKeyArgs keyArgs;
1844 keyboard->pressAndReleaseHomeKey();
1845 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1846 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001847 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001848 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001849 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1850 prevTimestamp = keyArgs.eventTime;
1851
1852 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1853 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001854 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001855 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1856}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001858/**
1859 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1860 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1861 * are passed to the listener.
1862 */
1863static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1864TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1865 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1866 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1867 NotifyKeyArgs keyArgs;
1868
1869 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1870 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1871 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1872 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1873
1874 controller->pressAndReleaseKey(BTN_GEAR_UP);
1875 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1876 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1877 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1878}
1879
Arthur Hungaab25622020-01-16 11:22:11 +08001880// --- TouchProcessTest ---
1881class TouchIntegrationTest : public InputReaderIntegrationTest {
1882protected:
1883 static const int32_t FIRST_SLOT = 0;
1884 static const int32_t SECOND_SLOT = 1;
1885 static const int32_t FIRST_TRACKING_ID = 0;
1886 static const int32_t SECOND_TRACKING_ID = 1;
1887 const std::string UNIQUE_ID = "local:0";
1888
1889 virtual void SetUp() override {
1890 InputReaderIntegrationTest::SetUp();
1891 // At least add an internal display.
1892 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1893 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001894 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001895
1896 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1897 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1898 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1899 }
1900
1901 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1902 int32_t orientation, const std::string& uniqueId,
1903 std::optional<uint8_t> physicalPort,
1904 ViewportType viewportType) {
1905 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1906 physicalPort, viewportType);
1907 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1908 }
1909
1910 std::unique_ptr<UinputTouchScreen> mDevice;
1911};
1912
1913TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1914 NotifyMotionArgs args;
1915 const Point centerPoint = mDevice->getCenterPoint();
1916
1917 // ACTION_DOWN
1918 mDevice->sendDown(centerPoint);
1919 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1920 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1921
1922 // ACTION_MOVE
1923 mDevice->sendMove(centerPoint + Point(1, 1));
1924 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1926
1927 // ACTION_UP
1928 mDevice->sendUp();
1929 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1930 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1931}
1932
1933TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1934 NotifyMotionArgs args;
1935 const Point centerPoint = mDevice->getCenterPoint();
1936
1937 // ACTION_DOWN
1938 mDevice->sendDown(centerPoint);
1939 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1940 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1941
1942 // ACTION_POINTER_DOWN (Second slot)
1943 const Point secondPoint = centerPoint + Point(100, 100);
1944 mDevice->sendSlot(SECOND_SLOT);
1945 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1946 mDevice->sendDown(secondPoint + Point(1, 1));
1947 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1948 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1949 args.action);
1950
1951 // ACTION_MOVE (Second slot)
1952 mDevice->sendMove(secondPoint);
1953 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1955
1956 // ACTION_POINTER_UP (Second slot)
1957 mDevice->sendUp();
1958 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1959 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1960 args.action);
1961
1962 // ACTION_UP
1963 mDevice->sendSlot(FIRST_SLOT);
1964 mDevice->sendUp();
1965 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1966 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1967}
1968
1969TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1970 NotifyMotionArgs args;
1971 const Point centerPoint = mDevice->getCenterPoint();
1972
1973 // ACTION_DOWN
1974 mDevice->sendDown(centerPoint);
1975 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1976 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1977
1978 // ACTION_POINTER_DOWN (Second slot)
1979 const Point secondPoint = centerPoint + Point(100, 100);
1980 mDevice->sendSlot(SECOND_SLOT);
1981 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1982 mDevice->sendDown(secondPoint);
1983 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1984 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1985 args.action);
1986
1987 // ACTION_MOVE (Second slot)
1988 mDevice->sendMove(secondPoint + Point(1, 1));
1989 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1991
1992 // Send MT_TOOL_PALM, which indicates that the touch IC has determined this to be a grip event.
1993 // Expect to receive ACTION_CANCEL, to abort the entire gesture.
1994 mDevice->sendToolType(MT_TOOL_PALM);
1995 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1996 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, args.action);
1997
1998 // ACTION_POINTER_UP (Second slot)
1999 mDevice->sendUp();
2000
2001 // ACTION_UP
2002 mDevice->sendSlot(FIRST_SLOT);
2003 mDevice->sendUp();
2004
2005 // Expect no event received after abort the entire gesture.
2006 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2007}
2008
Michael Wrightd02c5b62014-02-10 15:10:22 -08002009// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002010class InputDeviceTest : public testing::Test {
2011protected:
2012 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002013 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002014 static const int32_t DEVICE_ID;
2015 static const int32_t DEVICE_GENERATION;
2016 static const int32_t DEVICE_CONTROLLER_NUMBER;
2017 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002018 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002019
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002020 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002021 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002022 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002023 FakeInputReaderContext* mFakeContext;
2024
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002025 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002026
Prabir Pradhan28efc192019-11-05 01:10:04 +00002027 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002028 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002029 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002030 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002031 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2032
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002033 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002034 InputDeviceIdentifier identifier;
2035 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002036 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002037 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2038 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002039 }
2040
Prabir Pradhan28efc192019-11-05 01:10:04 +00002041 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002042 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002043 delete mFakeContext;
2044 mFakeListener.clear();
2045 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002046 }
2047};
2048
2049const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002050const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002051const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002052const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2053const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
2054const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
2055 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002056const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002057
2058TEST_F(InputDeviceTest, ImmutableProperties) {
2059 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002060 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002061 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002062}
2063
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002064TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2065 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002066}
2067
Michael Wrightd02c5b62014-02-10 15:10:22 -08002068TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2069 // Configuration.
2070 InputReaderConfiguration config;
2071 mDevice->configure(ARBITRARY_TIME, &config, 0);
2072
2073 // Reset.
2074 mDevice->reset(ARBITRARY_TIME);
2075
2076 NotifyDeviceResetArgs resetArgs;
2077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2078 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2079 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2080
2081 // Metadata.
2082 ASSERT_TRUE(mDevice->isIgnored());
2083 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2084
2085 InputDeviceInfo info;
2086 mDevice->getDeviceInfo(&info);
2087 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002088 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002089 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2090 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2091
2092 // State queries.
2093 ASSERT_EQ(0, mDevice->getMetaState());
2094
2095 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2096 << "Ignored device should return unknown key code state.";
2097 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2098 << "Ignored device should return unknown scan code state.";
2099 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2100 << "Ignored device should return unknown switch state.";
2101
2102 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2103 uint8_t flags[2] = { 0, 1 };
2104 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2105 << "Ignored device should never mark any key codes.";
2106 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2107 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2108}
2109
2110TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2111 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002112 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002113
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002114 FakeInputMapper& mapper1 =
2115 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002116 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2117 mapper1.setMetaState(AMETA_ALT_ON);
2118 mapper1.addSupportedKeyCode(AKEYCODE_A);
2119 mapper1.addSupportedKeyCode(AKEYCODE_B);
2120 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2121 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2122 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2123 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2124 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002125
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002126 FakeInputMapper& mapper2 =
2127 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002128 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002129
2130 InputReaderConfiguration config;
2131 mDevice->configure(ARBITRARY_TIME, &config, 0);
2132
2133 String8 propertyValue;
2134 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2135 << "Device should have read configuration during configuration phase.";
2136 ASSERT_STREQ("value", propertyValue.string());
2137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002138 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2139 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002140
2141 // Reset
2142 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002143 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2144 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002145
2146 NotifyDeviceResetArgs resetArgs;
2147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2148 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2149 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2150
2151 // Metadata.
2152 ASSERT_FALSE(mDevice->isIgnored());
2153 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2154
2155 InputDeviceInfo info;
2156 mDevice->getDeviceInfo(&info);
2157 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002158 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002159 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2160 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2161
2162 // State queries.
2163 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2164 << "Should query mappers and combine meta states.";
2165
2166 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2167 << "Should return unknown key code state when source not supported.";
2168 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2169 << "Should return unknown scan code state when source not supported.";
2170 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2171 << "Should return unknown switch state when source not supported.";
2172
2173 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2174 << "Should query mapper when source is supported.";
2175 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2176 << "Should query mapper when source is supported.";
2177 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2178 << "Should query mapper when source is supported.";
2179
2180 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2181 uint8_t flags[4] = { 0, 0, 0, 1 };
2182 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2183 << "Should do nothing when source is unsupported.";
2184 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2185 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2186 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2187 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2188
2189 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2190 << "Should query mapper when source is supported.";
2191 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2192 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2193 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2194 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2195
2196 // Event handling.
2197 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002198 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002199 mDevice->process(&event, 1);
2200
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002201 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2202 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002203}
2204
Arthur Hung2c9a3342019-07-23 14:18:59 +08002205// A single input device is associated with a specific display. Check that:
2206// 1. Device is disabled if the viewport corresponding to the associated display is not found
2207// 2. Device is disabled when setEnabled API is called
2208TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002209 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002210
2211 // First Configuration.
2212 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2213
2214 // Device should be enabled by default.
2215 ASSERT_TRUE(mDevice->isEnabled());
2216
2217 // Prepare associated info.
2218 constexpr uint8_t hdmi = 1;
2219 const std::string UNIQUE_ID = "local:1";
2220
2221 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2222 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2223 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2224 // Device should be disabled because it is associated with a specific display via
2225 // input port <-> display port association, but the corresponding display is not found
2226 ASSERT_FALSE(mDevice->isEnabled());
2227
2228 // Prepare displays.
2229 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002230 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002231 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2232 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2233 ASSERT_TRUE(mDevice->isEnabled());
2234
2235 // Device should be disabled after set disable.
2236 mFakePolicy->addDisabledDevice(mDevice->getId());
2237 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2238 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2239 ASSERT_FALSE(mDevice->isEnabled());
2240
2241 // Device should still be disabled even found the associated display.
2242 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2243 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2244 ASSERT_FALSE(mDevice->isEnabled());
2245}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002246
2247// --- InputMapperTest ---
2248
2249class InputMapperTest : public testing::Test {
2250protected:
2251 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002252 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002253 static const int32_t DEVICE_ID;
2254 static const int32_t DEVICE_GENERATION;
2255 static const int32_t DEVICE_CONTROLLER_NUMBER;
2256 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002257 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002259 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002260 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002261 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262 FakeInputReaderContext* mFakeContext;
2263 InputDevice* mDevice;
2264
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002265 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002266 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002267 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002268 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002269 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2270 InputDeviceIdentifier identifier;
2271 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002272 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002273 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002275 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002276 }
2277
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002278 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2279
Prabir Pradhan28efc192019-11-05 01:10:04 +00002280 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281 delete mDevice;
2282 delete mFakeContext;
2283 mFakeListener.clear();
2284 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285 }
2286
2287 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002288 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289 }
2290
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002291 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002292 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2293 mFakeContext->updatePointerDisplay();
2294 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002295 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2296 }
2297
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002298 template <class T, typename... Args>
2299 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002300 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002301 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002302 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002303 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002304 }
2305
2306 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002307 int32_t orientation, const std::string& uniqueId,
2308 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002309 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002310 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002311 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2312 }
2313
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002314 void clearViewports() {
2315 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002316 }
2317
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002318 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2319 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002320 RawEvent event;
2321 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002322 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323 event.type = type;
2324 event.code = code;
2325 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002326 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002327 }
2328
2329 static void assertMotionRange(const InputDeviceInfo& info,
2330 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2331 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002332 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002333 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2334 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2335 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2336 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2337 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2338 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2339 }
2340
2341 static void assertPointerCoords(const PointerCoords& coords,
2342 float x, float y, float pressure, float size,
2343 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2344 float orientation, float distance) {
2345 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2346 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2347 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2348 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2349 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2350 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2351 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2352 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2353 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2354 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2355 }
2356
Michael Wright17db18e2020-06-26 20:51:44 +01002357 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002359 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002360 ASSERT_NEAR(x, actualX, 1);
2361 ASSERT_NEAR(y, actualY, 1);
2362 }
2363};
2364
2365const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002366const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002367const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2369const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2370const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002371const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002372
2373// --- SwitchInputMapperTest ---
2374
2375class SwitchInputMapperTest : public InputMapperTest {
2376protected:
2377};
2378
2379TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002380 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002382 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383}
2384
2385TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002386 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002388 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002389 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002391 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002392 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393}
2394
2395TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002396 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002397
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002398 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2399 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2400 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2401 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402
2403 NotifySwitchArgs args;
2404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2405 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002406 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2407 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 args.switchMask);
2409 ASSERT_EQ(uint32_t(0), args.policyFlags);
2410}
2411
2412
2413// --- KeyboardInputMapperTest ---
2414
2415class KeyboardInputMapperTest : public InputMapperTest {
2416protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002417 const std::string UNIQUE_ID = "local:0";
2418
2419 void prepareDisplay(int32_t orientation);
2420
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002421 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002422 int32_t originalKeyCode, int32_t rotatedKeyCode,
2423 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424};
2425
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002426/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2427 * orientation.
2428 */
2429void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002430 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2431 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002432}
2433
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002434void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002435 int32_t originalScanCode, int32_t originalKeyCode,
2436 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437 NotifyKeyArgs args;
2438
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002439 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2441 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2442 ASSERT_EQ(originalScanCode, args.scanCode);
2443 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002444 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002446 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2448 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2449 ASSERT_EQ(originalScanCode, args.scanCode);
2450 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002451 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452}
2453
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002455 KeyboardInputMapper& mapper =
2456 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2457 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002459 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002460}
2461
2462TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2463 const int32_t USAGE_A = 0x070004;
2464 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002465 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2466 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002468 KeyboardInputMapper& mapper =
2469 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2470 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471
2472 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002473 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474 NotifyKeyArgs args;
2475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2476 ASSERT_EQ(DEVICE_ID, args.deviceId);
2477 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2478 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2479 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2480 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2481 ASSERT_EQ(KEY_HOME, args.scanCode);
2482 ASSERT_EQ(AMETA_NONE, args.metaState);
2483 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2484 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2485 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2486
2487 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002488 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2490 ASSERT_EQ(DEVICE_ID, args.deviceId);
2491 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2492 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2493 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2494 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2495 ASSERT_EQ(KEY_HOME, args.scanCode);
2496 ASSERT_EQ(AMETA_NONE, args.metaState);
2497 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2498 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2499 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2500
2501 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002502 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2503 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2505 ASSERT_EQ(DEVICE_ID, args.deviceId);
2506 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2507 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2508 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2509 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2510 ASSERT_EQ(0, args.scanCode);
2511 ASSERT_EQ(AMETA_NONE, args.metaState);
2512 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2513 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2514 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2515
2516 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002517 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2518 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2520 ASSERT_EQ(DEVICE_ID, args.deviceId);
2521 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2522 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2523 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2524 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2525 ASSERT_EQ(0, args.scanCode);
2526 ASSERT_EQ(AMETA_NONE, args.metaState);
2527 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2528 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2529 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2530
2531 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002532 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2533 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2535 ASSERT_EQ(DEVICE_ID, args.deviceId);
2536 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2537 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2538 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2539 ASSERT_EQ(0, args.keyCode);
2540 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2541 ASSERT_EQ(AMETA_NONE, args.metaState);
2542 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2543 ASSERT_EQ(0U, args.policyFlags);
2544 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2545
2546 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002547 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2548 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2550 ASSERT_EQ(DEVICE_ID, args.deviceId);
2551 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2552 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2553 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2554 ASSERT_EQ(0, args.keyCode);
2555 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2556 ASSERT_EQ(AMETA_NONE, args.metaState);
2557 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2558 ASSERT_EQ(0U, args.policyFlags);
2559 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2560}
2561
2562TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002563 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2564 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002566 KeyboardInputMapper& mapper =
2567 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2568 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002569
2570 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002571 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002572
2573 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002574 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575 NotifyKeyArgs args;
2576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002578 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2580
2581 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002582 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
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 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002588 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002591 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002592
2593 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002594 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2596 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002597 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2599}
2600
2601TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002602 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2603 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2604 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2605 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002607 KeyboardInputMapper& mapper =
2608 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2609 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002611 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2613 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2615 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2616 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2617 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2618 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2619 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2620}
2621
2622TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002623 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2624 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2625 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2626 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002627
Michael Wrightd02c5b62014-02-10 15:10:22 -08002628 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002629 KeyboardInputMapper& mapper =
2630 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2631 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002633 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002634 ASSERT_NO_FATAL_FAILURE(
2635 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2636 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2637 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2638 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2639 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2640 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2641 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002643 clearViewports();
2644 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002645 ASSERT_NO_FATAL_FAILURE(
2646 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2648 AKEYCODE_DPAD_UP, DISPLAY_ID));
2649 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2650 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2651 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2652 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002654 clearViewports();
2655 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002656 ASSERT_NO_FATAL_FAILURE(
2657 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2658 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2659 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2660 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2661 AKEYCODE_DPAD_UP, DISPLAY_ID));
2662 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2663 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002665 clearViewports();
2666 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002667 ASSERT_NO_FATAL_FAILURE(
2668 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2670 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2671 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2672 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2673 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2674 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675
2676 // Special case: if orientation changes while key is down, we still emit the same keycode
2677 // in the key up as we did in the key down.
2678 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002679 clearViewports();
2680 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002681 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2683 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2684 ASSERT_EQ(KEY_UP, args.scanCode);
2685 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2686
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002687 clearViewports();
2688 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002689 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2691 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2692 ASSERT_EQ(KEY_UP, args.scanCode);
2693 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2694}
2695
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002696TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2697 // If the keyboard is not orientation aware,
2698 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002699 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002700
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002701 KeyboardInputMapper& mapper =
2702 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2703 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002704 NotifyKeyArgs args;
2705
2706 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002707 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002709 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2711 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2712
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002713 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002714 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002716 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2718 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2719}
2720
2721TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2722 // If the keyboard is orientation aware,
2723 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002724 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002725
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002726 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002727 KeyboardInputMapper& mapper =
2728 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2729 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002730 NotifyKeyArgs args;
2731
2732 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2733 // ^--- already checked by the previous test
2734
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002735 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002736 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002737 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002739 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2741 ASSERT_EQ(DISPLAY_ID, args.displayId);
2742
2743 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002744 clearViewports();
2745 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002746 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002747 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002749 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2751 ASSERT_EQ(newDisplayId, args.displayId);
2752}
2753
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002755 KeyboardInputMapper& mapper =
2756 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2757 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002759 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002760 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002762 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002763 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764}
2765
2766TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 KeyboardInputMapper& mapper =
2768 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2769 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002771 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002772 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002773
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002774 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002775 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776}
2777
2778TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002779 KeyboardInputMapper& mapper =
2780 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2781 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
2785 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2786 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788 ASSERT_TRUE(flags[0]);
2789 ASSERT_FALSE(flags[1]);
2790}
2791
2792TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002793 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2794 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2795 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2796 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2797 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2798 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002800 KeyboardInputMapper& mapper =
2801 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2802 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803
2804 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002805 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2806 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2807 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808
2809 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002810 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2811 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002812 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2813 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2814 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002815 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816
2817 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002818 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2819 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002820 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2821 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2822 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002823 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824
2825 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002826 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2827 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002828 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2829 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2830 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002831 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832
2833 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002834 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2835 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002836 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2837 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2838 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002839 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002840
2841 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002842 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2843 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002844 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2845 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2846 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002847 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848
2849 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002850 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2851 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002852 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2853 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2854 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002855 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856}
2857
Arthur Hung2c9a3342019-07-23 14:18:59 +08002858TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2859 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002860 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2861 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2862 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2863 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002864
2865 // keyboard 2.
2866 const std::string USB2 = "USB2";
2867 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002868 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002869 InputDeviceIdentifier identifier;
2870 identifier.name = "KEYBOARD2";
2871 identifier.location = USB2;
2872 std::unique_ptr<InputDevice> device2 =
2873 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002874 identifier);
2875 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2876 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2877 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2878 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2879 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002880
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002881 KeyboardInputMapper& mapper =
2882 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2883 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002885 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002886 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002887 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002888 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2889 device2->reset(ARBITRARY_TIME);
2890
2891 // Prepared displays and associated info.
2892 constexpr uint8_t hdmi1 = 0;
2893 constexpr uint8_t hdmi2 = 1;
2894 const std::string SECONDARY_UNIQUE_ID = "local:1";
2895
2896 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2897 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2898
2899 // No associated display viewport found, should disable the device.
2900 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2901 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2902 ASSERT_FALSE(device2->isEnabled());
2903
2904 // Prepare second display.
2905 constexpr int32_t newDisplayId = 2;
2906 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002907 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002908 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002909 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002910 // Default device will reconfigure above, need additional reconfiguration for another device.
2911 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2912 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2913
2914 // Device should be enabled after the associated display is found.
2915 ASSERT_TRUE(mDevice->isEnabled());
2916 ASSERT_TRUE(device2->isEnabled());
2917
2918 // Test pad key events
2919 ASSERT_NO_FATAL_FAILURE(
2920 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2921 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2922 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2923 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2924 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2925 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2926 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2927
2928 ASSERT_NO_FATAL_FAILURE(
2929 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2930 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2931 AKEYCODE_DPAD_RIGHT, newDisplayId));
2932 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2933 AKEYCODE_DPAD_DOWN, newDisplayId));
2934 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2935 AKEYCODE_DPAD_LEFT, newDisplayId));
2936}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002938// --- KeyboardInputMapperTest_ExternalDevice ---
2939
2940class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2941protected:
2942 virtual void SetUp() override {
2943 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2944 }
2945};
2946
2947TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002948 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2949 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002950
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002951 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2952 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2953 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2954 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002956 KeyboardInputMapper& mapper =
2957 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2958 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002959
2960 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2961 NotifyKeyArgs args;
2962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2963 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2964
2965 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2967 ASSERT_EQ(uint32_t(0), args.policyFlags);
2968
2969 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2971 ASSERT_EQ(uint32_t(0), args.policyFlags);
2972
2973 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2975 ASSERT_EQ(uint32_t(0), args.policyFlags);
2976
2977 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2979 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2980
2981 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2983 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2984}
2985
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002986TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002987 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002988
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002989 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2990 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2991 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002992
Powei Fengd041c5d2019-05-03 17:11:33 -07002993 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002994 KeyboardInputMapper& mapper =
2995 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2996 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002997
2998 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2999 NotifyKeyArgs args;
3000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3001 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3002
3003 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3005 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3006
3007 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3009 ASSERT_EQ(uint32_t(0), args.policyFlags);
3010
3011 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3013 ASSERT_EQ(uint32_t(0), args.policyFlags);
3014
3015 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3017 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3018
3019 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3021 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3022}
3023
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024// --- CursorInputMapperTest ---
3025
3026class CursorInputMapperTest : public InputMapperTest {
3027protected:
3028 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3029
Michael Wright17db18e2020-06-26 20:51:44 +01003030 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031
Prabir Pradhan28efc192019-11-05 01:10:04 +00003032 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033 InputMapperTest::SetUp();
3034
Michael Wright17db18e2020-06-26 20:51:44 +01003035 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003036 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 }
3038
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003039 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3040 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003041
3042 void prepareDisplay(int32_t orientation) {
3043 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003044 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003045 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3046 orientation, uniqueId, NO_PORT, viewportType);
3047 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048};
3049
3050const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003052void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3053 int32_t originalY, int32_t rotatedX,
3054 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 NotifyMotionArgs args;
3056
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003057 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3058 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3059 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3063 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3064 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3065 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3066}
3067
3068TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003070 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003072 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073}
3074
3075TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003077 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080}
3081
3082TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085
3086 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003087 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088
3089 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003090 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3091 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3093 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3094
3095 // When the bounds are set, then there should be a valid motion range.
3096 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3097
3098 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
3101 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3102 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3103 1, 800 - 1, 0.0f, 0.0f));
3104 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3105 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3106 2, 480 - 1, 0.0f, 0.0f));
3107 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3108 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3109 0.0f, 1.0f, 0.0f, 0.0f));
3110}
3111
3112TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003114 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115
3116 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003117 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118
3119 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3120 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3121 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3122 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3123 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3124 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3125 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3126 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3127 0.0f, 1.0f, 0.0f, 0.0f));
3128}
3129
3130TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003132 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133
3134 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3135
3136 NotifyMotionArgs args;
3137
3138 // Button press.
3139 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003140 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3141 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3143 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3144 ASSERT_EQ(DEVICE_ID, args.deviceId);
3145 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3146 ASSERT_EQ(uint32_t(0), args.policyFlags);
3147 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3148 ASSERT_EQ(0, args.flags);
3149 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3150 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3151 ASSERT_EQ(0, args.edgeFlags);
3152 ASSERT_EQ(uint32_t(1), args.pointerCount);
3153 ASSERT_EQ(0, args.pointerProperties[0].id);
3154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3156 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3157 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3158 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3159 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3160
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3162 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3163 ASSERT_EQ(DEVICE_ID, args.deviceId);
3164 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3165 ASSERT_EQ(uint32_t(0), args.policyFlags);
3166 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3167 ASSERT_EQ(0, args.flags);
3168 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3169 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3170 ASSERT_EQ(0, args.edgeFlags);
3171 ASSERT_EQ(uint32_t(1), args.pointerCount);
3172 ASSERT_EQ(0, args.pointerProperties[0].id);
3173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3175 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3176 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3177 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3178 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3179
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003181 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3182 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3184 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3185 ASSERT_EQ(DEVICE_ID, args.deviceId);
3186 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3187 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3189 ASSERT_EQ(0, args.flags);
3190 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3191 ASSERT_EQ(0, args.buttonState);
3192 ASSERT_EQ(0, args.edgeFlags);
3193 ASSERT_EQ(uint32_t(1), args.pointerCount);
3194 ASSERT_EQ(0, args.pointerProperties[0].id);
3195 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3197 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3198 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3199 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3200 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3201
3202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3203 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3204 ASSERT_EQ(DEVICE_ID, args.deviceId);
3205 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3206 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3208 ASSERT_EQ(0, args.flags);
3209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3210 ASSERT_EQ(0, args.buttonState);
3211 ASSERT_EQ(0, args.edgeFlags);
3212 ASSERT_EQ(uint32_t(1), args.pointerCount);
3213 ASSERT_EQ(0, args.pointerProperties[0].id);
3214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3216 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3217 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3218 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3219 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3220}
3221
3222TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003224 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225
3226 NotifyMotionArgs args;
3227
3228 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003229 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3230 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3234 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3235
3236 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003237 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3238 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3242 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3243}
3244
3245TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
3249 NotifyMotionArgs args;
3250
3251 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003252 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3253 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3255 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3257 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3258
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3262 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3263
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003265 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3266 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3270 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3271
3272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3275 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3276}
3277
3278TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003280 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281
3282 NotifyMotionArgs args;
3283
3284 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003285 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3286 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3287 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3288 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3290 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3298 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3299 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3300
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003302 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3303 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3304 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3308 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3309 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3310
3311 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003312 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3313 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3317 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3318
3319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3322 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3323}
3324
3325TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003326 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003329 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3331 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3332 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3333 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3334 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3335 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3336 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3337 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3338}
3339
3340TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 addConfigurationProperty("cursor.mode", "navigation");
3342 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003343 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003345 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3350 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3351 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3352 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3354
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003355 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3357 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3358 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3359 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3360 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3361 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3362 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3364
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003365 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3367 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3368 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3374
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003375 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3377 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3378 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3379 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3380 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3381 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3382 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3383 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3384}
3385
3386TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003388 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389
3390 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3391 mFakePointerController->setPosition(100, 200);
3392 mFakePointerController->setButtonState(0);
3393
3394 NotifyMotionArgs motionArgs;
3395 NotifyKeyArgs keyArgs;
3396
3397 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003398 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3399 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3401 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3402 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3403 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3405 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3406
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3409 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3410 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3412 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3413
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003414 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3415 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003417 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418 ASSERT_EQ(0, motionArgs.buttonState);
3419 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3421 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3422
3423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003424 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_EQ(0, motionArgs.buttonState);
3426 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3428 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3429
3430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003432 ASSERT_EQ(0, motionArgs.buttonState);
3433 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3435 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3436
3437 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003438 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3439 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3440 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3442 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3443 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3444 motionArgs.buttonState);
3445 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3446 mFakePointerController->getButtonState());
3447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3448 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3449
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3451 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3452 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3453 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3454 mFakePointerController->getButtonState());
3455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3456 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3457
3458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3460 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3461 motionArgs.buttonState);
3462 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3463 mFakePointerController->getButtonState());
3464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3465 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3466
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003467 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3468 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3472 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3474 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3475
3476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003478 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3479 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3481 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3482
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003483 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3484 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3487 ASSERT_EQ(0, motionArgs.buttonState);
3488 ASSERT_EQ(0, mFakePointerController->getButtonState());
3489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3490 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 -08003491 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3492 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003493
3494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495 ASSERT_EQ(0, motionArgs.buttonState);
3496 ASSERT_EQ(0, mFakePointerController->getButtonState());
3497 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3499 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003500
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3502 ASSERT_EQ(0, motionArgs.buttonState);
3503 ASSERT_EQ(0, mFakePointerController->getButtonState());
3504 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3506 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3507
3508 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003509 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3510 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3512 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3513 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003514
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003516 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3518 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3520 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3521
3522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3523 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3524 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3525 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3527 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3528
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003529 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3530 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003532 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533 ASSERT_EQ(0, motionArgs.buttonState);
3534 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3536 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3537
3538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003540 ASSERT_EQ(0, motionArgs.buttonState);
3541 ASSERT_EQ(0, mFakePointerController->getButtonState());
3542
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3544 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3546 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3547 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3548
3549 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003550 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3551 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3553 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3554 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003555
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3559 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3561 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3562
3563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3565 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3566 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3568 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3569
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003570 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3571 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574 ASSERT_EQ(0, motionArgs.buttonState);
3575 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3577 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 -08003578
3579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3580 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3581 ASSERT_EQ(0, motionArgs.buttonState);
3582 ASSERT_EQ(0, mFakePointerController->getButtonState());
3583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3584 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3585
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3587 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3588 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3589
3590 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003591 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3592 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3594 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3595 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003596
Michael Wrightd02c5b62014-02-10 15:10:22 -08003597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3600 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3602 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3603
3604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3605 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3606 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3607 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3609 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3610
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003611 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3612 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615 ASSERT_EQ(0, motionArgs.buttonState);
3616 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3618 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 -08003619
3620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3621 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3622 ASSERT_EQ(0, motionArgs.buttonState);
3623 ASSERT_EQ(0, mFakePointerController->getButtonState());
3624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3625 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3626
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3628 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3629 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3630
3631 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003632 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3633 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3635 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3636 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003637
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3641 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3643 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3644
3645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3647 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3648 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3650 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3651
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003652 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3653 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003655 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656 ASSERT_EQ(0, motionArgs.buttonState);
3657 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3659 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 -08003660
3661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3662 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3663 ASSERT_EQ(0, motionArgs.buttonState);
3664 ASSERT_EQ(0, mFakePointerController->getButtonState());
3665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3666 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3667
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3669 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3670 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3671}
3672
3673TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003675 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676
3677 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3678 mFakePointerController->setPosition(100, 200);
3679 mFakePointerController->setButtonState(0);
3680
3681 NotifyMotionArgs args;
3682
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003683 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3684 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3685 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003687 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3688 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3690 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 +01003691 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003692}
3693
3694TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003695 addConfigurationProperty("cursor.mode", "pointer");
3696 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003697 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003698
3699 NotifyDeviceResetArgs resetArgs;
3700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3701 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3702 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3703
3704 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3705 mFakePointerController->setPosition(100, 200);
3706 mFakePointerController->setButtonState(0);
3707
3708 NotifyMotionArgs args;
3709
3710 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003711 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3712 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3713 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3715 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3718 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 +01003719 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003720
3721 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003722 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3723 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3725 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3726 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3728 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3730 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3731 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3733 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3734
3735 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003736 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3737 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3739 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3740 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3742 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3744 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3745 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3747 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3748
3749 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003750 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3751 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3752 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3754 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3757 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 +01003758 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003759
3760 // Disable pointer capture and check that the device generation got bumped
3761 // and events are generated the usual way.
3762 const uint32_t generation = mFakeContext->getGeneration();
3763 mFakePolicy->setPointerCapture(false);
3764 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3765 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3766
3767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3768 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3769 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3770
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003771 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3772 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3773 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3775 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3778 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003779 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780}
3781
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003782TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003783 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003784
Garfield Tan888a6a42020-01-09 11:39:16 -08003785 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003786 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003787 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3788 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003789 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003790 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3791 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3792
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003793 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3794 mFakePointerController->setPosition(100, 200);
3795 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003796
3797 NotifyMotionArgs args;
3798 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3799 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3800 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3802 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3803 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3805 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 +01003806 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003807 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3808}
3809
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810// --- TouchInputMapperTest ---
3811
3812class TouchInputMapperTest : public InputMapperTest {
3813protected:
3814 static const int32_t RAW_X_MIN;
3815 static const int32_t RAW_X_MAX;
3816 static const int32_t RAW_Y_MIN;
3817 static const int32_t RAW_Y_MAX;
3818 static const int32_t RAW_TOUCH_MIN;
3819 static const int32_t RAW_TOUCH_MAX;
3820 static const int32_t RAW_TOOL_MIN;
3821 static const int32_t RAW_TOOL_MAX;
3822 static const int32_t RAW_PRESSURE_MIN;
3823 static const int32_t RAW_PRESSURE_MAX;
3824 static const int32_t RAW_ORIENTATION_MIN;
3825 static const int32_t RAW_ORIENTATION_MAX;
3826 static const int32_t RAW_DISTANCE_MIN;
3827 static const int32_t RAW_DISTANCE_MAX;
3828 static const int32_t RAW_TILT_MIN;
3829 static const int32_t RAW_TILT_MAX;
3830 static const int32_t RAW_ID_MIN;
3831 static const int32_t RAW_ID_MAX;
3832 static const int32_t RAW_SLOT_MIN;
3833 static const int32_t RAW_SLOT_MAX;
3834 static const float X_PRECISION;
3835 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003836 static const float X_PRECISION_VIRTUAL;
3837 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838
3839 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003840 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841
3842 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3843
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003844 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003845 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003846
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847 enum Axes {
3848 POSITION = 1 << 0,
3849 TOUCH = 1 << 1,
3850 TOOL = 1 << 2,
3851 PRESSURE = 1 << 3,
3852 ORIENTATION = 1 << 4,
3853 MINOR = 1 << 5,
3854 ID = 1 << 6,
3855 DISTANCE = 1 << 7,
3856 TILT = 1 << 8,
3857 SLOT = 1 << 9,
3858 TOOL_TYPE = 1 << 10,
3859 };
3860
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003861 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3862 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003863 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003865 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866 int32_t toRawX(float displayX);
3867 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003868 float toCookedX(float rawX, float rawY);
3869 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003871 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003873 float toDisplayY(int32_t rawY, int32_t displayHeight);
3874
Michael Wrightd02c5b62014-02-10 15:10:22 -08003875};
3876
3877const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3878const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3879const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3880const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3881const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3882const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3883const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3884const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003885const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3886const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3888const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3889const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3890const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3891const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3892const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3893const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3894const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3895const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3896const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3897const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3898const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003899const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3900 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3901const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3902 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003903const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3904 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905
3906const float TouchInputMapperTest::GEOMETRIC_SCALE =
3907 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3908 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3909
3910const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3911 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3912 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3913};
3914
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003915void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003916 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3917 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003918}
3919
3920void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3921 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3922 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923}
3924
Santos Cordonfa5cf462017-04-05 10:37:00 -07003925void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003926 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
3927 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
3928 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003929}
3930
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003932 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3933 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3934 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3935 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936}
3937
Jason Gerecke489fda82012-09-07 17:19:40 -07003938void TouchInputMapperTest::prepareLocationCalibration() {
3939 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3940}
3941
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942int32_t TouchInputMapperTest::toRawX(float displayX) {
3943 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3944}
3945
3946int32_t TouchInputMapperTest::toRawY(float displayY) {
3947 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3948}
3949
Jason Gerecke489fda82012-09-07 17:19:40 -07003950float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3951 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3952 return rawX;
3953}
3954
3955float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3956 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3957 return rawY;
3958}
3959
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003961 return toDisplayX(rawX, DISPLAY_WIDTH);
3962}
3963
3964float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3965 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966}
3967
3968float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003969 return toDisplayY(rawY, DISPLAY_HEIGHT);
3970}
3971
3972float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3973 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974}
3975
3976
3977// --- SingleTouchInputMapperTest ---
3978
3979class SingleTouchInputMapperTest : public TouchInputMapperTest {
3980protected:
3981 void prepareButtons();
3982 void prepareAxes(int axes);
3983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003984 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3985 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3986 void processUp(SingleTouchInputMapper& mappery);
3987 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3988 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3989 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3990 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3991 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3992 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993};
3994
3995void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003996 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997}
3998
3999void SingleTouchInputMapperTest::prepareAxes(int axes) {
4000 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004001 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4002 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003 }
4004 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004005 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4006 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 }
4008 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004009 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4010 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 }
4012 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004013 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4014 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 }
4016 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004017 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4018 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 }
4020}
4021
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004022void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004023 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4024 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::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004029 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4030 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031}
4032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004033void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004034 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035}
4036
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004037void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004038 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039}
4040
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004041void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4042 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004043 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044}
4045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004046void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004047 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048}
4049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004050void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4051 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004052 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4053 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054}
4055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004056void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4057 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004058 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059}
4060
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004061void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004062 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063}
4064
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066 prepareButtons();
4067 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004068 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004070 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071}
4072
4073TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004074 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4075 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 prepareButtons();
4077 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004078 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004080 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081}
4082
4083TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 prepareButtons();
4085 prepareAxes(POSITION);
4086 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004087 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004089 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090}
4091
4092TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093 prepareButtons();
4094 prepareAxes(POSITION);
4095 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004096 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004098 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099}
4100
4101TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 addConfigurationProperty("touch.deviceType", "touchScreen");
4103 prepareDisplay(DISPLAY_ORIENTATION_0);
4104 prepareButtons();
4105 prepareAxes(POSITION);
4106 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004107 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108
4109 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004110 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111
4112 // Virtual key is down.
4113 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4114 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4115 processDown(mapper, x, y);
4116 processSync(mapper);
4117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004119 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120
4121 // Virtual key is up.
4122 processUp(mapper);
4123 processSync(mapper);
4124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004126 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127}
4128
4129TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 addConfigurationProperty("touch.deviceType", "touchScreen");
4131 prepareDisplay(DISPLAY_ORIENTATION_0);
4132 prepareButtons();
4133 prepareAxes(POSITION);
4134 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004135 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136
4137 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004138 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139
4140 // Virtual key is down.
4141 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4142 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4143 processDown(mapper, x, y);
4144 processSync(mapper);
4145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004147 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148
4149 // Virtual key is up.
4150 processUp(mapper);
4151 processSync(mapper);
4152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004154 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155}
4156
4157TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158 addConfigurationProperty("touch.deviceType", "touchScreen");
4159 prepareDisplay(DISPLAY_ORIENTATION_0);
4160 prepareButtons();
4161 prepareAxes(POSITION);
4162 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004163 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164
4165 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4166 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004167 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 ASSERT_TRUE(flags[0]);
4169 ASSERT_FALSE(flags[1]);
4170}
4171
4172TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173 addConfigurationProperty("touch.deviceType", "touchScreen");
4174 prepareDisplay(DISPLAY_ORIENTATION_0);
4175 prepareButtons();
4176 prepareAxes(POSITION);
4177 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004178 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179
4180 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4181
4182 NotifyKeyArgs args;
4183
4184 // Press virtual key.
4185 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4186 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4187 processDown(mapper, x, y);
4188 processSync(mapper);
4189
4190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4191 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4192 ASSERT_EQ(DEVICE_ID, args.deviceId);
4193 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4194 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4195 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4196 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4197 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4198 ASSERT_EQ(KEY_HOME, args.scanCode);
4199 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4200 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4201
4202 // Release virtual key.
4203 processUp(mapper);
4204 processSync(mapper);
4205
4206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4207 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4208 ASSERT_EQ(DEVICE_ID, args.deviceId);
4209 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4210 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4211 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4212 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4213 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4214 ASSERT_EQ(KEY_HOME, args.scanCode);
4215 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4216 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4217
4218 // Should not have sent any motions.
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4220}
4221
4222TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 addConfigurationProperty("touch.deviceType", "touchScreen");
4224 prepareDisplay(DISPLAY_ORIENTATION_0);
4225 prepareButtons();
4226 prepareAxes(POSITION);
4227 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004228 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229
4230 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4231
4232 NotifyKeyArgs keyArgs;
4233
4234 // Press virtual key.
4235 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4236 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4237 processDown(mapper, x, y);
4238 processSync(mapper);
4239
4240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4241 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4242 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4243 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4244 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4245 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4246 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4247 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4248 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4249 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4250 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4251
4252 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4253 // into the display area.
4254 y -= 100;
4255 processMove(mapper, x, y);
4256 processSync(mapper);
4257
4258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4259 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4260 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4261 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4262 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4263 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4264 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4265 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4266 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4267 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4269 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4270
4271 NotifyMotionArgs motionArgs;
4272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4278 ASSERT_EQ(0, motionArgs.flags);
4279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4280 ASSERT_EQ(0, motionArgs.buttonState);
4281 ASSERT_EQ(0, motionArgs.edgeFlags);
4282 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4286 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4287 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4288 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4290
4291 // Keep moving out of bounds. Should generate a pointer move.
4292 y -= 50;
4293 processMove(mapper, x, y);
4294 processSync(mapper);
4295
4296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4298 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4300 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4302 ASSERT_EQ(0, motionArgs.flags);
4303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4304 ASSERT_EQ(0, motionArgs.buttonState);
4305 ASSERT_EQ(0, motionArgs.edgeFlags);
4306 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4307 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4310 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4311 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4312 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4314
4315 // Release out of bounds. Should generate a pointer up.
4316 processUp(mapper);
4317 processSync(mapper);
4318
4319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4320 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4321 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4322 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4323 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4324 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4325 ASSERT_EQ(0, motionArgs.flags);
4326 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4327 ASSERT_EQ(0, motionArgs.buttonState);
4328 ASSERT_EQ(0, motionArgs.edgeFlags);
4329 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4330 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4333 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4334 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4335 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4336 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4337
4338 // Should not have sent any more keys or motions.
4339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4341}
4342
4343TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 addConfigurationProperty("touch.deviceType", "touchScreen");
4345 prepareDisplay(DISPLAY_ORIENTATION_0);
4346 prepareButtons();
4347 prepareAxes(POSITION);
4348 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004349 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
4351 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4352
4353 NotifyMotionArgs motionArgs;
4354
4355 // Initially go down out of bounds.
4356 int32_t x = -10;
4357 int32_t y = -10;
4358 processDown(mapper, x, y);
4359 processSync(mapper);
4360
4361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4362
4363 // Move into the display area. Should generate a pointer down.
4364 x = 50;
4365 y = 75;
4366 processMove(mapper, x, y);
4367 processSync(mapper);
4368
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4370 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4371 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4372 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4373 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4374 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4375 ASSERT_EQ(0, motionArgs.flags);
4376 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4377 ASSERT_EQ(0, motionArgs.buttonState);
4378 ASSERT_EQ(0, motionArgs.edgeFlags);
4379 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4380 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4383 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4384 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4385 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4386 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4387
4388 // Release. Should generate a pointer up.
4389 processUp(mapper);
4390 processSync(mapper);
4391
4392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4394 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4395 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4396 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4397 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4398 ASSERT_EQ(0, motionArgs.flags);
4399 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4400 ASSERT_EQ(0, motionArgs.buttonState);
4401 ASSERT_EQ(0, motionArgs.edgeFlags);
4402 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4403 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4406 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4407 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4408 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4410
4411 // Should not have sent any more keys or motions.
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4414}
4415
Santos Cordonfa5cf462017-04-05 10:37:00 -07004416TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004417 addConfigurationProperty("touch.deviceType", "touchScreen");
4418 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4419
4420 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4421 prepareButtons();
4422 prepareAxes(POSITION);
4423 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004424 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004425
4426 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4427
4428 NotifyMotionArgs motionArgs;
4429
4430 // Down.
4431 int32_t x = 100;
4432 int32_t y = 125;
4433 processDown(mapper, x, y);
4434 processSync(mapper);
4435
4436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4437 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4438 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4439 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4440 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4441 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4442 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4443 ASSERT_EQ(0, motionArgs.flags);
4444 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4445 ASSERT_EQ(0, motionArgs.buttonState);
4446 ASSERT_EQ(0, motionArgs.edgeFlags);
4447 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4451 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4452 1, 0, 0, 0, 0, 0, 0, 0));
4453 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4454 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4456
4457 // Move.
4458 x += 50;
4459 y += 75;
4460 processMove(mapper, x, y);
4461 processSync(mapper);
4462
4463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4464 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4465 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4466 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4467 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4468 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4470 ASSERT_EQ(0, motionArgs.flags);
4471 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4472 ASSERT_EQ(0, motionArgs.buttonState);
4473 ASSERT_EQ(0, motionArgs.edgeFlags);
4474 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4475 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4478 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4479 1, 0, 0, 0, 0, 0, 0, 0));
4480 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4481 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4483
4484 // Up.
4485 processUp(mapper);
4486 processSync(mapper);
4487
4488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4489 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4490 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4491 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4492 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4493 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4494 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4495 ASSERT_EQ(0, motionArgs.flags);
4496 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4497 ASSERT_EQ(0, motionArgs.buttonState);
4498 ASSERT_EQ(0, motionArgs.edgeFlags);
4499 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4500 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4503 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4504 1, 0, 0, 0, 0, 0, 0, 0));
4505 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4506 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4507 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4508
4509 // Should not have sent any more keys or motions.
4510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4512}
4513
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515 addConfigurationProperty("touch.deviceType", "touchScreen");
4516 prepareDisplay(DISPLAY_ORIENTATION_0);
4517 prepareButtons();
4518 prepareAxes(POSITION);
4519 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004520 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521
4522 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4523
4524 NotifyMotionArgs motionArgs;
4525
4526 // Down.
4527 int32_t x = 100;
4528 int32_t y = 125;
4529 processDown(mapper, x, y);
4530 processSync(mapper);
4531
4532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4533 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4534 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4535 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4536 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4537 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4538 ASSERT_EQ(0, motionArgs.flags);
4539 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4540 ASSERT_EQ(0, motionArgs.buttonState);
4541 ASSERT_EQ(0, motionArgs.edgeFlags);
4542 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4543 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4546 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4547 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4548 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4549 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4550
4551 // Move.
4552 x += 50;
4553 y += 75;
4554 processMove(mapper, x, y);
4555 processSync(mapper);
4556
4557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4558 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4559 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4560 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4561 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4563 ASSERT_EQ(0, motionArgs.flags);
4564 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4565 ASSERT_EQ(0, motionArgs.buttonState);
4566 ASSERT_EQ(0, motionArgs.edgeFlags);
4567 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4568 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4571 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4572 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4573 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4574 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4575
4576 // Up.
4577 processUp(mapper);
4578 processSync(mapper);
4579
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4582 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4583 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4584 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4585 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4586 ASSERT_EQ(0, motionArgs.flags);
4587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4588 ASSERT_EQ(0, motionArgs.buttonState);
4589 ASSERT_EQ(0, motionArgs.edgeFlags);
4590 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4591 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4594 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4595 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4596 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4597 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4598
4599 // Should not have sent any more keys or motions.
4600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4602}
4603
4604TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 addConfigurationProperty("touch.deviceType", "touchScreen");
4606 prepareButtons();
4607 prepareAxes(POSITION);
4608 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004609 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610
4611 NotifyMotionArgs args;
4612
4613 // Rotation 90.
4614 prepareDisplay(DISPLAY_ORIENTATION_90);
4615 processDown(mapper, toRawX(50), toRawY(75));
4616 processSync(mapper);
4617
4618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4619 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4620 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4621
4622 processUp(mapper);
4623 processSync(mapper);
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4625}
4626
4627TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 addConfigurationProperty("touch.deviceType", "touchScreen");
4629 prepareButtons();
4630 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004631 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
4633 NotifyMotionArgs args;
4634
4635 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004636 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 prepareDisplay(DISPLAY_ORIENTATION_0);
4638 processDown(mapper, toRawX(50), toRawY(75));
4639 processSync(mapper);
4640
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4642 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4643 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4644
4645 processUp(mapper);
4646 processSync(mapper);
4647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4648
4649 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004650 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 prepareDisplay(DISPLAY_ORIENTATION_90);
4652 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4653 processSync(mapper);
4654
4655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4656 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4657 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4658
4659 processUp(mapper);
4660 processSync(mapper);
4661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4662
4663 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004664 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 prepareDisplay(DISPLAY_ORIENTATION_180);
4666 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4667 processSync(mapper);
4668
4669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4670 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4671 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4672
4673 processUp(mapper);
4674 processSync(mapper);
4675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4676
4677 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004678 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679 prepareDisplay(DISPLAY_ORIENTATION_270);
4680 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4681 processSync(mapper);
4682
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4684 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4685 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4686
4687 processUp(mapper);
4688 processSync(mapper);
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4690}
4691
4692TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 addConfigurationProperty("touch.deviceType", "touchScreen");
4694 prepareDisplay(DISPLAY_ORIENTATION_0);
4695 prepareButtons();
4696 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004697 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698
4699 // These calculations are based on the input device calibration documentation.
4700 int32_t rawX = 100;
4701 int32_t rawY = 200;
4702 int32_t rawPressure = 10;
4703 int32_t rawToolMajor = 12;
4704 int32_t rawDistance = 2;
4705 int32_t rawTiltX = 30;
4706 int32_t rawTiltY = 110;
4707
4708 float x = toDisplayX(rawX);
4709 float y = toDisplayY(rawY);
4710 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4711 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4712 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4713 float distance = float(rawDistance);
4714
4715 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4716 float tiltScale = M_PI / 180;
4717 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4718 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4719 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4720 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4721
4722 processDown(mapper, rawX, rawY);
4723 processPressure(mapper, rawPressure);
4724 processToolMajor(mapper, rawToolMajor);
4725 processDistance(mapper, rawDistance);
4726 processTilt(mapper, rawTiltX, rawTiltY);
4727 processSync(mapper);
4728
4729 NotifyMotionArgs args;
4730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4732 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4733 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4734}
4735
Jason Gerecke489fda82012-09-07 17:19:40 -07004736TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004737 addConfigurationProperty("touch.deviceType", "touchScreen");
4738 prepareDisplay(DISPLAY_ORIENTATION_0);
4739 prepareLocationCalibration();
4740 prepareButtons();
4741 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004742 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004743
4744 int32_t rawX = 100;
4745 int32_t rawY = 200;
4746
4747 float x = toDisplayX(toCookedX(rawX, rawY));
4748 float y = toDisplayY(toCookedY(rawX, rawY));
4749
4750 processDown(mapper, rawX, rawY);
4751 processSync(mapper);
4752
4753 NotifyMotionArgs args;
4754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4756 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4757}
4758
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 addConfigurationProperty("touch.deviceType", "touchScreen");
4761 prepareDisplay(DISPLAY_ORIENTATION_0);
4762 prepareButtons();
4763 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004764 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765
4766 NotifyMotionArgs motionArgs;
4767 NotifyKeyArgs keyArgs;
4768
4769 processDown(mapper, 100, 200);
4770 processSync(mapper);
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4772 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4773 ASSERT_EQ(0, motionArgs.buttonState);
4774
4775 // press BTN_LEFT, release BTN_LEFT
4776 processKey(mapper, BTN_LEFT, 1);
4777 processSync(mapper);
4778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4780 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4781
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4784 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4785
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 processKey(mapper, BTN_LEFT, 0);
4787 processSync(mapper);
4788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004791
4792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795
4796 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4797 processKey(mapper, BTN_RIGHT, 1);
4798 processKey(mapper, BTN_MIDDLE, 1);
4799 processSync(mapper);
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4802 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4803 motionArgs.buttonState);
4804
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4807 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4808
4809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4810 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4811 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4812 motionArgs.buttonState);
4813
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814 processKey(mapper, BTN_RIGHT, 0);
4815 processSync(mapper);
4816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004817 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004819
4820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004822 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
4824 processKey(mapper, BTN_MIDDLE, 0);
4825 processSync(mapper);
4826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004827 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004829
4830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004832 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833
4834 // press BTN_BACK, release BTN_BACK
4835 processKey(mapper, BTN_BACK, 1);
4836 processSync(mapper);
4837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4838 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4839 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004840
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004843 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4844
4845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4846 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4847 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848
4849 processKey(mapper, BTN_BACK, 0);
4850 processSync(mapper);
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004854
4855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004857 ASSERT_EQ(0, motionArgs.buttonState);
4858
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4860 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4861 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4862
4863 // press BTN_SIDE, release BTN_SIDE
4864 processKey(mapper, BTN_SIDE, 1);
4865 processSync(mapper);
4866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4867 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4868 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004869
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004872 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4873
4874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4875 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4876 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877
4878 processKey(mapper, BTN_SIDE, 0);
4879 processSync(mapper);
4880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004881 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004883
4884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004886 ASSERT_EQ(0, motionArgs.buttonState);
4887
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4889 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4890 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4891
4892 // press BTN_FORWARD, release BTN_FORWARD
4893 processKey(mapper, BTN_FORWARD, 1);
4894 processSync(mapper);
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4896 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4897 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004898
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004901 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4902
4903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4905 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906
4907 processKey(mapper, BTN_FORWARD, 0);
4908 processSync(mapper);
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004912
4913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004915 ASSERT_EQ(0, motionArgs.buttonState);
4916
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4918 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4919 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4920
4921 // press BTN_EXTRA, release BTN_EXTRA
4922 processKey(mapper, BTN_EXTRA, 1);
4923 processSync(mapper);
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4925 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4926 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004927
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004930 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4931
4932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4934 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935
4936 processKey(mapper, BTN_EXTRA, 0);
4937 processSync(mapper);
4938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004941
4942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004944 ASSERT_EQ(0, motionArgs.buttonState);
4945
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4947 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4948 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4949
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4951
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 // press BTN_STYLUS, release BTN_STYLUS
4953 processKey(mapper, BTN_STYLUS, 1);
4954 processSync(mapper);
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004957 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4958
4959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4961 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962
4963 processKey(mapper, BTN_STYLUS, 0);
4964 processSync(mapper);
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004968
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004971 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972
4973 // press BTN_STYLUS2, release BTN_STYLUS2
4974 processKey(mapper, BTN_STYLUS2, 1);
4975 processSync(mapper);
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004978 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4979
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4982 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983
4984 processKey(mapper, BTN_STYLUS2, 0);
4985 processSync(mapper);
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004989
4990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004992 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993
4994 // release touch
4995 processUp(mapper);
4996 processSync(mapper);
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4998 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4999 ASSERT_EQ(0, motionArgs.buttonState);
5000}
5001
5002TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003 addConfigurationProperty("touch.deviceType", "touchScreen");
5004 prepareDisplay(DISPLAY_ORIENTATION_0);
5005 prepareButtons();
5006 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005007 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008
5009 NotifyMotionArgs motionArgs;
5010
5011 // default tool type is finger
5012 processDown(mapper, 100, 200);
5013 processSync(mapper);
5014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5015 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5016 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5017
5018 // eraser
5019 processKey(mapper, BTN_TOOL_RUBBER, 1);
5020 processSync(mapper);
5021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5024
5025 // stylus
5026 processKey(mapper, BTN_TOOL_RUBBER, 0);
5027 processKey(mapper, BTN_TOOL_PEN, 1);
5028 processSync(mapper);
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5032
5033 // brush
5034 processKey(mapper, BTN_TOOL_PEN, 0);
5035 processKey(mapper, BTN_TOOL_BRUSH, 1);
5036 processSync(mapper);
5037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5039 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5040
5041 // pencil
5042 processKey(mapper, BTN_TOOL_BRUSH, 0);
5043 processKey(mapper, BTN_TOOL_PENCIL, 1);
5044 processSync(mapper);
5045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5048
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005049 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050 processKey(mapper, BTN_TOOL_PENCIL, 0);
5051 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5052 processSync(mapper);
5053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5056
5057 // mouse
5058 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5059 processKey(mapper, BTN_TOOL_MOUSE, 1);
5060 processSync(mapper);
5061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5064
5065 // lens
5066 processKey(mapper, BTN_TOOL_MOUSE, 0);
5067 processKey(mapper, BTN_TOOL_LENS, 1);
5068 processSync(mapper);
5069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5072
5073 // double-tap
5074 processKey(mapper, BTN_TOOL_LENS, 0);
5075 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5076 processSync(mapper);
5077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5080
5081 // triple-tap
5082 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5083 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5084 processSync(mapper);
5085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5088
5089 // quad-tap
5090 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5091 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5092 processSync(mapper);
5093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5096
5097 // finger
5098 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5099 processKey(mapper, BTN_TOOL_FINGER, 1);
5100 processSync(mapper);
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5104
5105 // stylus trumps finger
5106 processKey(mapper, BTN_TOOL_PEN, 1);
5107 processSync(mapper);
5108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5111
5112 // eraser trumps stylus
5113 processKey(mapper, BTN_TOOL_RUBBER, 1);
5114 processSync(mapper);
5115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5118
5119 // mouse trumps eraser
5120 processKey(mapper, BTN_TOOL_MOUSE, 1);
5121 processSync(mapper);
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5125
5126 // back to default tool type
5127 processKey(mapper, BTN_TOOL_MOUSE, 0);
5128 processKey(mapper, BTN_TOOL_RUBBER, 0);
5129 processKey(mapper, BTN_TOOL_PEN, 0);
5130 processKey(mapper, BTN_TOOL_FINGER, 0);
5131 processSync(mapper);
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5135}
5136
5137TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138 addConfigurationProperty("touch.deviceType", "touchScreen");
5139 prepareDisplay(DISPLAY_ORIENTATION_0);
5140 prepareButtons();
5141 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005142 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005143 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144
5145 NotifyMotionArgs motionArgs;
5146
5147 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5148 processKey(mapper, BTN_TOOL_FINGER, 1);
5149 processMove(mapper, 100, 200);
5150 processSync(mapper);
5151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5152 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5157 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5159 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5160
5161 // move a little
5162 processMove(mapper, 150, 250);
5163 processSync(mapper);
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5167 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5168
5169 // down when BTN_TOUCH is pressed, pressure defaults to 1
5170 processKey(mapper, BTN_TOUCH, 1);
5171 processSync(mapper);
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5173 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5175 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5176
5177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5178 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5180 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5181
5182 // up when BTN_TOUCH is released, hover restored
5183 processKey(mapper, BTN_TOUCH, 0);
5184 processSync(mapper);
5185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5186 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5188 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5189
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5191 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5198 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5199
5200 // exit hover when pointer goes away
5201 processKey(mapper, BTN_TOOL_FINGER, 0);
5202 processSync(mapper);
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5204 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5206 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5207}
5208
5209TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210 addConfigurationProperty("touch.deviceType", "touchScreen");
5211 prepareDisplay(DISPLAY_ORIENTATION_0);
5212 prepareButtons();
5213 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005214 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215
5216 NotifyMotionArgs motionArgs;
5217
5218 // initially hovering because pressure is 0
5219 processDown(mapper, 100, 200);
5220 processPressure(mapper, 0);
5221 processSync(mapper);
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5223 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5228 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5230 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5231
5232 // move a little
5233 processMove(mapper, 150, 250);
5234 processSync(mapper);
5235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5238 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5239
5240 // down when pressure is non-zero
5241 processPressure(mapper, RAW_PRESSURE_MAX);
5242 processSync(mapper);
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5244 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5246 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5247
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5251 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5252
5253 // up when pressure becomes 0, hover restored
5254 processPressure(mapper, 0);
5255 processSync(mapper);
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5257 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5259 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5260
5261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5262 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5269 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5270
5271 // exit hover when pointer goes away
5272 processUp(mapper);
5273 processSync(mapper);
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5277 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5278}
5279
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280// --- MultiTouchInputMapperTest ---
5281
5282class MultiTouchInputMapperTest : public TouchInputMapperTest {
5283protected:
5284 void prepareAxes(int axes);
5285
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005286 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5287 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5288 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5289 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5290 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5291 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5292 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5293 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5294 void processId(MultiTouchInputMapper& mapper, int32_t id);
5295 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5296 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5297 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5298 void processMTSync(MultiTouchInputMapper& mapper);
5299 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300};
5301
5302void MultiTouchInputMapperTest::prepareAxes(int axes) {
5303 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005304 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5305 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306 }
5307 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005308 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5309 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005311 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5312 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313 }
5314 }
5315 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005316 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5317 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005319 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5320 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321 }
5322 }
5323 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005324 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5325 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326 }
5327 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005328 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5329 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330 }
5331 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005332 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5333 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334 }
5335 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005336 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5337 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338 }
5339 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005340 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5341 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342 }
5343 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005344 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345 }
5346}
5347
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005348void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5349 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005350 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5351 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352}
5353
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005354void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5355 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005356 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357}
5358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005359void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5360 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005361 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362}
5363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005364void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005365 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366}
5367
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005368void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005369 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370}
5371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005372void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5373 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005374 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375}
5376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005377void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005378 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379}
5380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005382 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383}
5384
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005385void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005386 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387}
5388
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005389void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005390 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391}
5392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005394 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395}
5396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005397void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5398 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005399 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400}
5401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005403 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404}
5405
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005406void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005407 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408}
5409
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 addConfigurationProperty("touch.deviceType", "touchScreen");
5412 prepareDisplay(DISPLAY_ORIENTATION_0);
5413 prepareAxes(POSITION);
5414 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005415 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416
5417 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5418
5419 NotifyMotionArgs motionArgs;
5420
5421 // Two fingers down at once.
5422 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5423 processPosition(mapper, x1, y1);
5424 processMTSync(mapper);
5425 processPosition(mapper, x2, y2);
5426 processMTSync(mapper);
5427 processSync(mapper);
5428
5429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5430 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5431 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5432 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5433 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5434 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5435 ASSERT_EQ(0, motionArgs.flags);
5436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5437 ASSERT_EQ(0, motionArgs.buttonState);
5438 ASSERT_EQ(0, motionArgs.edgeFlags);
5439 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5443 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5444 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5445 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5447
5448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5450 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5451 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5452 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5453 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5454 motionArgs.action);
5455 ASSERT_EQ(0, motionArgs.flags);
5456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5457 ASSERT_EQ(0, motionArgs.buttonState);
5458 ASSERT_EQ(0, motionArgs.edgeFlags);
5459 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5460 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5462 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5465 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5467 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5468 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5469 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5470 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5471
5472 // Move.
5473 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5474 processPosition(mapper, x1, y1);
5475 processMTSync(mapper);
5476 processPosition(mapper, x2, y2);
5477 processMTSync(mapper);
5478 processSync(mapper);
5479
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5481 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5482 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5483 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5484 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5486 ASSERT_EQ(0, motionArgs.flags);
5487 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5488 ASSERT_EQ(0, motionArgs.buttonState);
5489 ASSERT_EQ(0, motionArgs.edgeFlags);
5490 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5491 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5493 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5496 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5498 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5499 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5500 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5501 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5502
5503 // First finger up.
5504 x2 += 15; y2 -= 20;
5505 processPosition(mapper, x2, y2);
5506 processMTSync(mapper);
5507 processSync(mapper);
5508
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5510 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5511 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5512 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5513 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5514 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5515 motionArgs.action);
5516 ASSERT_EQ(0, motionArgs.flags);
5517 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5518 ASSERT_EQ(0, motionArgs.buttonState);
5519 ASSERT_EQ(0, motionArgs.edgeFlags);
5520 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5521 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5523 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5526 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5528 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5529 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5530 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5531 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5534 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5535 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5536 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5537 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5539 ASSERT_EQ(0, motionArgs.flags);
5540 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5541 ASSERT_EQ(0, motionArgs.buttonState);
5542 ASSERT_EQ(0, motionArgs.edgeFlags);
5543 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5544 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5547 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5548 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5549 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5550 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5551
5552 // Move.
5553 x2 += 20; y2 -= 25;
5554 processPosition(mapper, x2, y2);
5555 processMTSync(mapper);
5556 processSync(mapper);
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5560 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5561 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5562 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5564 ASSERT_EQ(0, motionArgs.flags);
5565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5566 ASSERT_EQ(0, motionArgs.buttonState);
5567 ASSERT_EQ(0, motionArgs.edgeFlags);
5568 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5569 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5572 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5573 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5574 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5575 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5576
5577 // New finger down.
5578 int32_t x3 = 700, y3 = 300;
5579 processPosition(mapper, x2, y2);
5580 processMTSync(mapper);
5581 processPosition(mapper, x3, y3);
5582 processMTSync(mapper);
5583 processSync(mapper);
5584
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5586 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5587 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5588 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5589 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5590 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5591 motionArgs.action);
5592 ASSERT_EQ(0, motionArgs.flags);
5593 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5594 ASSERT_EQ(0, motionArgs.buttonState);
5595 ASSERT_EQ(0, motionArgs.edgeFlags);
5596 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5597 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5599 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5602 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5604 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5605 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5606 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5607 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5608
5609 // Second finger up.
5610 x3 += 30; y3 -= 20;
5611 processPosition(mapper, x3, y3);
5612 processMTSync(mapper);
5613 processSync(mapper);
5614
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5616 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5617 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5618 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5619 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5620 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5621 motionArgs.action);
5622 ASSERT_EQ(0, motionArgs.flags);
5623 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5624 ASSERT_EQ(0, motionArgs.buttonState);
5625 ASSERT_EQ(0, motionArgs.edgeFlags);
5626 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5629 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5632 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5634 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5635 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5636 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5638
5639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5640 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5641 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5642 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5643 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5645 ASSERT_EQ(0, motionArgs.flags);
5646 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5647 ASSERT_EQ(0, motionArgs.buttonState);
5648 ASSERT_EQ(0, motionArgs.edgeFlags);
5649 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5653 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5654 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5655 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5656 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5657
5658 // Last finger up.
5659 processMTSync(mapper);
5660 processSync(mapper);
5661
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5663 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5664 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5665 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5666 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5667 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5668 ASSERT_EQ(0, motionArgs.flags);
5669 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5670 ASSERT_EQ(0, motionArgs.buttonState);
5671 ASSERT_EQ(0, motionArgs.edgeFlags);
5672 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5676 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5677 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5678 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5679 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5680
5681 // Should not have sent any more keys or motions.
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5684}
5685
5686TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005687 addConfigurationProperty("touch.deviceType", "touchScreen");
5688 prepareDisplay(DISPLAY_ORIENTATION_0);
5689 prepareAxes(POSITION | ID);
5690 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005691 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692
5693 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5694
5695 NotifyMotionArgs motionArgs;
5696
5697 // Two fingers down at once.
5698 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5699 processPosition(mapper, x1, y1);
5700 processId(mapper, 1);
5701 processMTSync(mapper);
5702 processPosition(mapper, x2, y2);
5703 processId(mapper, 2);
5704 processMTSync(mapper);
5705 processSync(mapper);
5706
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5708 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5709 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5710 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5713 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5714
5715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5716 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5717 motionArgs.action);
5718 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5719 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5721 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5724 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5726 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5727
5728 // Move.
5729 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5730 processPosition(mapper, x1, y1);
5731 processId(mapper, 1);
5732 processMTSync(mapper);
5733 processPosition(mapper, x2, y2);
5734 processId(mapper, 2);
5735 processMTSync(mapper);
5736 processSync(mapper);
5737
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5740 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5741 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5743 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5746 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5748 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5749
5750 // First finger up.
5751 x2 += 15; y2 -= 20;
5752 processPosition(mapper, x2, y2);
5753 processId(mapper, 2);
5754 processMTSync(mapper);
5755 processSync(mapper);
5756
5757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5758 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5759 motionArgs.action);
5760 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5761 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5763 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5766 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5768 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5769
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5772 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5773 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5776 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5777
5778 // Move.
5779 x2 += 20; y2 -= 25;
5780 processPosition(mapper, x2, y2);
5781 processId(mapper, 2);
5782 processMTSync(mapper);
5783 processSync(mapper);
5784
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5787 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5788 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5791 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5792
5793 // New finger down.
5794 int32_t x3 = 700, y3 = 300;
5795 processPosition(mapper, x2, y2);
5796 processId(mapper, 2);
5797 processMTSync(mapper);
5798 processPosition(mapper, x3, y3);
5799 processId(mapper, 3);
5800 processMTSync(mapper);
5801 processSync(mapper);
5802
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5804 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5805 motionArgs.action);
5806 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5807 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5809 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5812 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5814 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5815
5816 // Second finger up.
5817 x3 += 30; y3 -= 20;
5818 processPosition(mapper, x3, y3);
5819 processId(mapper, 3);
5820 processMTSync(mapper);
5821 processSync(mapper);
5822
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5825 motionArgs.action);
5826 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5827 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5829 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5832 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5834 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5835
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5838 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5842 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5843
5844 // Last finger up.
5845 processMTSync(mapper);
5846 processSync(mapper);
5847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5850 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5851 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5854 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5855
5856 // Should not have sent any more keys or motions.
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5859}
5860
5861TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 addConfigurationProperty("touch.deviceType", "touchScreen");
5863 prepareDisplay(DISPLAY_ORIENTATION_0);
5864 prepareAxes(POSITION | ID | SLOT);
5865 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005866 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867
5868 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5869
5870 NotifyMotionArgs motionArgs;
5871
5872 // Two fingers down at once.
5873 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5874 processPosition(mapper, x1, y1);
5875 processId(mapper, 1);
5876 processSlot(mapper, 1);
5877 processPosition(mapper, x2, y2);
5878 processId(mapper, 2);
5879 processSync(mapper);
5880
5881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5882 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5883 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5884 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5885 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5887 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5888
5889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5890 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5891 motionArgs.action);
5892 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5893 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5894 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5895 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5898 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5900 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5901
5902 // Move.
5903 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5904 processSlot(mapper, 0);
5905 processPosition(mapper, x1, y1);
5906 processSlot(mapper, 1);
5907 processPosition(mapper, x2, y2);
5908 processSync(mapper);
5909
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5912 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5913 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5915 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5918 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5920 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5921
5922 // First finger up.
5923 x2 += 15; y2 -= 20;
5924 processSlot(mapper, 0);
5925 processId(mapper, -1);
5926 processSlot(mapper, 1);
5927 processPosition(mapper, x2, y2);
5928 processSync(mapper);
5929
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5932 motionArgs.action);
5933 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5934 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5936 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5939 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5941 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5942
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5945 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5946 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5949 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5950
5951 // Move.
5952 x2 += 20; y2 -= 25;
5953 processPosition(mapper, x2, y2);
5954 processSync(mapper);
5955
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5958 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5959 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5962 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5963
5964 // New finger down.
5965 int32_t x3 = 700, y3 = 300;
5966 processPosition(mapper, x2, y2);
5967 processSlot(mapper, 0);
5968 processId(mapper, 3);
5969 processPosition(mapper, x3, y3);
5970 processSync(mapper);
5971
5972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5973 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5974 motionArgs.action);
5975 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5976 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5977 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5978 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5979 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5981 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5983 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5984
5985 // Second finger up.
5986 x3 += 30; y3 -= 20;
5987 processSlot(mapper, 1);
5988 processId(mapper, -1);
5989 processSlot(mapper, 0);
5990 processPosition(mapper, x3, y3);
5991 processSync(mapper);
5992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5994 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5995 motionArgs.action);
5996 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5997 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5999 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6002 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6004 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6005
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6008 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6009 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6012 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6013
6014 // Last finger up.
6015 processId(mapper, -1);
6016 processSync(mapper);
6017
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6019 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6020 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6021 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6024 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6025
6026 // Should not have sent any more keys or motions.
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6029}
6030
6031TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032 addConfigurationProperty("touch.deviceType", "touchScreen");
6033 prepareDisplay(DISPLAY_ORIENTATION_0);
6034 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006035 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006036
6037 // These calculations are based on the input device calibration documentation.
6038 int32_t rawX = 100;
6039 int32_t rawY = 200;
6040 int32_t rawTouchMajor = 7;
6041 int32_t rawTouchMinor = 6;
6042 int32_t rawToolMajor = 9;
6043 int32_t rawToolMinor = 8;
6044 int32_t rawPressure = 11;
6045 int32_t rawDistance = 0;
6046 int32_t rawOrientation = 3;
6047 int32_t id = 5;
6048
6049 float x = toDisplayX(rawX);
6050 float y = toDisplayY(rawY);
6051 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6052 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6053 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6054 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6055 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6056 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6057 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6058 float distance = float(rawDistance);
6059
6060 processPosition(mapper, rawX, rawY);
6061 processTouchMajor(mapper, rawTouchMajor);
6062 processTouchMinor(mapper, rawTouchMinor);
6063 processToolMajor(mapper, rawToolMajor);
6064 processToolMinor(mapper, rawToolMinor);
6065 processPressure(mapper, rawPressure);
6066 processOrientation(mapper, rawOrientation);
6067 processDistance(mapper, rawDistance);
6068 processId(mapper, id);
6069 processMTSync(mapper);
6070 processSync(mapper);
6071
6072 NotifyMotionArgs args;
6073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6074 ASSERT_EQ(0, args.pointerProperties[0].id);
6075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6076 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6077 orientation, distance));
6078}
6079
6080TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081 addConfigurationProperty("touch.deviceType", "touchScreen");
6082 prepareDisplay(DISPLAY_ORIENTATION_0);
6083 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6084 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006085 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086
6087 // These calculations are based on the input device calibration documentation.
6088 int32_t rawX = 100;
6089 int32_t rawY = 200;
6090 int32_t rawTouchMajor = 140;
6091 int32_t rawTouchMinor = 120;
6092 int32_t rawToolMajor = 180;
6093 int32_t rawToolMinor = 160;
6094
6095 float x = toDisplayX(rawX);
6096 float y = toDisplayY(rawY);
6097 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6098 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6099 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6100 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6101 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6102
6103 processPosition(mapper, rawX, rawY);
6104 processTouchMajor(mapper, rawTouchMajor);
6105 processTouchMinor(mapper, rawTouchMinor);
6106 processToolMajor(mapper, rawToolMajor);
6107 processToolMinor(mapper, rawToolMinor);
6108 processMTSync(mapper);
6109 processSync(mapper);
6110
6111 NotifyMotionArgs args;
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6114 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6115}
6116
6117TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118 addConfigurationProperty("touch.deviceType", "touchScreen");
6119 prepareDisplay(DISPLAY_ORIENTATION_0);
6120 prepareAxes(POSITION | TOUCH | TOOL);
6121 addConfigurationProperty("touch.size.calibration", "diameter");
6122 addConfigurationProperty("touch.size.scale", "10");
6123 addConfigurationProperty("touch.size.bias", "160");
6124 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006125 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126
6127 // These calculations are based on the input device calibration documentation.
6128 // Note: We only provide a single common touch/tool value because the device is assumed
6129 // not to emit separate values for each pointer (isSummed = 1).
6130 int32_t rawX = 100;
6131 int32_t rawY = 200;
6132 int32_t rawX2 = 150;
6133 int32_t rawY2 = 250;
6134 int32_t rawTouchMajor = 5;
6135 int32_t rawToolMajor = 8;
6136
6137 float x = toDisplayX(rawX);
6138 float y = toDisplayY(rawY);
6139 float x2 = toDisplayX(rawX2);
6140 float y2 = toDisplayY(rawY2);
6141 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6142 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6143 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6144
6145 processPosition(mapper, rawX, rawY);
6146 processTouchMajor(mapper, rawTouchMajor);
6147 processToolMajor(mapper, rawToolMajor);
6148 processMTSync(mapper);
6149 processPosition(mapper, rawX2, rawY2);
6150 processTouchMajor(mapper, rawTouchMajor);
6151 processToolMajor(mapper, rawToolMajor);
6152 processMTSync(mapper);
6153 processSync(mapper);
6154
6155 NotifyMotionArgs args;
6156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6157 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6158
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6160 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6161 args.action);
6162 ASSERT_EQ(size_t(2), args.pointerCount);
6163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6164 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6166 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6167}
6168
6169TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170 addConfigurationProperty("touch.deviceType", "touchScreen");
6171 prepareDisplay(DISPLAY_ORIENTATION_0);
6172 prepareAxes(POSITION | TOUCH | TOOL);
6173 addConfigurationProperty("touch.size.calibration", "area");
6174 addConfigurationProperty("touch.size.scale", "43");
6175 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006176 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177
6178 // These calculations are based on the input device calibration documentation.
6179 int32_t rawX = 100;
6180 int32_t rawY = 200;
6181 int32_t rawTouchMajor = 5;
6182 int32_t rawToolMajor = 8;
6183
6184 float x = toDisplayX(rawX);
6185 float y = toDisplayY(rawY);
6186 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6187 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6188 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6189
6190 processPosition(mapper, rawX, rawY);
6191 processTouchMajor(mapper, rawTouchMajor);
6192 processToolMajor(mapper, rawToolMajor);
6193 processMTSync(mapper);
6194 processSync(mapper);
6195
6196 NotifyMotionArgs args;
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6199 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6200}
6201
6202TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203 addConfigurationProperty("touch.deviceType", "touchScreen");
6204 prepareDisplay(DISPLAY_ORIENTATION_0);
6205 prepareAxes(POSITION | PRESSURE);
6206 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6207 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006208 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209
Michael Wrightaa449c92017-12-13 21:21:43 +00006210 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006211 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006212 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6213 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6214 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6215
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216 // These calculations are based on the input device calibration documentation.
6217 int32_t rawX = 100;
6218 int32_t rawY = 200;
6219 int32_t rawPressure = 60;
6220
6221 float x = toDisplayX(rawX);
6222 float y = toDisplayY(rawY);
6223 float pressure = float(rawPressure) * 0.01f;
6224
6225 processPosition(mapper, rawX, rawY);
6226 processPressure(mapper, rawPressure);
6227 processMTSync(mapper);
6228 processSync(mapper);
6229
6230 NotifyMotionArgs args;
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6233 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6234}
6235
6236TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 addConfigurationProperty("touch.deviceType", "touchScreen");
6238 prepareDisplay(DISPLAY_ORIENTATION_0);
6239 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006240 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241
6242 NotifyMotionArgs motionArgs;
6243 NotifyKeyArgs keyArgs;
6244
6245 processId(mapper, 1);
6246 processPosition(mapper, 100, 200);
6247 processSync(mapper);
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6250 ASSERT_EQ(0, motionArgs.buttonState);
6251
6252 // press BTN_LEFT, release BTN_LEFT
6253 processKey(mapper, BTN_LEFT, 1);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6257 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6258
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6261 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6262
Michael Wrightd02c5b62014-02-10 15:10:22 -08006263 processKey(mapper, BTN_LEFT, 0);
6264 processSync(mapper);
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006268
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272
6273 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6274 processKey(mapper, BTN_RIGHT, 1);
6275 processKey(mapper, BTN_MIDDLE, 1);
6276 processSync(mapper);
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6279 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6280 motionArgs.buttonState);
6281
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6283 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6284 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6285
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6288 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6289 motionArgs.buttonState);
6290
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291 processKey(mapper, BTN_RIGHT, 0);
6292 processSync(mapper);
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006296
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300
6301 processKey(mapper, BTN_MIDDLE, 0);
6302 processSync(mapper);
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006309 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310
6311 // press BTN_BACK, release BTN_BACK
6312 processKey(mapper, BTN_BACK, 1);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6316 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6321
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6324 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325
6326 processKey(mapper, BTN_BACK, 0);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006331
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006334 ASSERT_EQ(0, motionArgs.buttonState);
6335
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6337 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6338 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6339
6340 // press BTN_SIDE, release BTN_SIDE
6341 processKey(mapper, BTN_SIDE, 1);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6345 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6353 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
6355 processKey(mapper, BTN_SIDE, 0);
6356 processSync(mapper);
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363 ASSERT_EQ(0, motionArgs.buttonState);
6364
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6366 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6367 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6368
6369 // press BTN_FORWARD, release BTN_FORWARD
6370 processKey(mapper, BTN_FORWARD, 1);
6371 processSync(mapper);
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6373 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6374 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006378 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6379
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6382 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383
6384 processKey(mapper, BTN_FORWARD, 0);
6385 processSync(mapper);
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006392 ASSERT_EQ(0, motionArgs.buttonState);
6393
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6395 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6396 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6397
6398 // press BTN_EXTRA, release BTN_EXTRA
6399 processKey(mapper, BTN_EXTRA, 1);
6400 processSync(mapper);
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6402 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6403 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006404
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006407 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6408
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6411 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412
6413 processKey(mapper, BTN_EXTRA, 0);
6414 processSync(mapper);
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006418
6419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006421 ASSERT_EQ(0, motionArgs.buttonState);
6422
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6424 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6425 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6426
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6428
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 // press BTN_STYLUS, release BTN_STYLUS
6430 processKey(mapper, BTN_STYLUS, 1);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006434 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6435
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6438 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439
6440 processKey(mapper, BTN_STYLUS, 0);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006445
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006448 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006449
6450 // press BTN_STYLUS2, release BTN_STYLUS2
6451 processKey(mapper, BTN_STYLUS2, 1);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006455 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6459 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460
6461 processKey(mapper, BTN_STYLUS2, 0);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006466
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006469 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006470
6471 // release touch
6472 processId(mapper, -1);
6473 processSync(mapper);
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6475 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6476 ASSERT_EQ(0, motionArgs.buttonState);
6477}
6478
6479TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480 addConfigurationProperty("touch.deviceType", "touchScreen");
6481 prepareDisplay(DISPLAY_ORIENTATION_0);
6482 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006483 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006484
6485 NotifyMotionArgs motionArgs;
6486
6487 // default tool type is finger
6488 processId(mapper, 1);
6489 processPosition(mapper, 100, 200);
6490 processSync(mapper);
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6492 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6494
6495 // eraser
6496 processKey(mapper, BTN_TOOL_RUBBER, 1);
6497 processSync(mapper);
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6501
6502 // stylus
6503 processKey(mapper, BTN_TOOL_RUBBER, 0);
6504 processKey(mapper, BTN_TOOL_PEN, 1);
6505 processSync(mapper);
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6509
6510 // brush
6511 processKey(mapper, BTN_TOOL_PEN, 0);
6512 processKey(mapper, BTN_TOOL_BRUSH, 1);
6513 processSync(mapper);
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6517
6518 // pencil
6519 processKey(mapper, BTN_TOOL_BRUSH, 0);
6520 processKey(mapper, BTN_TOOL_PENCIL, 1);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6525
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006526 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527 processKey(mapper, BTN_TOOL_PENCIL, 0);
6528 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6529 processSync(mapper);
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6533
6534 // mouse
6535 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6536 processKey(mapper, BTN_TOOL_MOUSE, 1);
6537 processSync(mapper);
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6541
6542 // lens
6543 processKey(mapper, BTN_TOOL_MOUSE, 0);
6544 processKey(mapper, BTN_TOOL_LENS, 1);
6545 processSync(mapper);
6546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6549
6550 // double-tap
6551 processKey(mapper, BTN_TOOL_LENS, 0);
6552 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6557
6558 // triple-tap
6559 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6560 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6561 processSync(mapper);
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6565
6566 // quad-tap
6567 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6568 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6573
6574 // finger
6575 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6576 processKey(mapper, BTN_TOOL_FINGER, 1);
6577 processSync(mapper);
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6581
6582 // stylus trumps finger
6583 processKey(mapper, BTN_TOOL_PEN, 1);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6588
6589 // eraser trumps stylus
6590 processKey(mapper, BTN_TOOL_RUBBER, 1);
6591 processSync(mapper);
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6595
6596 // mouse trumps eraser
6597 processKey(mapper, BTN_TOOL_MOUSE, 1);
6598 processSync(mapper);
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6602
6603 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6604 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6605 processSync(mapper);
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6609
6610 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6611 processToolType(mapper, MT_TOOL_PEN);
6612 processSync(mapper);
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6616
6617 // back to default tool type
6618 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6619 processKey(mapper, BTN_TOOL_MOUSE, 0);
6620 processKey(mapper, BTN_TOOL_RUBBER, 0);
6621 processKey(mapper, BTN_TOOL_PEN, 0);
6622 processKey(mapper, BTN_TOOL_FINGER, 0);
6623 processSync(mapper);
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6627}
6628
6629TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630 addConfigurationProperty("touch.deviceType", "touchScreen");
6631 prepareDisplay(DISPLAY_ORIENTATION_0);
6632 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006633 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006634 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006635
6636 NotifyMotionArgs motionArgs;
6637
6638 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6639 processId(mapper, 1);
6640 processPosition(mapper, 100, 200);
6641 processSync(mapper);
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6648 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6650 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6651
6652 // move a little
6653 processPosition(mapper, 150, 250);
6654 processSync(mapper);
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6656 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6658 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6659
6660 // down when BTN_TOUCH is pressed, pressure defaults to 1
6661 processKey(mapper, BTN_TOUCH, 1);
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6666 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6667
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6671 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6672
6673 // up when BTN_TOUCH is released, hover restored
6674 processKey(mapper, BTN_TOUCH, 0);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6679 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6680
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6687 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6689 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6690
6691 // exit hover when pointer goes away
6692 processId(mapper, -1);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6698}
6699
6700TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006701 addConfigurationProperty("touch.deviceType", "touchScreen");
6702 prepareDisplay(DISPLAY_ORIENTATION_0);
6703 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006704 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705
6706 NotifyMotionArgs motionArgs;
6707
6708 // initially hovering because pressure is 0
6709 processId(mapper, 1);
6710 processPosition(mapper, 100, 200);
6711 processPressure(mapper, 0);
6712 processSync(mapper);
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6721 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6722
6723 // move a little
6724 processPosition(mapper, 150, 250);
6725 processSync(mapper);
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6727 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6729 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6730
6731 // down when pressure becomes non-zero
6732 processPressure(mapper, RAW_PRESSURE_MAX);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6738
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6740 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6742 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6743
6744 // up when pressure becomes 0, hover restored
6745 processPressure(mapper, 0);
6746 processSync(mapper);
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6751
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6758 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6760 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6761
6762 // exit hover when pointer goes away
6763 processId(mapper, -1);
6764 processSync(mapper);
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6768 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6769}
6770
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006771/**
6772 * Set the input device port <--> display port associations, and check that the
6773 * events are routed to the display that matches the display port.
6774 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6775 */
6776TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006777 const std::string usb2 = "USB2";
6778 const uint8_t hdmi1 = 0;
6779 const uint8_t hdmi2 = 1;
6780 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006781 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006782
6783 addConfigurationProperty("touch.deviceType", "touchScreen");
6784 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006785 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006786
6787 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6788 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6789
6790 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6791 // for this input device is specified, and the matching viewport is not present,
6792 // the input device should be disabled (at the mapper level).
6793
6794 // Add viewport for display 2 on hdmi2
6795 prepareSecondaryDisplay(type, hdmi2);
6796 // Send a touch event
6797 processPosition(mapper, 100, 100);
6798 processSync(mapper);
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6800
6801 // Add viewport for display 1 on hdmi1
6802 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6803 // Send a touch event again
6804 processPosition(mapper, 100, 100);
6805 processSync(mapper);
6806
6807 NotifyMotionArgs args;
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6809 ASSERT_EQ(DISPLAY_ID, args.displayId);
6810}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006811
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006812TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006813 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006814 std::shared_ptr<FakePointerController> fakePointerController =
6815 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006816 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006817 fakePointerController->setPosition(100, 200);
6818 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006819 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6820
Garfield Tan888a6a42020-01-09 11:39:16 -08006821 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006822 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006823
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006824 prepareDisplay(DISPLAY_ORIENTATION_0);
6825 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006826 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006827
6828 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006829 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006830
6831 NotifyMotionArgs motionArgs;
6832 processPosition(mapper, 100, 100);
6833 processSync(mapper);
6834
6835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6837 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6838}
6839
Arthur Hung7c645402019-01-25 17:45:42 +08006840TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6841 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006842 prepareAxes(POSITION | ID | SLOT);
6843 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006844 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006845
6846 // Create the second touch screen device, and enable multi fingers.
6847 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006848 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006849 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006850 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006851 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006852 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006853 std::unique_ptr<InputDevice> device2 =
6854 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006855 identifier);
6856 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6857 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6858 0 /*flat*/, 0 /*fuzz*/);
6859 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6860 0 /*flat*/, 0 /*fuzz*/);
6861 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6862 0 /*flat*/, 0 /*fuzz*/);
6863 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6864 0 /*flat*/, 0 /*fuzz*/);
6865 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6866 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6867 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006868
6869 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006870 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006871 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6872 device2->reset(ARBITRARY_TIME);
6873
6874 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006875 std::shared_ptr<FakePointerController> fakePointerController =
6876 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006877 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6878 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6879
6880 // Setup policy for associated displays and show touches.
6881 const uint8_t hdmi1 = 0;
6882 const uint8_t hdmi2 = 1;
6883 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6884 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6885 mFakePolicy->setShowTouches(true);
6886
6887 // Create displays.
6888 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006889 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006890
6891 // Default device will reconfigure above, need additional reconfiguration for another device.
6892 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006893 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006894
6895 // Two fingers down at default display.
6896 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6897 processPosition(mapper, x1, y1);
6898 processId(mapper, 1);
6899 processSlot(mapper, 1);
6900 processPosition(mapper, x2, y2);
6901 processId(mapper, 2);
6902 processSync(mapper);
6903
6904 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6905 fakePointerController->getSpots().find(DISPLAY_ID);
6906 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6907 ASSERT_EQ(size_t(2), iter->second.size());
6908
6909 // Two fingers down at second display.
6910 processPosition(mapper2, x1, y1);
6911 processId(mapper2, 1);
6912 processSlot(mapper2, 1);
6913 processPosition(mapper2, x2, y2);
6914 processId(mapper2, 2);
6915 processSync(mapper2);
6916
6917 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6918 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6919 ASSERT_EQ(size_t(2), iter->second.size());
6920}
6921
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006922TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006923 prepareAxes(POSITION);
6924 addConfigurationProperty("touch.deviceType", "touchScreen");
6925 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006926 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006927
6928 NotifyMotionArgs motionArgs;
6929 // Unrotated video frame
6930 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6931 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006932 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006933 processPosition(mapper, 100, 200);
6934 processSync(mapper);
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6936 ASSERT_EQ(frames, motionArgs.videoFrames);
6937
6938 // Subsequent touch events should not have any videoframes
6939 // This is implemented separately in FakeEventHub,
6940 // but that should match the behaviour of TouchVideoDevice.
6941 processPosition(mapper, 200, 200);
6942 processSync(mapper);
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6944 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6945}
6946
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006947TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006948 prepareAxes(POSITION);
6949 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006950 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006951 // Unrotated video frame
6952 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6953 NotifyMotionArgs motionArgs;
6954
6955 // Test all 4 orientations
6956 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6957 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6958 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6959 clearViewports();
6960 prepareDisplay(orientation);
6961 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006962 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006963 processPosition(mapper, 100, 200);
6964 processSync(mapper);
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6966 frames[0].rotate(orientation);
6967 ASSERT_EQ(frames, motionArgs.videoFrames);
6968 }
6969}
6970
6971TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006972 prepareAxes(POSITION);
6973 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006974 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006975 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6976 // so mix these.
6977 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6978 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6979 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6980 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6981 NotifyMotionArgs motionArgs;
6982
6983 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006984 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006985 processPosition(mapper, 100, 200);
6986 processSync(mapper);
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6988 std::for_each(frames.begin(), frames.end(),
6989 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6990 ASSERT_EQ(frames, motionArgs.videoFrames);
6991}
6992
Arthur Hung9da14732019-09-02 16:16:58 +08006993/**
6994 * If we had defined port associations, but the viewport is not ready, the touch device would be
6995 * expected to be disabled, and it should be enabled after the viewport has found.
6996 */
6997TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006998 constexpr uint8_t hdmi2 = 1;
6999 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007000 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007001
7002 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7003
7004 addConfigurationProperty("touch.deviceType", "touchScreen");
7005 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007006 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007007
7008 ASSERT_EQ(mDevice->isEnabled(), false);
7009
7010 // Add display on hdmi2, the device should be enabled and can receive touch event.
7011 prepareSecondaryDisplay(type, hdmi2);
7012 ASSERT_EQ(mDevice->isEnabled(), true);
7013
7014 // Send a touch event.
7015 processPosition(mapper, 100, 100);
7016 processSync(mapper);
7017
7018 NotifyMotionArgs args;
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7020 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7021}
7022
Arthur Hung6cd19a42019-08-30 19:04:12 +08007023
Arthur Hung6cd19a42019-08-30 19:04:12 +08007024
Arthur Hung421eb1c2020-01-16 00:09:42 +08007025TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007026 addConfigurationProperty("touch.deviceType", "touchScreen");
7027 prepareDisplay(DISPLAY_ORIENTATION_0);
7028 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007029 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007030
7031 NotifyMotionArgs motionArgs;
7032
7033 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7034 // finger down
7035 processId(mapper, 1);
7036 processPosition(mapper, x1, y1);
7037 processSync(mapper);
7038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7039 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7041
7042 // finger move
7043 processId(mapper, 1);
7044 processPosition(mapper, x2, y2);
7045 processSync(mapper);
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7048 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7049
7050 // finger up.
7051 processId(mapper, -1);
7052 processSync(mapper);
7053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7054 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7056
7057 // new finger down
7058 processId(mapper, 1);
7059 processPosition(mapper, x3, y3);
7060 processSync(mapper);
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7062 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7064}
7065
7066/**
7067 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
7068 * UP events should be ignored.
7069 */
7070TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007071 addConfigurationProperty("touch.deviceType", "touchScreen");
7072 prepareDisplay(DISPLAY_ORIENTATION_0);
7073 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007074 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007075
7076 NotifyMotionArgs motionArgs;
7077
7078 // default tool type is finger
7079 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7080 processId(mapper, 1);
7081 processPosition(mapper, x1, y1);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7086
7087 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7088 processToolType(mapper, MT_TOOL_PALM);
7089 processSync(mapper);
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7091 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7092
7093 // Ignore the following MOVE and UP events if had detect a palm event.
7094 processId(mapper, 1);
7095 processPosition(mapper, x2, y2);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7098
7099 // finger up.
7100 processId(mapper, -1);
7101 processSync(mapper);
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7103
7104 // new finger down
7105 processToolType(mapper, MT_TOOL_FINGER);
7106 processId(mapper, 1);
7107 processPosition(mapper, x3, y3);
7108 processSync(mapper);
7109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7110 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7112}
7113
arthurhungbf89a482020-04-17 17:37:55 +08007114/**
7115 * Test multi-touch should be canceled when received the MT_TOOL_PALM event from some finger,
7116 * and could be allowed again after all non-MT_TOOL_PALM is release and the new point is
7117 * MT_TOOL_FINGER.
7118 */
7119TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType2) {
7120 addConfigurationProperty("touch.deviceType", "touchScreen");
7121 prepareDisplay(DISPLAY_ORIENTATION_0);
7122 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7123 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7124
7125 NotifyMotionArgs motionArgs;
7126
7127 // default tool type is finger
7128 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7129 processId(mapper, 1);
7130 processPosition(mapper, x1, y1);
7131 processSync(mapper);
7132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7133 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7135
7136 // Second finger down.
7137 processSlot(mapper, 1);
7138 processPosition(mapper, x2, y2);
7139 processId(mapper, 2);
7140 processSync(mapper);
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7142 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7143 motionArgs.action);
7144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7145
7146 // If the tool type of the first pointer changes to MT_TOOL_PALM,
7147 // the entire gesture should be aborted, so we expect to receive ACTION_CANCEL.
7148 processSlot(mapper, 0);
7149 processId(mapper, 1);
7150 processToolType(mapper, MT_TOOL_PALM);
7151 processSync(mapper);
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7153 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7154
7155 // Ignore the following MOVE and UP events if had detect a palm event.
7156 processSlot(mapper, 1);
7157 processId(mapper, 2);
7158 processPosition(mapper, x3, y3);
7159 processSync(mapper);
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7161
7162 // second finger up.
7163 processId(mapper, -1);
7164 processSync(mapper);
7165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7166
7167 // first finger move, but still in palm
7168 processSlot(mapper, 0);
7169 processId(mapper, 1);
7170 processPosition(mapper, x1 - 1, y1 - 1);
7171 processSync(mapper);
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7173
7174 // second finger down, expect as new finger down.
7175 processSlot(mapper, 1);
7176 processId(mapper, 2);
7177 processPosition(mapper, x2, y2);
7178 processSync(mapper);
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7180 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7182}
7183
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007184// --- MultiTouchInputMapperTest_ExternalDevice ---
7185
7186class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7187protected:
7188 virtual void SetUp() override {
7189 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7190 }
7191};
7192
7193/**
7194 * Expect fallback to internal viewport if device is external and external viewport is not present.
7195 */
7196TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7197 prepareAxes(POSITION);
7198 addConfigurationProperty("touch.deviceType", "touchScreen");
7199 prepareDisplay(DISPLAY_ORIENTATION_0);
7200 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7201
7202 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7203
7204 NotifyMotionArgs motionArgs;
7205
7206 // Expect the event to be sent to the internal viewport,
7207 // because an external viewport is not present.
7208 processPosition(mapper, 100, 100);
7209 processSync(mapper);
7210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7211 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7212
7213 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007214 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007215 processPosition(mapper, 100, 100);
7216 processSync(mapper);
7217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7218 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7219}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007220
7221/**
7222 * Test touch should not work if outside of surface.
7223 */
7224class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7225protected:
7226 void halfDisplayToCenterHorizontal(int32_t orientation) {
7227 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007228 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007229
7230 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7231 internalViewport->orientation = orientation;
7232 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7233 internalViewport->logicalLeft = 0;
7234 internalViewport->logicalTop = 0;
7235 internalViewport->logicalRight = DISPLAY_HEIGHT;
7236 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7237
7238 internalViewport->physicalLeft = 0;
7239 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7240 internalViewport->physicalRight = DISPLAY_HEIGHT;
7241 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7242
7243 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7244 internalViewport->deviceHeight = DISPLAY_WIDTH;
7245 } else {
7246 internalViewport->logicalLeft = 0;
7247 internalViewport->logicalTop = 0;
7248 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7249 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7250
7251 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7252 internalViewport->physicalTop = 0;
7253 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7254 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7255
7256 internalViewport->deviceWidth = DISPLAY_WIDTH;
7257 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7258 }
7259
7260 mFakePolicy->updateViewport(internalViewport.value());
7261 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7262 }
7263
7264 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7265 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7266 int32_t yExpected) {
7267 // touch on outside area should not work.
7268 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7269 processSync(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7271
7272 // touch on inside area should receive the event.
7273 NotifyMotionArgs args;
7274 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7277 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7278 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7279
7280 // Reset.
7281 mapper.reset(ARBITRARY_TIME);
7282 }
7283};
7284
7285TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7286 addConfigurationProperty("touch.deviceType", "touchScreen");
7287 prepareDisplay(DISPLAY_ORIENTATION_0);
7288 prepareAxes(POSITION);
7289 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7290
7291 // Touch on center of normal display should work.
7292 const int32_t x = DISPLAY_WIDTH / 4;
7293 const int32_t y = DISPLAY_HEIGHT / 2;
7294 processPosition(mapper, toRawX(x), toRawY(y));
7295 processSync(mapper);
7296 NotifyMotionArgs args;
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7299 0.0f, 0.0f, 0.0f, 0.0f));
7300 // Reset.
7301 mapper.reset(ARBITRARY_TIME);
7302
7303 // Let physical display be different to device, and make surface and physical could be 1:1.
7304 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7305
7306 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7307 const int32_t yExpected = y;
7308 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7309}
7310
7311TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7312 addConfigurationProperty("touch.deviceType", "touchScreen");
7313 prepareDisplay(DISPLAY_ORIENTATION_0);
7314 prepareAxes(POSITION);
7315 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7316
7317 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7318 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7319
7320 const int32_t x = DISPLAY_WIDTH / 4;
7321 const int32_t y = DISPLAY_HEIGHT / 2;
7322
7323 // expect x/y = swap x/y then reverse y.
7324 const int32_t xExpected = y;
7325 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7326 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7327}
7328
7329TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7330 addConfigurationProperty("touch.deviceType", "touchScreen");
7331 prepareDisplay(DISPLAY_ORIENTATION_0);
7332 prepareAxes(POSITION);
7333 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7334
7335 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7336 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7337
7338 const int32_t x = DISPLAY_WIDTH / 4;
7339 const int32_t y = DISPLAY_HEIGHT / 2;
7340
7341 // expect x/y = swap x/y then reverse x.
7342 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7343 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7344 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7345}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007346} // namespace android