blob: 7614790cb60d0b2af419eebb57b7914adb5459d9 [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
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000059static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080060static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000061static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_WIDTH = 480;
63static constexpr int32_t DISPLAY_HEIGHT = 800;
64static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
65static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
66static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070067static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070068static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080069
arthurhungcc7f9802020-04-30 17:55:40 +080070static constexpr int32_t FIRST_SLOT = 0;
71static constexpr int32_t SECOND_SLOT = 1;
72static constexpr int32_t THIRD_SLOT = 2;
73static constexpr int32_t INVALID_TRACKING_ID = -1;
74static constexpr int32_t FIRST_TRACKING_ID = 0;
75static constexpr int32_t SECOND_TRACKING_ID = 1;
76static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080077static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080078static constexpr int32_t BATTERY_STATUS = 4;
79static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080080static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
81static constexpr int32_t LIGHT_COLOR = 0x7F448866;
82static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080083
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080084static constexpr int32_t ACTION_POINTER_0_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_DOWN =
89 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92
Michael Wrightd02c5b62014-02-10 15:10:22 -080093// Error tolerance for floating point assertions.
94static const float EPSILON = 0.001f;
95
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000096using ::testing::AllOf;
97
98MATCHER_P(WithAction, action, "InputEvent with specified action") {
99 return arg.action == action;
100}
101
102MATCHER_P(WithSource, source, "InputEvent with specified source") {
103 return arg.source == source;
104}
105
106MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
107 return arg.displayId == displayId;
108}
109
110MATCHER_P2(WithCoords, x, y, "MotionEvent with specified action") {
111 return arg.pointerCoords[0].getX() == x && arg.pointerCoords[0].getY();
112}
113
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114template<typename T>
115static inline T min(T a, T b) {
116 return a < b ? a : b;
117}
118
119static inline float avg(float x, float y) {
120 return (x + y) / 2;
121}
122
Chris Ye3fdbfef2021-01-06 18:45:18 -0800123// Mapping for light color name and the light color
124const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
125 {"green", LightColor::GREEN},
126 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700128static int32_t getInverseRotation(int32_t orientation) {
129 switch (orientation) {
130 case DISPLAY_ORIENTATION_90:
131 return DISPLAY_ORIENTATION_270;
132 case DISPLAY_ORIENTATION_270:
133 return DISPLAY_ORIENTATION_90;
134 default:
135 return orientation;
136 }
137}
138
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800139static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
140 InputDeviceInfo info;
141 mapper.populateDeviceInfo(&info);
142
143 const InputDeviceInfo::MotionRange* motionRange =
144 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
145 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
146}
147
148static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
149 InputDeviceInfo info;
150 mapper.populateDeviceInfo(&info);
151
152 const InputDeviceInfo::MotionRange* motionRange =
153 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
154 ASSERT_EQ(nullptr, motionRange);
155}
156
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157// --- FakePointerController ---
158
159class FakePointerController : public PointerControllerInterface {
160 bool mHaveBounds;
161 float mMinX, mMinY, mMaxX, mMaxY;
162 float mX, mY;
163 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800164 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166public:
167 FakePointerController() :
168 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800169 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 }
171
Michael Wright17db18e2020-06-26 20:51:44 +0100172 virtual ~FakePointerController() {}
173
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174 void setBounds(float minX, float minY, float maxX, float maxY) {
175 mHaveBounds = true;
176 mMinX = minX;
177 mMinY = minY;
178 mMaxX = maxX;
179 mMaxY = maxY;
180 }
181
Chris Yea52ade12020-08-27 16:49:20 -0700182 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183 mX = x;
184 mY = y;
185 }
186
Chris Yea52ade12020-08-27 16:49:20 -0700187 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
Chris Yea52ade12020-08-27 16:49:20 -0700191 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 *outX = mX;
193 *outY = mY;
194 }
195
Chris Yea52ade12020-08-27 16:49:20 -0700196 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800197
Chris Yea52ade12020-08-27 16:49:20 -0700198 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800199 mDisplayId = viewport.displayId;
200 }
201
Arthur Hung7c645402019-01-25 17:45:42 +0800202 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
203 return mSpotsByDisplay;
204 }
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206private:
Chris Yea52ade12020-08-27 16:49:20 -0700207 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 *outMinX = mMinX;
209 *outMinY = mMinY;
210 *outMaxX = mMaxX;
211 *outMaxY = mMaxY;
212 return mHaveBounds;
213 }
214
Chris Yea52ade12020-08-27 16:49:20 -0700215 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 mX += deltaX;
217 if (mX < mMinX) mX = mMinX;
218 if (mX > mMaxX) mX = mMaxX;
219 mY += deltaY;
220 if (mY < mMinY) mY = mMinY;
221 if (mY > mMaxY) mY = mMaxY;
222 }
223
Chris Yea52ade12020-08-27 16:49:20 -0700224 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225
Chris Yea52ade12020-08-27 16:49:20 -0700226 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
Chris Yea52ade12020-08-27 16:49:20 -0700228 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229
Chris Yea52ade12020-08-27 16:49:20 -0700230 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
231 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800232 std::vector<int32_t> newSpots;
233 // Add spots for fingers that are down.
234 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
235 uint32_t id = idBits.clearFirstMarkedBit();
236 newSpots.push_back(id);
237 }
238
239 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240 }
241
Chris Yea52ade12020-08-27 16:49:20 -0700242 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800243
244 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245};
246
247
248// --- FakeInputReaderPolicy ---
249
250class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700251 std::mutex mLock;
252 std::condition_variable mDevicesChangedCondition;
253
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000255 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700256 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
257 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100258 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700259 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260
261protected:
Chris Yea52ade12020-08-27 16:49:20 -0700262 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263
264public:
265 FakeInputReaderPolicy() {
266 }
267
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700268 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800269 waitForInputDevices([](bool devicesChanged) {
270 if (!devicesChanged) {
271 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
272 }
273 });
274 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700275
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800276 void assertInputDevicesNotChanged() {
277 waitForInputDevices([](bool devicesChanged) {
278 if (devicesChanged) {
279 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
280 }
281 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700282 }
283
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700284 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100285 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100286 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700287 }
288
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700289 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
290 return mConfig.getDisplayViewportByUniqueId(uniqueId);
291 }
292 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
293 return mConfig.getDisplayViewportByType(type);
294 }
295
296 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
297 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700298 }
299
Prabir Pradhan5632d622021-09-06 07:57:20 -0700300 void addDisplayViewport(DisplayViewport viewport) {
301 mViewports.push_back(std::move(viewport));
302 mConfig.setDisplayViewports(mViewports);
303 }
304
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700305 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000306 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700307 std::optional<uint8_t> physicalPort, ViewportType type) {
308 const bool isRotated =
309 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
310 DisplayViewport v;
311 v.displayId = displayId;
312 v.orientation = orientation;
313 v.logicalLeft = 0;
314 v.logicalTop = 0;
315 v.logicalRight = isRotated ? height : width;
316 v.logicalBottom = isRotated ? width : height;
317 v.physicalLeft = 0;
318 v.physicalTop = 0;
319 v.physicalRight = isRotated ? height : width;
320 v.physicalBottom = isRotated ? width : height;
321 v.deviceWidth = isRotated ? height : width;
322 v.deviceHeight = isRotated ? width : height;
323 v.isActive = isActive;
324 v.uniqueId = uniqueId;
325 v.physicalPort = physicalPort;
326 v.type = type;
327
328 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 }
330
Arthur Hung6cd19a42019-08-30 19:04:12 +0800331 bool updateViewport(const DisplayViewport& viewport) {
332 size_t count = mViewports.size();
333 for (size_t i = 0; i < count; i++) {
334 const DisplayViewport& currentViewport = mViewports[i];
335 if (currentViewport.displayId == viewport.displayId) {
336 mViewports[i] = viewport;
337 mConfig.setDisplayViewports(mViewports);
338 return true;
339 }
340 }
341 // no viewport found.
342 return false;
343 }
344
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100345 void addExcludedDeviceName(const std::string& deviceName) {
346 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700349 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
350 mConfig.portAssociations.insert({inputPort, displayPort});
351 }
352
Christine Franks1ba71cc2021-04-07 14:37:42 -0700353 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
354 const std::string& displayUniqueId) {
355 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
356 }
357
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000358 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700359
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000360 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700361
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000362 void setPointerController(std::shared_ptr<FakePointerController> controller) {
363 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 }
365
366 const InputReaderConfiguration* getReaderConfiguration() const {
367 return &mConfig;
368 }
369
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800370 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 return mInputDevices;
372 }
373
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100374 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700375 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700376 return transform;
377 }
378
379 void setTouchAffineTransformation(const TouchAffineTransformation t) {
380 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800381 }
382
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000383 PointerCaptureRequest setPointerCapture(bool enabled) {
384 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
385 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800386 }
387
Arthur Hung7c645402019-01-25 17:45:42 +0800388 void setShowTouches(bool enabled) {
389 mConfig.showTouches = enabled;
390 }
391
Garfield Tan888a6a42020-01-09 11:39:16 -0800392 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
393 mConfig.defaultPointerDisplayId = pointerDisplayId;
394 }
395
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800396 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
397
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000398 void setVelocityControlParams(const VelocityControlParameters& params) {
399 mConfig.pointerVelocityControlParameters = params;
400 mConfig.wheelVelocityControlParameters = params;
401 }
402
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000404 uint32_t mNextPointerCaptureSequenceNumber = 0;
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 *outConfig = mConfig;
408 }
409
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000410 std::shared_ptr<PointerControllerInterface> obtainPointerController(
411 int32_t /*deviceId*/) override {
412 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413 }
414
Chris Yea52ade12020-08-27 16:49:20 -0700415 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700416 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700418 mInputDevicesChanged = true;
419 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 }
421
Chris Yea52ade12020-08-27 16:49:20 -0700422 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
423 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700424 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425 }
426
Chris Yea52ade12020-08-27 16:49:20 -0700427 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800428
429 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
430 std::unique_lock<std::mutex> lock(mLock);
431 base::ScopedLockAssertion assumeLocked(mLock);
432
433 const bool devicesChanged =
434 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
435 return mInputDevicesChanged;
436 });
437 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
438 mInputDevicesChanged = false;
439 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440};
441
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442// --- FakeEventHub ---
443
444class FakeEventHub : public EventHubInterface {
445 struct KeyInfo {
446 int32_t keyCode;
447 uint32_t flags;
448 };
449
Chris Yef59a2f42020-10-16 12:55:26 -0700450 struct SensorInfo {
451 InputDeviceSensorType sensorType;
452 int32_t sensorDataIndex;
453 };
454
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 struct Device {
456 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700457 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 PropertyMap configuration;
459 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
460 KeyedVector<int, bool> relativeAxes;
461 KeyedVector<int32_t, int32_t> keyCodeStates;
462 KeyedVector<int32_t, int32_t> scanCodeStates;
463 KeyedVector<int32_t, int32_t> switchStates;
464 KeyedVector<int32_t, int32_t> absoluteAxisValue;
465 KeyedVector<int32_t, KeyInfo> keysByScanCode;
466 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
467 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100468 // fake mapping which would normally come from keyCharacterMap
469 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700470 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
471 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800472 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700473 bool enabled;
474
475 status_t enable() {
476 enabled = true;
477 return OK;
478 }
479
480 status_t disable() {
481 enabled = false;
482 return OK;
483 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700485 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486 };
487
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700488 std::mutex mLock;
489 std::condition_variable mEventsCondition;
490
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100492 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000493 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600494 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000495 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800496 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
497 // Simulates a device light brightness, from light id to light brightness.
498 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
499 // Simulates a device light intensities, from light id to light intensities map.
500 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
501 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700503public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 virtual ~FakeEventHub() {
505 for (size_t i = 0; i < mDevices.size(); i++) {
506 delete mDevices.valueAt(i);
507 }
508 }
509
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 FakeEventHub() { }
511
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700512 void addDevice(int32_t deviceId, const std::string& name,
513 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 Device* device = new Device(classes);
515 device->identifier.name = name;
516 mDevices.add(deviceId, device);
517
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000518 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 }
520
521 void removeDevice(int32_t deviceId) {
522 delete mDevices.valueFor(deviceId);
523 mDevices.removeItem(deviceId);
524
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000525 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526 }
527
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700528 bool isDeviceEnabled(int32_t deviceId) {
529 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700530 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700531 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
532 return false;
533 }
534 return device->enabled;
535 }
536
537 status_t enableDevice(int32_t deviceId) {
538 status_t result;
539 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700540 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700541 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
542 return BAD_VALUE;
543 }
544 if (device->enabled) {
545 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
546 return OK;
547 }
548 result = device->enable();
549 return result;
550 }
551
552 status_t disableDevice(int32_t deviceId) {
553 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700554 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700555 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
556 return BAD_VALUE;
557 }
558 if (!device->enabled) {
559 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
560 return OK;
561 }
562 return device->disable();
563 }
564
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000566 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 }
568
569 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
570 Device* device = getDevice(deviceId);
571 device->configuration.addProperty(key, value);
572 }
573
574 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
575 Device* device = getDevice(deviceId);
576 device->configuration.addAll(configuration);
577 }
578
579 void addAbsoluteAxis(int32_t deviceId, int axis,
580 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
581 Device* device = getDevice(deviceId);
582
583 RawAbsoluteAxisInfo info;
584 info.valid = true;
585 info.minValue = minValue;
586 info.maxValue = maxValue;
587 info.flat = flat;
588 info.fuzz = fuzz;
589 info.resolution = resolution;
590 device->absoluteAxes.add(axis, info);
591 }
592
593 void addRelativeAxis(int32_t deviceId, int32_t axis) {
594 Device* device = getDevice(deviceId);
595 device->relativeAxes.add(axis, true);
596 }
597
598 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
599 Device* device = getDevice(deviceId);
600 device->keyCodeStates.replaceValueFor(keyCode, state);
601 }
602
603 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
604 Device* device = getDevice(deviceId);
605 device->scanCodeStates.replaceValueFor(scanCode, state);
606 }
607
608 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
609 Device* device = getDevice(deviceId);
610 device->switchStates.replaceValueFor(switchCode, state);
611 }
612
613 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
614 Device* device = getDevice(deviceId);
615 device->absoluteAxisValue.replaceValueFor(axis, value);
616 }
617
618 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
619 int32_t keyCode, uint32_t flags) {
620 Device* device = getDevice(deviceId);
621 KeyInfo info;
622 info.keyCode = keyCode;
623 info.flags = flags;
624 if (scanCode) {
625 device->keysByScanCode.add(scanCode, info);
626 }
627 if (usageCode) {
628 device->keysByUsageCode.add(usageCode, info);
629 }
630 }
631
Philip Junker4af3b3d2021-12-14 10:36:55 +0100632 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
633 Device* device = getDevice(deviceId);
634 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
635 }
636
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 void addLed(int32_t deviceId, int32_t led, bool initialState) {
638 Device* device = getDevice(deviceId);
639 device->leds.add(led, initialState);
640 }
641
Chris Yef59a2f42020-10-16 12:55:26 -0700642 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
643 int32_t sensorDataIndex) {
644 Device* device = getDevice(deviceId);
645 SensorInfo info;
646 info.sensorType = sensorType;
647 info.sensorDataIndex = sensorDataIndex;
648 device->sensorsByAbsCode.emplace(absCode, info);
649 }
650
651 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
652 Device* device = getDevice(deviceId);
653 typename BitArray<MSC_MAX>::Buffer buffer;
654 buffer[mscEvent / 32] = 1 << mscEvent % 32;
655 device->mscBitmask.loadFromBuffer(buffer);
656 }
657
Chris Ye3fdbfef2021-01-06 18:45:18 -0800658 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
659 mRawLightInfos.emplace(rawId, std::move(info));
660 }
661
662 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
663 mLightBrightness.emplace(rawId, brightness);
664 }
665
666 void fakeLightIntensities(int32_t rawId,
667 const std::unordered_map<LightColor, int32_t> intensities) {
668 mLightIntensities.emplace(rawId, std::move(intensities));
669 }
670
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 bool getLedState(int32_t deviceId, int32_t led) {
672 Device* device = getDevice(deviceId);
673 return device->leds.valueFor(led);
674 }
675
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100676 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 return mExcludedDevices;
678 }
679
680 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
681 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800682 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 }
684
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000685 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
686 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700687 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 RawEvent event;
689 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000690 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 event.deviceId = deviceId;
692 event.type = type;
693 event.code = code;
694 event.value = value;
695 mEvents.push_back(event);
696
697 if (type == EV_ABS) {
698 setAbsoluteAxisValue(deviceId, code, value);
699 }
700 }
701
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600702 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
703 std::vector<TouchVideoFrame>> videoFrames) {
704 mVideoFrames = std::move(videoFrames);
705 }
706
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700708 std::unique_lock<std::mutex> lock(mLock);
709 base::ScopedLockAssertion assumeLocked(mLock);
710 const bool queueIsEmpty =
711 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
712 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
713 if (!queueIsEmpty) {
714 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
715 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 }
717
718private:
719 Device* getDevice(int32_t deviceId) const {
720 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100721 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 }
723
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700724 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700726 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 }
728
Chris Yea52ade12020-08-27 16:49:20 -0700729 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 Device* device = getDevice(deviceId);
731 return device ? device->identifier : InputDeviceIdentifier();
732 }
733
Chris Yea52ade12020-08-27 16:49:20 -0700734 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735
Chris Yea52ade12020-08-27 16:49:20 -0700736 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 Device* device = getDevice(deviceId);
738 if (device) {
739 *outConfiguration = device->configuration;
740 }
741 }
742
Chris Yea52ade12020-08-27 16:49:20 -0700743 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
744 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800746 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 ssize_t index = device->absoluteAxes.indexOfKey(axis);
748 if (index >= 0) {
749 *outAxisInfo = device->absoluteAxes.valueAt(index);
750 return OK;
751 }
752 }
753 outAxisInfo->clear();
754 return -1;
755 }
756
Chris Yea52ade12020-08-27 16:49:20 -0700757 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 Device* device = getDevice(deviceId);
759 if (device) {
760 return device->relativeAxes.indexOfKey(axis) >= 0;
761 }
762 return false;
763 }
764
Chris Yea52ade12020-08-27 16:49:20 -0700765 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766
Chris Yef59a2f42020-10-16 12:55:26 -0700767 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
768 Device* device = getDevice(deviceId);
769 if (device) {
770 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
771 }
772 return false;
773 }
774
Chris Yea52ade12020-08-27 16:49:20 -0700775 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
776 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 Device* device = getDevice(deviceId);
778 if (device) {
779 const KeyInfo* key = getKey(device, scanCode, usageCode);
780 if (key) {
781 if (outKeycode) {
782 *outKeycode = key->keyCode;
783 }
784 if (outFlags) {
785 *outFlags = key->flags;
786 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700787 if (outMetaState) {
788 *outMetaState = metaState;
789 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 return OK;
791 }
792 }
793 return NAME_NOT_FOUND;
794 }
795
796 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
797 if (usageCode) {
798 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
799 if (index >= 0) {
800 return &device->keysByUsageCode.valueAt(index);
801 }
802 }
803 if (scanCode) {
804 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
805 if (index >= 0) {
806 return &device->keysByScanCode.valueAt(index);
807 }
808 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700809 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
Chris Yea52ade12020-08-27 16:49:20 -0700812 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813
Chris Yef59a2f42020-10-16 12:55:26 -0700814 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
815 int32_t absCode) {
816 Device* device = getDevice(deviceId);
817 if (!device) {
818 return Errorf("Sensor device not found.");
819 }
820 auto it = device->sensorsByAbsCode.find(absCode);
821 if (it == device->sensorsByAbsCode.end()) {
822 return Errorf("Sensor map not found.");
823 }
824 const SensorInfo& info = it->second;
825 return std::make_pair(info.sensorType, info.sensorDataIndex);
826 }
827
Chris Yea52ade12020-08-27 16:49:20 -0700828 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 mExcludedDevices = devices;
830 }
831
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000832 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
833 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000835 const size_t filledSize = std::min(mEvents.size(), bufferSize);
836 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
837
838 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700839 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000840 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800841 }
842
Chris Yea52ade12020-08-27 16:49:20 -0700843 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600844 auto it = mVideoFrames.find(deviceId);
845 if (it != mVideoFrames.end()) {
846 std::vector<TouchVideoFrame> frames = std::move(it->second);
847 mVideoFrames.erase(deviceId);
848 return frames;
849 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800850 return {};
851 }
852
Chris Yea52ade12020-08-27 16:49:20 -0700853 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 Device* device = getDevice(deviceId);
855 if (device) {
856 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
857 if (index >= 0) {
858 return device->scanCodeStates.valueAt(index);
859 }
860 }
861 return AKEY_STATE_UNKNOWN;
862 }
863
Chris Yea52ade12020-08-27 16:49:20 -0700864 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865 Device* device = getDevice(deviceId);
866 if (device) {
867 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
868 if (index >= 0) {
869 return device->keyCodeStates.valueAt(index);
870 }
871 }
872 return AKEY_STATE_UNKNOWN;
873 }
874
Chris Yea52ade12020-08-27 16:49:20 -0700875 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 Device* device = getDevice(deviceId);
877 if (device) {
878 ssize_t index = device->switchStates.indexOfKey(sw);
879 if (index >= 0) {
880 return device->switchStates.valueAt(index);
881 }
882 }
883 return AKEY_STATE_UNKNOWN;
884 }
885
Chris Yea52ade12020-08-27 16:49:20 -0700886 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
887 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800888 Device* device = getDevice(deviceId);
889 if (device) {
890 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
891 if (index >= 0) {
892 *outValue = device->absoluteAxisValue.valueAt(index);
893 return OK;
894 }
895 }
896 *outValue = 0;
897 return -1;
898 }
899
Philip Junker4af3b3d2021-12-14 10:36:55 +0100900 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
901 Device* device = getDevice(deviceId);
902 if (!device) {
903 return AKEYCODE_UNKNOWN;
904 }
905 auto it = device->keyCodeMapping.find(locationKeyCode);
906 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
907 }
908
Chris Yea52ade12020-08-27 16:49:20 -0700909 // Return true if the device has non-empty key layout.
910 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
911 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 bool result = false;
913 Device* device = getDevice(deviceId);
914 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700915 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 for (size_t i = 0; i < numCodes; i++) {
917 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
918 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
919 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920 }
921 }
922 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
923 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
924 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 }
926 }
927 }
928 }
929 return result;
930 }
931
Chris Yea52ade12020-08-27 16:49:20 -0700932 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 Device* device = getDevice(deviceId);
934 if (device) {
935 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
936 return index >= 0;
937 }
938 return false;
939 }
940
Arthur Hungcb40a002021-08-03 14:31:01 +0000941 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
942 Device* device = getDevice(deviceId);
943 if (!device) {
944 return false;
945 }
946 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
947 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
948 return true;
949 }
950 }
951 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
952 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
953 return true;
954 }
955 }
956 return false;
957 }
958
Chris Yea52ade12020-08-27 16:49:20 -0700959 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 Device* device = getDevice(deviceId);
961 return device && device->leds.indexOfKey(led) >= 0;
962 }
963
Chris Yea52ade12020-08-27 16:49:20 -0700964 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 Device* device = getDevice(deviceId);
966 if (device) {
967 ssize_t index = device->leds.indexOfKey(led);
968 if (index >= 0) {
969 device->leds.replaceValueAt(led, on);
970 } else {
971 ADD_FAILURE()
972 << "Attempted to set the state of an LED that the EventHub declared "
973 "was not present. led=" << led;
974 }
975 }
976 }
977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void getVirtualKeyDefinitions(
979 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 outVirtualKeys.clear();
981
982 Device* device = getDevice(deviceId);
983 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800984 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 }
986 }
987
Chris Yea52ade12020-08-27 16:49:20 -0700988 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700989 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 return false;
994 }
995
Chris Yea52ade12020-08-27 16:49:20 -0700996 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997
Chris Yea52ade12020-08-27 16:49:20 -0700998 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999
Chris Ye87143712020-11-10 05:05:58 +00001000 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
1001
Chris Yee2b1e5c2021-03-10 22:45:12 -08001002 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1003 return BATTERY_CAPACITY;
1004 }
Kim Low03ea0352020-11-06 12:45:07 -08001005
Chris Yee2b1e5c2021-03-10 22:45:12 -08001006 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1007 return BATTERY_STATUS;
1008 }
1009
1010 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
1011
1012 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
1013 return std::nullopt;
1014 }
Kim Low03ea0352020-11-06 12:45:07 -08001015
Chris Ye3fdbfef2021-01-06 18:45:18 -08001016 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1017 std::vector<int32_t> ids;
1018 for (const auto& [rawId, info] : mRawLightInfos) {
1019 ids.push_back(rawId);
1020 }
1021 return ids;
1022 }
1023
1024 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1025 auto it = mRawLightInfos.find(lightId);
1026 if (it == mRawLightInfos.end()) {
1027 return std::nullopt;
1028 }
1029 return it->second;
1030 }
1031
1032 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1033 mLightBrightness.emplace(lightId, brightness);
1034 }
1035
1036 void setLightIntensities(int32_t deviceId, int32_t lightId,
1037 std::unordered_map<LightColor, int32_t> intensities) override {
1038 mLightIntensities.emplace(lightId, intensities);
1039 };
1040
1041 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1042 auto lightIt = mLightBrightness.find(lightId);
1043 if (lightIt == mLightBrightness.end()) {
1044 return std::nullopt;
1045 }
1046 return lightIt->second;
1047 }
1048
1049 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1050 int32_t deviceId, int32_t lightId) override {
1051 auto lightIt = mLightIntensities.find(lightId);
1052 if (lightIt == mLightIntensities.end()) {
1053 return std::nullopt;
1054 }
1055 return lightIt->second;
1056 };
1057
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001058 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001059 return false;
1060 }
1061
Chris Yea52ade12020-08-27 16:49:20 -07001062 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063
Chris Yea52ade12020-08-27 16:49:20 -07001064 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Chris Yea52ade12020-08-27 16:49:20 -07001066 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067
Chris Yea52ade12020-08-27 16:49:20 -07001068 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069};
1070
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071// --- FakeInputMapper ---
1072
1073class FakeInputMapper : public InputMapper {
1074 uint32_t mSources;
1075 int32_t mKeyboardType;
1076 int32_t mMetaState;
1077 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1078 KeyedVector<int32_t, int32_t> mScanCodeStates;
1079 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001080 // fake mapping which would normally come from keyCharacterMap
1081 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001082 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001084 std::mutex mLock;
1085 std::condition_variable mStateChangedCondition;
1086 bool mConfigureWasCalled GUARDED_BY(mLock);
1087 bool mResetWasCalled GUARDED_BY(mLock);
1088 bool mProcessWasCalled GUARDED_BY(mLock);
1089 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090
Arthur Hungc23540e2018-11-29 20:42:11 +08001091 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001093 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1094 : InputMapper(deviceContext),
1095 mSources(sources),
1096 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001098 mConfigureWasCalled(false),
1099 mResetWasCalled(false),
1100 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101
Chris Yea52ade12020-08-27 16:49:20 -07001102 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103
1104 void setKeyboardType(int32_t keyboardType) {
1105 mKeyboardType = keyboardType;
1106 }
1107
1108 void setMetaState(int32_t metaState) {
1109 mMetaState = metaState;
1110 }
1111
1112 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001113 std::unique_lock<std::mutex> lock(mLock);
1114 base::ScopedLockAssertion assumeLocked(mLock);
1115 const bool configureCalled =
1116 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1117 return mConfigureWasCalled;
1118 });
1119 if (!configureCalled) {
1120 FAIL() << "Expected configure() to have been called.";
1121 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001122 mConfigureWasCalled = false;
1123 }
1124
1125 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001126 std::unique_lock<std::mutex> lock(mLock);
1127 base::ScopedLockAssertion assumeLocked(mLock);
1128 const bool resetCalled =
1129 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1130 return mResetWasCalled;
1131 });
1132 if (!resetCalled) {
1133 FAIL() << "Expected reset() to have been called.";
1134 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 mResetWasCalled = false;
1136 }
1137
Yi Kong9b14ac62018-07-17 13:48:38 -07001138 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001139 std::unique_lock<std::mutex> lock(mLock);
1140 base::ScopedLockAssertion assumeLocked(mLock);
1141 const bool processCalled =
1142 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1143 return mProcessWasCalled;
1144 });
1145 if (!processCalled) {
1146 FAIL() << "Expected process() to have been called.";
1147 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 if (outLastEvent) {
1149 *outLastEvent = mLastEvent;
1150 }
1151 mProcessWasCalled = false;
1152 }
1153
1154 void setKeyCodeState(int32_t keyCode, int32_t state) {
1155 mKeyCodeStates.replaceValueFor(keyCode, state);
1156 }
1157
1158 void setScanCodeState(int32_t scanCode, int32_t state) {
1159 mScanCodeStates.replaceValueFor(scanCode, state);
1160 }
1161
1162 void setSwitchState(int32_t switchCode, int32_t state) {
1163 mSwitchStates.replaceValueFor(switchCode, state);
1164 }
1165
1166 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001167 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 }
1169
Philip Junker4af3b3d2021-12-14 10:36:55 +01001170 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1171 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1172 }
1173
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001175 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176
Chris Yea52ade12020-08-27 16:49:20 -07001177 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178 InputMapper::populateDeviceInfo(deviceInfo);
1179
1180 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1181 deviceInfo->setKeyboardType(mKeyboardType);
1182 }
1183 }
1184
Chris Yea52ade12020-08-27 16:49:20 -07001185 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001186 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001188
1189 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001190 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001191 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1192 mViewport = config->getDisplayViewportByPort(*displayPort);
1193 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001194
1195 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 }
1197
Chris Yea52ade12020-08-27 16:49:20 -07001198 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001199 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001201 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 }
1203
Chris Yea52ade12020-08-27 16:49:20 -07001204 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001205 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 mLastEvent = *rawEvent;
1207 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001208 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 }
1210
Chris Yea52ade12020-08-27 16:49:20 -07001211 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001212 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1213 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1214 }
1215
Philip Junker4af3b3d2021-12-14 10:36:55 +01001216 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1217 auto it = mKeyCodeMapping.find(locationKeyCode);
1218 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1219 }
1220
Chris Yea52ade12020-08-27 16:49:20 -07001221 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1223 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1224 }
1225
Chris Yea52ade12020-08-27 16:49:20 -07001226 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001227 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1228 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1229 }
1230
Chris Yea52ade12020-08-27 16:49:20 -07001231 // Return true if the device has non-empty key layout.
1232 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1233 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234 for (size_t i = 0; i < numCodes; i++) {
1235 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1236 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1237 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238 }
1239 }
1240 }
Chris Yea52ade12020-08-27 16:49:20 -07001241 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 return result;
1243 }
1244
1245 virtual int32_t getMetaState() {
1246 return mMetaState;
1247 }
1248
1249 virtual void fadePointer() {
1250 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001251
1252 virtual std::optional<int32_t> getAssociatedDisplay() {
1253 if (mViewport) {
1254 return std::make_optional(mViewport->displayId);
1255 }
1256 return std::nullopt;
1257 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258};
1259
1260
1261// --- InstrumentedInputReader ---
1262
1263class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001264 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265
1266public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001267 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1268 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001269 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001270 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001271
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001272 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001274 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001276 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001277 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278 InputDeviceIdentifier identifier;
1279 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001280 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001282 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 }
1284
Prabir Pradhan28efc192019-11-05 01:10:04 +00001285 // Make the protected loopOnce method accessible to tests.
1286 using InputReader::loopOnce;
1287
Michael Wrightd02c5b62014-02-10 15:10:22 -08001288protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001289 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1290 const InputDeviceIdentifier& identifier)
1291 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001292 if (!mNextDevices.empty()) {
1293 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1294 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295 return device;
1296 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001297 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001298 }
1299
arthurhungdcef2dc2020-08-11 14:47:50 +08001300 // --- FakeInputReaderContext ---
1301 class FakeInputReaderContext : public ContextImpl {
1302 int32_t mGlobalMetaState;
1303 bool mUpdateGlobalMetaStateWasCalled;
1304 int32_t mGeneration;
1305
1306 public:
1307 FakeInputReaderContext(InputReader* reader)
1308 : ContextImpl(reader),
1309 mGlobalMetaState(0),
1310 mUpdateGlobalMetaStateWasCalled(false),
1311 mGeneration(1) {}
1312
1313 virtual ~FakeInputReaderContext() {}
1314
1315 void assertUpdateGlobalMetaStateWasCalled() {
1316 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1317 << "Expected updateGlobalMetaState() to have been called.";
1318 mUpdateGlobalMetaStateWasCalled = false;
1319 }
1320
1321 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1322
1323 uint32_t getGeneration() { return mGeneration; }
1324
1325 void updateGlobalMetaState() override {
1326 mUpdateGlobalMetaStateWasCalled = true;
1327 ContextImpl::updateGlobalMetaState();
1328 }
1329
1330 int32_t getGlobalMetaState() override {
1331 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1332 }
1333
1334 int32_t bumpGeneration() override {
1335 mGeneration = ContextImpl::bumpGeneration();
1336 return mGeneration;
1337 }
1338 } mFakeContext;
1339
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001341
1342public:
1343 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001344};
1345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346// --- InputReaderPolicyTest ---
1347class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001348protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001349 sp<FakeInputReaderPolicy> mFakePolicy;
1350
Chris Yea52ade12020-08-27 16:49:20 -07001351 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1352 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001353};
1354
1355/**
1356 * Check that empty set of viewports is an acceptable configuration.
1357 * Also try to get internal viewport two different ways - by type and by uniqueId.
1358 *
1359 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1360 * Such configuration is not currently allowed.
1361 */
1362TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001363 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364
1365 // We didn't add any viewports yet, so there shouldn't be any.
1366 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001367 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368 ASSERT_FALSE(internalViewport);
1369
1370 // Add an internal viewport, then clear it
1371 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001372 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001373 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374
1375 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001376 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001377 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001378 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379
1380 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001381 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001382 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001383 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384
1385 mFakePolicy->clearViewports();
1386 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001387 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 ASSERT_FALSE(internalViewport);
1391}
1392
1393TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1394 const std::string internalUniqueId = "local:0";
1395 const std::string externalUniqueId = "local:1";
1396 const std::string virtualUniqueId1 = "virtual:2";
1397 const std::string virtualUniqueId2 = "virtual:3";
1398 constexpr int32_t virtualDisplayId1 = 2;
1399 constexpr int32_t virtualDisplayId2 = 3;
1400
1401 // Add an internal viewport
1402 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001403 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1404 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001405 // Add an external viewport
1406 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001407 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1408 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001409 // Add an virtual viewport
1410 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001411 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1412 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 // Add another virtual viewport
1414 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1416 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417
1418 // Check matching by type for internal
1419 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001420 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 ASSERT_TRUE(internalViewport);
1422 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1423
1424 // Check matching by type for external
1425 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001426 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_TRUE(externalViewport);
1428 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1429
1430 // Check matching by uniqueId for virtual viewport #1
1431 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001432 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001433 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001434 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1436 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1437
1438 // Check matching by uniqueId for virtual viewport #2
1439 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1444 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1445}
1446
1447
1448/**
1449 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1450 * that lookup works by checking display id.
1451 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1452 */
1453TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1454 const std::string uniqueId1 = "uniqueId1";
1455 const std::string uniqueId2 = "uniqueId2";
1456 constexpr int32_t displayId1 = 2;
1457 constexpr int32_t displayId2 = 3;
1458
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001459 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1460 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001461 for (const ViewportType& type : types) {
1462 mFakePolicy->clearViewports();
1463 // Add a viewport
1464 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001465 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1466 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001467 // Add another viewport
1468 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001469 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1470 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001471
1472 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001473 std::optional<DisplayViewport> viewport1 =
1474 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 ASSERT_TRUE(viewport1);
1476 ASSERT_EQ(displayId1, viewport1->displayId);
1477 ASSERT_EQ(type, viewport1->type);
1478
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001479 std::optional<DisplayViewport> viewport2 =
1480 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001481 ASSERT_TRUE(viewport2);
1482 ASSERT_EQ(displayId2, viewport2->displayId);
1483 ASSERT_EQ(type, viewport2->type);
1484
1485 // When there are multiple viewports of the same kind, and uniqueId is not specified
1486 // in the call to getDisplayViewport, then that situation is not supported.
1487 // The viewports can be stored in any order, so we cannot rely on the order, since that
1488 // is just implementation detail.
1489 // However, we can check that it still returns *a* viewport, we just cannot assert
1490 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001491 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001492 ASSERT_TRUE(someViewport);
1493 }
1494}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001496/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001497 * When we have multiple internal displays make sure we always return the default display when
1498 * querying by type.
1499 */
1500TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1501 const std::string uniqueId1 = "uniqueId1";
1502 const std::string uniqueId2 = "uniqueId2";
1503 constexpr int32_t nonDefaultDisplayId = 2;
1504 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1505 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1506
1507 // Add the default display first and ensure it gets returned.
1508 mFakePolicy->clearViewports();
1509 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001510 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001511 ViewportType::INTERNAL);
1512 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001513 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001514 ViewportType::INTERNAL);
1515
1516 std::optional<DisplayViewport> viewport =
1517 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1518 ASSERT_TRUE(viewport);
1519 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1520 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1521
1522 // Add the default display second to make sure order doesn't matter.
1523 mFakePolicy->clearViewports();
1524 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001525 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001526 ViewportType::INTERNAL);
1527 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001528 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001529 ViewportType::INTERNAL);
1530
1531 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1532 ASSERT_TRUE(viewport);
1533 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1534 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1535}
1536
1537/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001538 * Check getDisplayViewportByPort
1539 */
1540TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001541 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001542 const std::string uniqueId1 = "uniqueId1";
1543 const std::string uniqueId2 = "uniqueId2";
1544 constexpr int32_t displayId1 = 1;
1545 constexpr int32_t displayId2 = 2;
1546 const uint8_t hdmi1 = 0;
1547 const uint8_t hdmi2 = 1;
1548 const uint8_t hdmi3 = 2;
1549
1550 mFakePolicy->clearViewports();
1551 // Add a viewport that's associated with some display port that's not of interest.
1552 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001553 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1554 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001555 // Add another viewport, connected to HDMI1 port
1556 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001557 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1558 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001559
1560 // Check that correct display viewport was returned by comparing the display ports.
1561 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1562 ASSERT_TRUE(hdmi1Viewport);
1563 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1564 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1565
1566 // Check that we can still get the same viewport using the uniqueId
1567 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1568 ASSERT_TRUE(hdmi1Viewport);
1569 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1570 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1571 ASSERT_EQ(type, hdmi1Viewport->type);
1572
1573 // Check that we cannot find a port with "HDMI2", because we never added one
1574 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1575 ASSERT_FALSE(hdmi2Viewport);
1576}
1577
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578// --- InputReaderTest ---
1579
1580class InputReaderTest : public testing::Test {
1581protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001582 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001583 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001584 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001585 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586
Chris Yea52ade12020-08-27 16:49:20 -07001587 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001588 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001589 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001590 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591
Prabir Pradhan28efc192019-11-05 01:10:04 +00001592 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001593 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 }
1595
Chris Yea52ade12020-08-27 16:49:20 -07001596 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001597 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001599 }
1600
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001601 void addDevice(int32_t eventHubId, const std::string& name,
1602 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001603 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604
1605 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 }
1608 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001609 mReader->loopOnce();
1610 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001611 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1612 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613 }
1614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001615 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001616 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001617 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001618 }
1619
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001620 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001622 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 }
1624
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001626 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001627 ftl::Flags<InputDeviceClass> classes,
1628 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001629 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1631 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001632 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634 return mapper;
1635 }
1636};
1637
Chris Ye98d3f532020-10-01 21:48:59 -07001638TEST_F(InputReaderTest, PolicyGetInputDevices) {
1639 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001640 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001641 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642
1643 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001644 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001647 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1649 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001650 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651}
1652
Chris Yee7310032020-09-22 15:36:28 -07001653TEST_F(InputReaderTest, GetMergedInputDevices) {
1654 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1655 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1656 // Add two subdevices to device
1657 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1658 // Must add at least one mapper or the device will be ignored!
1659 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1660 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1661
1662 // Push same device instance for next device to be added, so they'll have same identifier.
1663 mReader->pushNextDevice(device);
1664 mReader->pushNextDevice(device);
1665 ASSERT_NO_FATAL_FAILURE(
1666 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1667 ASSERT_NO_FATAL_FAILURE(
1668 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1669
1670 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001671 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001672}
1673
Chris Yee14523a2020-12-19 13:46:00 -08001674TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1675 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1676 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1677 // Add two subdevices to device
1678 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1679 // Must add at least one mapper or the device will be ignored!
1680 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1681 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1682
1683 // Push same device instance for next device to be added, so they'll have same identifier.
1684 mReader->pushNextDevice(device);
1685 mReader->pushNextDevice(device);
1686 // Sensor device is initially disabled
1687 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1688 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1689 nullptr));
1690 // Device is disabled because the only sub device is a sensor device and disabled initially.
1691 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1692 ASSERT_FALSE(device->isEnabled());
1693 ASSERT_NO_FATAL_FAILURE(
1694 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1695 // The merged device is enabled if any sub device is enabled
1696 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1697 ASSERT_TRUE(device->isEnabled());
1698}
1699
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001700TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001701 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001702 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001703 constexpr int32_t eventHubId = 1;
1704 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001705 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001707 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001709
Yi Kong9b14ac62018-07-17 13:48:38 -07001710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001711
1712 NotifyDeviceResetArgs resetArgs;
1713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714 ASSERT_EQ(deviceId, resetArgs.deviceId);
1715
1716 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001717 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001718 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001719
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001721 ASSERT_EQ(deviceId, resetArgs.deviceId);
1722 ASSERT_EQ(device->isEnabled(), false);
1723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001724 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001725 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001728 ASSERT_EQ(device->isEnabled(), false);
1729
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001730 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001731 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001733 ASSERT_EQ(deviceId, resetArgs.deviceId);
1734 ASSERT_EQ(device->isEnabled(), true);
1735}
1736
Michael Wrightd02c5b62014-02-10 15:10:22 -08001737TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001738 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001739 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001741 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001743 AINPUT_SOURCE_KEYBOARD, nullptr);
1744 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001745
1746 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1747 AINPUT_SOURCE_ANY, AKEYCODE_A))
1748 << "Should return unknown when the device id is >= 0 but unknown.";
1749
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001750 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1751 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1752 << "Should return unknown when the device id is valid but the sources are not "
1753 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001754
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001755 ASSERT_EQ(AKEY_STATE_DOWN,
1756 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1757 AKEYCODE_A))
1758 << "Should return value provided by mapper when device id is valid and the device "
1759 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001760
1761 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1762 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1763 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1764
1765 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1766 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1767 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1768}
1769
Philip Junker4af3b3d2021-12-14 10:36:55 +01001770TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1771 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1772 constexpr int32_t eventHubId = 1;
1773 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1774 InputDeviceClass::KEYBOARD,
1775 AINPUT_SOURCE_KEYBOARD, nullptr);
1776 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1777
1778 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1779 << "Should return unknown when the device with the specified id is not found.";
1780
1781 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1782 << "Should return correct mapping when device id is valid and mapping exists.";
1783
1784 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1785 << "Should return the location key code when device id is valid and there's no "
1786 "mapping.";
1787}
1788
1789TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1790 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1791 constexpr int32_t eventHubId = 1;
1792 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1793 InputDeviceClass::JOYSTICK,
1794 AINPUT_SOURCE_GAMEPAD, nullptr);
1795 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1796
1797 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1798 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1799}
1800
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001802 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001803 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001805 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 AINPUT_SOURCE_KEYBOARD, nullptr);
1808 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809
1810 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1811 AINPUT_SOURCE_ANY, KEY_A))
1812 << "Should return unknown when the device id is >= 0 but unknown.";
1813
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1815 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1816 << "Should return unknown when the device id is valid but the sources are not "
1817 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 ASSERT_EQ(AKEY_STATE_DOWN,
1820 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1821 KEY_A))
1822 << "Should return value provided by mapper when device id is valid and the device "
1823 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824
1825 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1826 AINPUT_SOURCE_TRACKBALL, KEY_A))
1827 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1828
1829 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1830 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1831 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1832}
1833
1834TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001835 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001836 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001838 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001839 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001840 AINPUT_SOURCE_KEYBOARD, nullptr);
1841 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842
1843 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1844 AINPUT_SOURCE_ANY, SW_LID))
1845 << "Should return unknown when the device id is >= 0 but unknown.";
1846
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1848 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1849 << "Should return unknown when the device id is valid but the sources are not "
1850 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 ASSERT_EQ(AKEY_STATE_DOWN,
1853 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1854 SW_LID))
1855 << "Should return value provided by mapper when device id is valid and the device "
1856 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857
1858 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1859 AINPUT_SOURCE_TRACKBALL, SW_LID))
1860 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1861
1862 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1863 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1864 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1865}
1866
1867TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001869 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001875 mapper.addSupportedKeyCode(AKEYCODE_A);
1876 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001877
1878 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1879 uint8_t flags[4] = { 0, 0, 0, 1 };
1880
1881 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1882 << "Should return false when device id is >= 0 but unknown.";
1883 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1884
1885 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001886 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1887 << "Should return false when device id is valid but the sources are not supported by "
1888 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1890
1891 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001892 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1893 keyCodes, flags))
1894 << "Should return value provided by mapper when device id is valid and the device "
1895 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1897
1898 flags[3] = 1;
1899 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1900 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1901 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1902
1903 flags[3] = 1;
1904 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1905 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1906 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1907}
1908
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001909TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001911 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001912
1913 NotifyConfigurationChangedArgs args;
1914
1915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1916 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1917}
1918
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001919TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001920 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001921 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001922 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001923 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001924 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001925 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001927 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001928
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001929 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001930 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001931 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1932
1933 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001934 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001935 ASSERT_EQ(when, event.when);
1936 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001937 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001938 ASSERT_EQ(EV_KEY, event.type);
1939 ASSERT_EQ(KEY_A, event.code);
1940 ASSERT_EQ(1, event.value);
1941}
1942
Garfield Tan1c7bc862020-01-28 13:24:04 -08001943TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001944 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001945 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001946 constexpr int32_t eventHubId = 1;
1947 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001948 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001949 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001950 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001951 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001952
1953 NotifyDeviceResetArgs resetArgs;
1954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001955 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001957 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001958 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001960 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001961 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001963 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001964 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001966 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001967 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001969 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001970 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001972 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001973 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001974}
1975
Garfield Tan1c7bc862020-01-28 13:24:04 -08001976TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1977 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001978 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001979 constexpr int32_t eventHubId = 1;
1980 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1981 // Must add at least one mapper or the device will be ignored!
1982 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001983 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001984 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1985
1986 NotifyDeviceResetArgs resetArgs;
1987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1988 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1989}
1990
Arthur Hungc23540e2018-11-29 20:42:11 +08001991TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001992 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001993 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001994 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001995 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001996 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1997 FakeInputMapper& mapper =
1998 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001999 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002000
2001 const uint8_t hdmi1 = 1;
2002
2003 // Associated touch screen with second display.
2004 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2005
2006 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002007 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002008 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002009 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002010 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002011 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002012 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002013 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002014 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002015 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002016
2017 // Add the device, and make sure all of the callbacks are triggered.
2018 // The device is added after the input port associations are processed since
2019 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002020 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002023 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002024
Arthur Hung2c9a3342019-07-23 14:18:59 +08002025 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002026 ASSERT_EQ(deviceId, device->getId());
2027 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2028 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002029
2030 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002031 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002032 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002033 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002034}
2035
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002036TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2037 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002038 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002039 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2040 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2041 // Must add at least one mapper or the device will be ignored!
2042 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2043 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2044 mReader->pushNextDevice(device);
2045 mReader->pushNextDevice(device);
2046 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2047 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2048
2049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2050
2051 NotifyDeviceResetArgs resetArgs;
2052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2053 ASSERT_EQ(deviceId, resetArgs.deviceId);
2054 ASSERT_TRUE(device->isEnabled());
2055 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2056 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2057
2058 disableDevice(deviceId);
2059 mReader->loopOnce();
2060
2061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2062 ASSERT_EQ(deviceId, resetArgs.deviceId);
2063 ASSERT_FALSE(device->isEnabled());
2064 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2065 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2066
2067 enableDevice(deviceId);
2068 mReader->loopOnce();
2069
2070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2071 ASSERT_EQ(deviceId, resetArgs.deviceId);
2072 ASSERT_TRUE(device->isEnabled());
2073 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2074 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2075}
2076
2077TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2078 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002079 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002080 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2081 // Add two subdevices to device
2082 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2083 FakeInputMapper& mapperDevice1 =
2084 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2085 FakeInputMapper& mapperDevice2 =
2086 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2087 mReader->pushNextDevice(device);
2088 mReader->pushNextDevice(device);
2089 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2090 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2091
2092 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2093 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2094
2095 ASSERT_EQ(AKEY_STATE_DOWN,
2096 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2097 ASSERT_EQ(AKEY_STATE_DOWN,
2098 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2099 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2100 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2101}
2102
Prabir Pradhan7e186182020-11-10 13:56:45 -08002103TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2104 NotifyPointerCaptureChangedArgs args;
2105
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002106 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002107 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2108 mReader->loopOnce();
2109 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002110 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2111 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002112
2113 mFakePolicy->setPointerCapture(false);
2114 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2115 mReader->loopOnce();
2116 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002117 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002118
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002119 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002120 // does not change.
2121 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2122 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002123 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002124}
2125
Chris Ye87143712020-11-10 05:05:58 +00002126class FakeVibratorInputMapper : public FakeInputMapper {
2127public:
2128 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2129 : FakeInputMapper(deviceContext, sources) {}
2130
2131 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2132};
2133
2134TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2135 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002136 ftl::Flags<InputDeviceClass> deviceClass =
2137 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002138 constexpr int32_t eventHubId = 1;
2139 const char* DEVICE_LOCATION = "BLUETOOTH";
2140 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2141 FakeVibratorInputMapper& mapper =
2142 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2143 mReader->pushNextDevice(device);
2144
2145 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2146 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2147
2148 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2149 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2150}
2151
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002152// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002153
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002154class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002155public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002156 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002157
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002158 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002159
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002160 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2161
Chris Yee2b1e5c2021-03-10 22:45:12 -08002162 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2163
2164 void dump(std::string& dump) override {}
2165
2166 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2167 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002168 }
2169
Chris Yee2b1e5c2021-03-10 22:45:12 -08002170 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2171 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002172 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002173
2174 bool setLightColor(int32_t lightId, int32_t color) override {
2175 getDeviceContext().setLightBrightness(lightId, color >> 24);
2176 return true;
2177 }
2178
2179 std::optional<int32_t> getLightColor(int32_t lightId) override {
2180 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2181 if (!result.has_value()) {
2182 return std::nullopt;
2183 }
2184 return result.value() << 24;
2185 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002186
2187 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2188
2189 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2190
2191private:
2192 InputDeviceContext& mDeviceContext;
2193 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2194 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002195 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002196};
2197
Chris Yee2b1e5c2021-03-10 22:45:12 -08002198TEST_F(InputReaderTest, BatteryGetCapacity) {
2199 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002200 ftl::Flags<InputDeviceClass> deviceClass =
2201 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002202 constexpr int32_t eventHubId = 1;
2203 const char* DEVICE_LOCATION = "BLUETOOTH";
2204 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002205 FakePeripheralController& controller =
2206 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002207 mReader->pushNextDevice(device);
2208
2209 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2210
2211 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2212 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2213}
2214
2215TEST_F(InputReaderTest, BatteryGetStatus) {
2216 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002217 ftl::Flags<InputDeviceClass> deviceClass =
2218 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002219 constexpr int32_t eventHubId = 1;
2220 const char* DEVICE_LOCATION = "BLUETOOTH";
2221 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002222 FakePeripheralController& controller =
2223 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002224 mReader->pushNextDevice(device);
2225
2226 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2227
2228 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2229 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2230}
2231
Chris Ye3fdbfef2021-01-06 18:45:18 -08002232TEST_F(InputReaderTest, LightGetColor) {
2233 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002234 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002235 constexpr int32_t eventHubId = 1;
2236 const char* DEVICE_LOCATION = "BLUETOOTH";
2237 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002238 FakePeripheralController& controller =
2239 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002240 mReader->pushNextDevice(device);
2241 RawLightInfo info = {.id = 1,
2242 .name = "Mono",
2243 .maxBrightness = 255,
2244 .flags = InputLightClass::BRIGHTNESS,
2245 .path = ""};
2246 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2247 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2248
2249 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002250
Chris Yee2b1e5c2021-03-10 22:45:12 -08002251 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2252 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002253 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2254 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2255}
2256
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002257// --- InputReaderIntegrationTest ---
2258
2259// These tests create and interact with the InputReader only through its interface.
2260// The InputReader is started during SetUp(), which starts its processing in its own
2261// thread. The tests use linux uinput to emulate input devices.
2262// NOTE: Interacting with the physical device while these tests are running may cause
2263// the tests to fail.
2264class InputReaderIntegrationTest : public testing::Test {
2265protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002266 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002267 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002268 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002269
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002270 std::shared_ptr<FakePointerController> mFakePointerController;
2271
Chris Yea52ade12020-08-27 16:49:20 -07002272 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002273 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002274 mFakePointerController = std::make_shared<FakePointerController>();
2275 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002276 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2277 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002278
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002279 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2280 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002281 ASSERT_EQ(mReader->start(), OK);
2282
2283 // Since this test is run on a real device, all the input devices connected
2284 // to the test device will show up in mReader. We wait for those input devices to
2285 // show up before beginning the tests.
2286 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2287 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2288 }
2289
Chris Yea52ade12020-08-27 16:49:20 -07002290 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002291 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002292 mReader.reset();
2293 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002294 mFakePolicy.clear();
2295 }
2296};
2297
2298TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2299 // An invalid input device that is only used for this test.
2300 class InvalidUinputDevice : public UinputDevice {
2301 public:
2302 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2303
2304 private:
2305 void configureDevice(int fd, uinput_user_dev* device) override {}
2306 };
2307
2308 const size_t numDevices = mFakePolicy->getInputDevices().size();
2309
2310 // UinputDevice does not set any event or key bits, so InputReader should not
2311 // consider it as a valid device.
2312 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2313 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2315 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2316
2317 invalidDevice.reset();
2318 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2319 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2320 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2321}
2322
2323TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2324 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2325
2326 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2327 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2328 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2329 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2330
2331 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002332 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002333 const auto& it =
2334 std::find_if(inputDevices.begin(), inputDevices.end(),
2335 [&keyboard](const InputDeviceInfo& info) {
2336 return info.getIdentifier().name == keyboard->getName();
2337 });
2338
2339 ASSERT_NE(it, inputDevices.end());
2340 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2341 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2342 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002343
2344 keyboard.reset();
2345 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2347 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2348}
2349
2350TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2351 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2352 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2353
2354 NotifyConfigurationChangedArgs configChangedArgs;
2355 ASSERT_NO_FATAL_FAILURE(
2356 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002357 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002358 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2359
2360 NotifyKeyArgs keyArgs;
2361 keyboard->pressAndReleaseHomeKey();
2362 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2363 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002364 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002365 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002366 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002367 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002368 prevTimestamp = keyArgs.eventTime;
2369
2370 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2371 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002372 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002373 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002374 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002375}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002376
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002377/**
2378 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2379 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2380 * are passed to the listener.
2381 */
2382static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2383TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2384 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2385 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2386 NotifyKeyArgs keyArgs;
2387
2388 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2390 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2391 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2392
2393 controller->pressAndReleaseKey(BTN_GEAR_UP);
2394 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2395 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2396 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2397}
2398
Arthur Hungaab25622020-01-16 11:22:11 +08002399// --- TouchProcessTest ---
2400class TouchIntegrationTest : public InputReaderIntegrationTest {
2401protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002402 const std::string UNIQUE_ID = "local:0";
2403
Chris Yea52ade12020-08-27 16:49:20 -07002404 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002405 InputReaderIntegrationTest::SetUp();
2406 // At least add an internal display.
2407 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2408 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002409 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002410
2411 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2414 }
2415
2416 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2417 int32_t orientation, const std::string& uniqueId,
2418 std::optional<uint8_t> physicalPort,
2419 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002420 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2421 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002422 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2423 }
2424
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002425 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2426 NotifyMotionArgs args;
2427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2428 EXPECT_EQ(action, args.action);
2429 ASSERT_EQ(points.size(), args.pointerCount);
2430 for (size_t i = 0; i < args.pointerCount; i++) {
2431 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2432 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2433 }
2434 }
2435
Arthur Hungaab25622020-01-16 11:22:11 +08002436 std::unique_ptr<UinputTouchScreen> mDevice;
2437};
2438
2439TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2440 NotifyMotionArgs args;
2441 const Point centerPoint = mDevice->getCenterPoint();
2442
2443 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002444 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002445 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002446 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002447 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2448 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2449
2450 // ACTION_MOVE
2451 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002452 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2455
2456 // ACTION_UP
2457 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002458 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002459 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2460 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2461}
2462
2463TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2464 NotifyMotionArgs args;
2465 const Point centerPoint = mDevice->getCenterPoint();
2466
2467 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002468 mDevice->sendSlot(FIRST_SLOT);
2469 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002470 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002471 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2474
2475 // ACTION_POINTER_DOWN (Second slot)
2476 const Point secondPoint = centerPoint + Point(100, 100);
2477 mDevice->sendSlot(SECOND_SLOT);
2478 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002479 mDevice->sendDown(secondPoint);
2480 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002482 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002483
2484 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002485 mDevice->sendMove(secondPoint + Point(1, 1));
2486 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002487 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2489
2490 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002491 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002492 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002493 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002494 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002495
2496 // ACTION_UP
2497 mDevice->sendSlot(FIRST_SLOT);
2498 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002499 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2501 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2502}
2503
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002504/**
2505 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2506 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2507 * data?
2508 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2509 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2510 * for Pointer 0 only is generated after.
2511 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2512 * events, we will not miss any information.
2513 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2514 * event generated afterwards that contains the newest movement of pointer 0.
2515 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2516 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2517 * losing information about non-palm pointers.
2518 */
2519TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2520 NotifyMotionArgs args;
2521 const Point centerPoint = mDevice->getCenterPoint();
2522
2523 // ACTION_DOWN
2524 mDevice->sendSlot(FIRST_SLOT);
2525 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2526 mDevice->sendDown(centerPoint);
2527 mDevice->sendSync();
2528 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2529
2530 // ACTION_POINTER_DOWN (Second slot)
2531 const Point secondPoint = centerPoint + Point(100, 100);
2532 mDevice->sendSlot(SECOND_SLOT);
2533 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2534 mDevice->sendDown(secondPoint);
2535 mDevice->sendSync();
2536 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2537
2538 // ACTION_MOVE (First slot)
2539 mDevice->sendSlot(FIRST_SLOT);
2540 mDevice->sendMove(centerPoint + Point(5, 5));
2541 // ACTION_POINTER_UP (Second slot)
2542 mDevice->sendSlot(SECOND_SLOT);
2543 mDevice->sendPointerUp();
2544 // Send a single sync for the above 2 pointer updates
2545 mDevice->sendSync();
2546
2547 // First, we should get POINTER_UP for the second pointer
2548 assertReceivedMotion(ACTION_POINTER_1_UP,
2549 {/*first pointer */ centerPoint + Point(5, 5),
2550 /*second pointer*/ secondPoint});
2551
2552 // Next, the MOVE event for the first pointer
2553 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2554}
2555
2556/**
2557 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2558 * move, and then it will go up, all in the same frame.
2559 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2560 * gets sent to the listener.
2561 */
2562TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2563 NotifyMotionArgs args;
2564 const Point centerPoint = mDevice->getCenterPoint();
2565
2566 // ACTION_DOWN
2567 mDevice->sendSlot(FIRST_SLOT);
2568 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2569 mDevice->sendDown(centerPoint);
2570 mDevice->sendSync();
2571 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2572
2573 // ACTION_POINTER_DOWN (Second slot)
2574 const Point secondPoint = centerPoint + Point(100, 100);
2575 mDevice->sendSlot(SECOND_SLOT);
2576 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2577 mDevice->sendDown(secondPoint);
2578 mDevice->sendSync();
2579 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2580
2581 // ACTION_MOVE (First slot)
2582 mDevice->sendSlot(FIRST_SLOT);
2583 mDevice->sendMove(centerPoint + Point(5, 5));
2584 // ACTION_POINTER_UP (Second slot)
2585 mDevice->sendSlot(SECOND_SLOT);
2586 mDevice->sendMove(secondPoint + Point(6, 6));
2587 mDevice->sendPointerUp();
2588 // Send a single sync for the above 2 pointer updates
2589 mDevice->sendSync();
2590
2591 // First, we should get POINTER_UP for the second pointer
2592 // The movement of the second pointer during the liftoff frame is ignored.
2593 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2594 assertReceivedMotion(ACTION_POINTER_1_UP,
2595 {/*first pointer */ centerPoint + Point(5, 5),
2596 /*second pointer*/ secondPoint});
2597
2598 // Next, the MOVE event for the first pointer
2599 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2600}
2601
Arthur Hungaab25622020-01-16 11:22:11 +08002602TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2603 NotifyMotionArgs args;
2604 const Point centerPoint = mDevice->getCenterPoint();
2605
2606 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002607 mDevice->sendSlot(FIRST_SLOT);
2608 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002609 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002610 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002611 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2612 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2613
arthurhungcc7f9802020-04-30 17:55:40 +08002614 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002615 const Point secondPoint = centerPoint + Point(100, 100);
2616 mDevice->sendSlot(SECOND_SLOT);
2617 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2618 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002619 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002620 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002621 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002622
arthurhungcc7f9802020-04-30 17:55:40 +08002623 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002624 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002625 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002626 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2628
arthurhungcc7f9802020-04-30 17:55:40 +08002629 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2630 // a palm event.
2631 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002632 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002633 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002634 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002635 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002636 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002637
arthurhungcc7f9802020-04-30 17:55:40 +08002638 // Send up to second slot, expect first slot send moving.
2639 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002640 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002641 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002643
arthurhungcc7f9802020-04-30 17:55:40 +08002644 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002645 mDevice->sendSlot(FIRST_SLOT);
2646 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002647 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002648
arthurhungcc7f9802020-04-30 17:55:40 +08002649 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2650 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002651}
2652
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654class InputDeviceTest : public testing::Test {
2655protected:
2656 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002657 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658 static const int32_t DEVICE_ID;
2659 static const int32_t DEVICE_GENERATION;
2660 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002661 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002662 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002664 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002666 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002667 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002668 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669
Chris Yea52ade12020-08-27 16:49:20 -07002670 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002671 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002673 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002674 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002675 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676 InputDeviceIdentifier identifier;
2677 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002678 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002679 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002680 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002681 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002682 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002683 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684 }
2685
Chris Yea52ade12020-08-27 16:49:20 -07002686 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002687 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 }
2690};
2691
2692const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002693const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002694const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2696const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002697const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002698 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002699const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700
2701TEST_F(InputDeviceTest, ImmutableProperties) {
2702 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002703 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002704 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705}
2706
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002707TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2708 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002709}
2710
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2712 // Configuration.
2713 InputReaderConfiguration config;
2714 mDevice->configure(ARBITRARY_TIME, &config, 0);
2715
2716 // Reset.
2717 mDevice->reset(ARBITRARY_TIME);
2718
2719 NotifyDeviceResetArgs resetArgs;
2720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2721 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2722 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2723
2724 // Metadata.
2725 ASSERT_TRUE(mDevice->isIgnored());
2726 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2727
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002728 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002730 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2732 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2733
2734 // State queries.
2735 ASSERT_EQ(0, mDevice->getMetaState());
2736
2737 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2738 << "Ignored device should return unknown key code state.";
2739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2740 << "Ignored device should return unknown scan code state.";
2741 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2742 << "Ignored device should return unknown switch state.";
2743
2744 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2745 uint8_t flags[2] = { 0, 1 };
2746 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2747 << "Ignored device should never mark any key codes.";
2748 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2749 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2750}
2751
2752TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2753 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002754 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002756 FakeInputMapper& mapper1 =
2757 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002758 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2759 mapper1.setMetaState(AMETA_ALT_ON);
2760 mapper1.addSupportedKeyCode(AKEYCODE_A);
2761 mapper1.addSupportedKeyCode(AKEYCODE_B);
2762 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2763 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2764 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2765 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2766 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002768 FakeInputMapper& mapper2 =
2769 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002770 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771
2772 InputReaderConfiguration config;
2773 mDevice->configure(ARBITRARY_TIME, &config, 0);
2774
2775 String8 propertyValue;
2776 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2777 << "Device should have read configuration during configuration phase.";
2778 ASSERT_STREQ("value", propertyValue.string());
2779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002780 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2781 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782
2783 // Reset
2784 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002785 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2786 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787
2788 NotifyDeviceResetArgs resetArgs;
2789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2790 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2791 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2792
2793 // Metadata.
2794 ASSERT_FALSE(mDevice->isIgnored());
2795 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2796
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002797 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002799 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2801 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2802
2803 // State queries.
2804 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2805 << "Should query mappers and combine meta states.";
2806
2807 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2808 << "Should return unknown key code state when source not supported.";
2809 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2810 << "Should return unknown scan code state when source not supported.";
2811 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2812 << "Should return unknown switch state when source not supported.";
2813
2814 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2815 << "Should query mapper when source is supported.";
2816 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2817 << "Should query mapper when source is supported.";
2818 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2819 << "Should query mapper when source is supported.";
2820
2821 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2822 uint8_t flags[4] = { 0, 0, 0, 1 };
2823 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2824 << "Should do nothing when source is unsupported.";
2825 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2826 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2827 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2828 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2829
2830 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2831 << "Should query mapper when source is supported.";
2832 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2833 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2834 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2835 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2836
2837 // Event handling.
2838 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002839 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002840 mDevice->process(&event, 1);
2841
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002842 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2843 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844}
2845
Arthur Hung2c9a3342019-07-23 14:18:59 +08002846// A single input device is associated with a specific display. Check that:
2847// 1. Device is disabled if the viewport corresponding to the associated display is not found
2848// 2. Device is disabled when setEnabled API is called
2849TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002850 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002851
2852 // First Configuration.
2853 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2854
2855 // Device should be enabled by default.
2856 ASSERT_TRUE(mDevice->isEnabled());
2857
2858 // Prepare associated info.
2859 constexpr uint8_t hdmi = 1;
2860 const std::string UNIQUE_ID = "local:1";
2861
2862 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2863 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2864 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2865 // Device should be disabled because it is associated with a specific display via
2866 // input port <-> display port association, but the corresponding display is not found
2867 ASSERT_FALSE(mDevice->isEnabled());
2868
2869 // Prepare displays.
2870 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002871 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2872 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002873 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2874 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2875 ASSERT_TRUE(mDevice->isEnabled());
2876
2877 // Device should be disabled after set disable.
2878 mFakePolicy->addDisabledDevice(mDevice->getId());
2879 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2880 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2881 ASSERT_FALSE(mDevice->isEnabled());
2882
2883 // Device should still be disabled even found the associated display.
2884 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2885 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2886 ASSERT_FALSE(mDevice->isEnabled());
2887}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888
Christine Franks1ba71cc2021-04-07 14:37:42 -07002889TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2890 // Device should be enabled by default.
2891 mFakePolicy->clearViewports();
2892 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2893 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2894 ASSERT_TRUE(mDevice->isEnabled());
2895
2896 // Device should be disabled because it is associated with a specific display, but the
2897 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002898 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002899 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2900 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2901 ASSERT_FALSE(mDevice->isEnabled());
2902
2903 // Device should be enabled when a display is found.
2904 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2905 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2906 NO_PORT, ViewportType::INTERNAL);
2907 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2908 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2909 ASSERT_TRUE(mDevice->isEnabled());
2910
2911 // Device should be disabled after set disable.
2912 mFakePolicy->addDisabledDevice(mDevice->getId());
2913 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2914 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2915 ASSERT_FALSE(mDevice->isEnabled());
2916
2917 // Device should still be disabled even found the associated display.
2918 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2919 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2920 ASSERT_FALSE(mDevice->isEnabled());
2921}
2922
Christine Franks2a2293c2022-01-18 11:51:16 -08002923TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2924 mFakePolicy->clearViewports();
2925 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2926 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2927
Christine Franks2a2293c2022-01-18 11:51:16 -08002928 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2929 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2930 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2931 NO_PORT, ViewportType::INTERNAL);
2932 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2933 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2934 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2935}
2936
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002937/**
2938 * This test reproduces a crash caused by a dangling reference that remains after device is added
2939 * and removed. The reference is accessed in InputDevice::dump(..);
2940 */
2941TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2942 constexpr int32_t TEST_EVENTHUB_ID = 10;
2943 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2944
2945 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
2946 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
2947 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2948 std::string dumpStr, eventHubDevStr;
2949 device.dump(dumpStr, eventHubDevStr);
2950}
2951
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952// --- InputMapperTest ---
2953
2954class InputMapperTest : public testing::Test {
2955protected:
2956 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002957 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 static const int32_t DEVICE_ID;
2959 static const int32_t DEVICE_GENERATION;
2960 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002961 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002962 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002964 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002966 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002967 std::unique_ptr<InstrumentedInputReader> mReader;
2968 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002970 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002971 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002973 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002974 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002975 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002976 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002977 // Consume the device reset notification generated when adding a new device.
2978 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979 }
2980
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002981 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002982 SetUp(DEVICE_CLASSES);
2983 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002984
Chris Yea52ade12020-08-27 16:49:20 -07002985 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002986 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 }
2989
2990 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002991 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992 }
2993
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002994 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002995 if (!changes ||
2996 (changes &
2997 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2998 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002999 mReader->requestRefreshConfiguration(changes);
3000 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003001 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003002 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhan36690412022-08-05 22:26:56 +00003003 // Loop the reader to flush the input listener queue.
3004 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003005 }
3006
arthurhungdcef2dc2020-08-11 14:47:50 +08003007 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3008 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003009 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003010 InputDeviceIdentifier identifier;
3011 identifier.name = name;
3012 identifier.location = location;
3013 std::shared_ptr<InputDevice> device =
3014 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3015 identifier);
3016 mReader->pushNextDevice(device);
3017 mFakeEventHub->addDevice(eventHubId, name, classes);
3018 mReader->loopOnce();
3019 return device;
3020 }
3021
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003022 template <class T, typename... Args>
3023 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003024 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003025 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003027 mapper.reset(ARBITRARY_TIME);
Prabir Pradhan36690412022-08-05 22:26:56 +00003028 // Loop the reader to flush the input listener queue.
3029 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003030 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 }
3032
3033 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003034 int32_t orientation, const std::string& uniqueId,
3035 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003036 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3037 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003038 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3039 }
3040
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003041 void clearViewports() {
3042 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043 }
3044
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003045 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3046 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 RawEvent event;
3048 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003049 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003050 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 event.type = type;
3052 event.code = code;
3053 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003054 mapper.process(&event);
Prabir Pradhan36690412022-08-05 22:26:56 +00003055 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003056 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057 }
3058
3059 static void assertMotionRange(const InputDeviceInfo& info,
3060 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3061 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003062 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3064 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3065 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3066 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3067 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3068 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3069 }
3070
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003071 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3072 float size, float touchMajor, float touchMinor, float toolMajor,
3073 float toolMinor, float orientation, float distance,
3074 float scaledAxisEpsilon = 1.f) {
3075 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3076 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3078 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003079 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3080 scaledAxisEpsilon);
3081 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3082 scaledAxisEpsilon);
3083 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3084 scaledAxisEpsilon);
3085 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3086 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3088 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3089 }
3090
Michael Wright17db18e2020-06-26 20:51:44 +01003091 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003093 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094 ASSERT_NEAR(x, actualX, 1);
3095 ASSERT_NEAR(y, actualY, 1);
3096 }
3097};
3098
3099const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003100const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003101const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3103const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003104const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3105 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003106const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107
3108// --- SwitchInputMapperTest ---
3109
3110class SwitchInputMapperTest : public InputMapperTest {
3111protected:
3112};
3113
3114TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003115 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003117 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118}
3119
3120TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003121 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003123 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003124 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003126 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003127 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128}
3129
3130TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003131 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137
3138 NotifySwitchArgs args;
3139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3140 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003141 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3142 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 args.switchMask);
3144 ASSERT_EQ(uint32_t(0), args.policyFlags);
3145}
3146
Chris Ye87143712020-11-10 05:05:58 +00003147// --- VibratorInputMapperTest ---
3148class VibratorInputMapperTest : public InputMapperTest {
3149protected:
3150 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3151};
3152
3153TEST_F(VibratorInputMapperTest, GetSources) {
3154 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3155
3156 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3157}
3158
3159TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3160 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3161
3162 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3163}
3164
3165TEST_F(VibratorInputMapperTest, Vibrate) {
3166 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003167 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003168 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3169
3170 VibrationElement pattern(2);
3171 VibrationSequence sequence(2);
3172 pattern.duration = std::chrono::milliseconds(200);
3173 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3174 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3175 sequence.addElement(pattern);
3176 pattern.duration = std::chrono::milliseconds(500);
3177 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3178 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3179 sequence.addElement(pattern);
3180
3181 std::vector<int64_t> timings = {0, 1};
3182 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3183
3184 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003185 // Start vibrating
3186 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003187 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003188 // Verify vibrator state listener was notified.
3189 mReader->loopOnce();
3190 NotifyVibratorStateArgs args;
3191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3192 ASSERT_EQ(DEVICE_ID, args.deviceId);
3193 ASSERT_TRUE(args.isOn);
3194 // Stop vibrating
3195 mapper.cancelVibrate(VIBRATION_TOKEN);
3196 ASSERT_FALSE(mapper.isVibrating());
3197 // Verify vibrator state listener was notified.
3198 mReader->loopOnce();
3199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3200 ASSERT_EQ(DEVICE_ID, args.deviceId);
3201 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003202}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203
Chris Yef59a2f42020-10-16 12:55:26 -07003204// --- SensorInputMapperTest ---
3205
3206class SensorInputMapperTest : public InputMapperTest {
3207protected:
3208 static const int32_t ACCEL_RAW_MIN;
3209 static const int32_t ACCEL_RAW_MAX;
3210 static const int32_t ACCEL_RAW_FUZZ;
3211 static const int32_t ACCEL_RAW_FLAT;
3212 static const int32_t ACCEL_RAW_RESOLUTION;
3213
3214 static const int32_t GYRO_RAW_MIN;
3215 static const int32_t GYRO_RAW_MAX;
3216 static const int32_t GYRO_RAW_FUZZ;
3217 static const int32_t GYRO_RAW_FLAT;
3218 static const int32_t GYRO_RAW_RESOLUTION;
3219
3220 static const float GRAVITY_MS2_UNIT;
3221 static const float DEGREE_RADIAN_UNIT;
3222
3223 void prepareAccelAxes();
3224 void prepareGyroAxes();
3225 void setAccelProperties();
3226 void setGyroProperties();
3227 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3228};
3229
3230const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3231const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3232const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3233const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3234const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3235
3236const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3237const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3238const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3239const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3240const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3241
3242const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3243const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3244
3245void SensorInputMapperTest::prepareAccelAxes() {
3246 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3247 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3248 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3249 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3250 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3251 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3252}
3253
3254void SensorInputMapperTest::prepareGyroAxes() {
3255 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3256 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3258 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3259 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3260 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3261}
3262
3263void SensorInputMapperTest::setAccelProperties() {
3264 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3265 /* sensorDataIndex */ 0);
3266 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3267 /* sensorDataIndex */ 1);
3268 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3269 /* sensorDataIndex */ 2);
3270 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3271 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3272 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3273 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3274 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3275}
3276
3277void SensorInputMapperTest::setGyroProperties() {
3278 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3279 /* sensorDataIndex */ 0);
3280 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3281 /* sensorDataIndex */ 1);
3282 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3283 /* sensorDataIndex */ 2);
3284 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3285 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3286 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3287 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3288 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3289}
3290
3291TEST_F(SensorInputMapperTest, GetSources) {
3292 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3293
3294 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3295}
3296
3297TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3298 setAccelProperties();
3299 prepareAccelAxes();
3300 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3301
3302 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3303 std::chrono::microseconds(10000),
3304 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003305 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003311
3312 NotifySensorArgs args;
3313 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3314 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3315 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3316
3317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3318 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3319 ASSERT_EQ(args.deviceId, DEVICE_ID);
3320 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3321 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3322 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3323 ASSERT_EQ(args.values, values);
3324 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3325}
3326
3327TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3328 setGyroProperties();
3329 prepareGyroAxes();
3330 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3331
3332 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3333 std::chrono::microseconds(10000),
3334 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003335 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003341
3342 NotifySensorArgs args;
3343 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3344 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3345 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3346
3347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3348 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3349 ASSERT_EQ(args.deviceId, DEVICE_ID);
3350 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3351 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3352 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3353 ASSERT_EQ(args.values, values);
3354 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3355}
3356
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357// --- KeyboardInputMapperTest ---
3358
3359class KeyboardInputMapperTest : public InputMapperTest {
3360protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003361 const std::string UNIQUE_ID = "local:0";
3362
3363 void prepareDisplay(int32_t orientation);
3364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003365 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003366 int32_t originalKeyCode, int32_t rotatedKeyCode,
3367 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368};
3369
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003370/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3371 * orientation.
3372 */
3373void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003374 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3375 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003376}
3377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003378void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003379 int32_t originalScanCode, int32_t originalKeyCode,
3380 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 NotifyKeyArgs args;
3382
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3386 ASSERT_EQ(originalScanCode, args.scanCode);
3387 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003388 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3392 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3393 ASSERT_EQ(originalScanCode, args.scanCode);
3394 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003395 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396}
3397
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003399 KeyboardInputMapper& mapper =
3400 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3401 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003403 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404}
3405
3406TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3407 const int32_t USAGE_A = 0x070004;
3408 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003409 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3410 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003411 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3412 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3413 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003415 KeyboardInputMapper& mapper =
3416 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3417 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003418 // Initial metastate is AMETA_NONE.
3419 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420
3421 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423 NotifyKeyArgs args;
3424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3425 ASSERT_EQ(DEVICE_ID, args.deviceId);
3426 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3427 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3428 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3429 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3430 ASSERT_EQ(KEY_HOME, args.scanCode);
3431 ASSERT_EQ(AMETA_NONE, args.metaState);
3432 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3433 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3434 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3435
3436 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003437 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3439 ASSERT_EQ(DEVICE_ID, args.deviceId);
3440 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3441 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3442 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3443 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3444 ASSERT_EQ(KEY_HOME, args.scanCode);
3445 ASSERT_EQ(AMETA_NONE, args.metaState);
3446 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3447 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3448 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3449
3450 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3454 ASSERT_EQ(DEVICE_ID, args.deviceId);
3455 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3456 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3457 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3458 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3459 ASSERT_EQ(0, args.scanCode);
3460 ASSERT_EQ(AMETA_NONE, args.metaState);
3461 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3462 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3463 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3464
3465 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3467 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3469 ASSERT_EQ(DEVICE_ID, args.deviceId);
3470 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3471 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3472 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3473 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3474 ASSERT_EQ(0, args.scanCode);
3475 ASSERT_EQ(AMETA_NONE, args.metaState);
3476 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3477 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3478 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3479
3480 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3484 ASSERT_EQ(DEVICE_ID, args.deviceId);
3485 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3486 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3487 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3488 ASSERT_EQ(0, args.keyCode);
3489 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3490 ASSERT_EQ(AMETA_NONE, args.metaState);
3491 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3492 ASSERT_EQ(0U, args.policyFlags);
3493 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3494
3495 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3497 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3499 ASSERT_EQ(DEVICE_ID, args.deviceId);
3500 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3501 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3502 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3503 ASSERT_EQ(0, args.keyCode);
3504 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3505 ASSERT_EQ(AMETA_NONE, args.metaState);
3506 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3507 ASSERT_EQ(0U, args.policyFlags);
3508 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3509}
3510
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511/**
3512 * Ensure that the readTime is set to the time when the EV_KEY is received.
3513 */
3514TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3515 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3516
3517 KeyboardInputMapper& mapper =
3518 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3519 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3520 NotifyKeyArgs args;
3521
3522 // Key down
3523 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3525 ASSERT_EQ(12, args.readTime);
3526
3527 // Key up
3528 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3530 ASSERT_EQ(15, args.readTime);
3531}
3532
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003534 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003536 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3537 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3538 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003540 KeyboardInputMapper& mapper =
3541 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3542 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543
Arthur Hung95f68612022-04-07 14:08:22 +08003544 // Initial metastate is AMETA_NONE.
3545 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
3547 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549 NotifyKeyArgs args;
3550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3551 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003552 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003553 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003554
3555 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003556 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560
3561 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003562 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3564 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566
3567 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003568 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3570 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003571 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003572 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573}
3574
3575TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3577 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003581 KeyboardInputMapper& mapper =
3582 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3583 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003585 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3587 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3588 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3589 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3590 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3591 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3593 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3594}
3595
3596TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003597 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3599 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3600 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003601
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003603 KeyboardInputMapper& mapper =
3604 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3605 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003607 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003608 ASSERT_NO_FATAL_FAILURE(
3609 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3610 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3611 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3613 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3615 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003617 clearViewports();
3618 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003619 ASSERT_NO_FATAL_FAILURE(
3620 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3622 AKEYCODE_DPAD_UP, DISPLAY_ID));
3623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3624 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3626 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003628 clearViewports();
3629 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003630 ASSERT_NO_FATAL_FAILURE(
3631 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3633 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3635 AKEYCODE_DPAD_UP, DISPLAY_ID));
3636 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3637 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003639 clearViewports();
3640 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003641 ASSERT_NO_FATAL_FAILURE(
3642 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3644 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3646 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3648 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003649
3650 // Special case: if orientation changes while key is down, we still emit the same keycode
3651 // in the key up as we did in the key down.
3652 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003653 clearViewports();
3654 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3657 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3658 ASSERT_EQ(KEY_UP, args.scanCode);
3659 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3660
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003661 clearViewports();
3662 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3665 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3666 ASSERT_EQ(KEY_UP, args.scanCode);
3667 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3668}
3669
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003670TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3671 // If the keyboard is not orientation aware,
3672 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003673 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003674
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003675 KeyboardInputMapper& mapper =
3676 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3677 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003678 NotifyKeyArgs args;
3679
3680 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3685 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3686
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003687 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3692 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3693}
3694
3695TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3696 // If the keyboard is orientation aware,
3697 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003698 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003699
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003700 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003701 KeyboardInputMapper& mapper =
3702 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3703 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003704 NotifyKeyArgs args;
3705
3706 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3707 // ^--- already checked by the previous test
3708
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003709 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003710 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3715 ASSERT_EQ(DISPLAY_ID, args.displayId);
3716
3717 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003718 clearViewports();
3719 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003720 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3725 ASSERT_EQ(newDisplayId, args.displayId);
3726}
3727
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003729 KeyboardInputMapper& mapper =
3730 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3731 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003733 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003736 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738}
3739
Philip Junker4af3b3d2021-12-14 10:36:55 +01003740TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3741 KeyboardInputMapper& mapper =
3742 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3743 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3744
3745 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3746 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3747 << "If a mapping is available, the result is equal to the mapping";
3748
3749 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3750 << "If no mapping is available, the result is the key location";
3751}
3752
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003754 KeyboardInputMapper& mapper =
3755 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3756 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003759 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003762 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003763}
3764
3765TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 KeyboardInputMapper& mapper =
3767 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3768 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771
3772 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3773 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775 ASSERT_TRUE(flags[0]);
3776 ASSERT_FALSE(flags[1]);
3777}
3778
3779TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3781 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3782 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3784 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 KeyboardInputMapper& mapper =
3788 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3789 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003790 // Initial metastate is AMETA_NONE.
3791 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792
3793 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003794 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3795 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3796 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
3798 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003801 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3802 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805
3806 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003809 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3810 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3811 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003812 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813
3814 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3818 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3819 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003820 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821
3822 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3826 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3827 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003828 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829
3830 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003833 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3834 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3835 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003836 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837
3838 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003841 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3842 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3843 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003844 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845}
3846
Chris Yea52ade12020-08-27 16:49:20 -07003847TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3848 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3849 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3850 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3851 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3852
3853 KeyboardInputMapper& mapper =
3854 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3855 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3856
Chris Yea52ade12020-08-27 16:49:20 -07003857 // Meta state should be AMETA_NONE after reset
3858 mapper.reset(ARBITRARY_TIME);
3859 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3860 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3861 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3862 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3863
3864 NotifyKeyArgs args;
3865 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3868 ASSERT_EQ(AMETA_NONE, args.metaState);
3869 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3870 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3871 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3872
3873 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003874 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3876 ASSERT_EQ(AMETA_NONE, args.metaState);
3877 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3878 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3879 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3880}
3881
Arthur Hung2c9a3342019-07-23 14:18:59 +08003882TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3883 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003884 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3885 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3886 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3887 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003888
3889 // keyboard 2.
3890 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003891 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003892 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003893 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003894 std::shared_ptr<InputDevice> device2 =
3895 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003896 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003897
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003898 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3899 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3900 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3901 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003902
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003903 KeyboardInputMapper& mapper =
3904 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3905 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003906
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003907 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003908 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003909 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003910 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3911 device2->reset(ARBITRARY_TIME);
3912
3913 // Prepared displays and associated info.
3914 constexpr uint8_t hdmi1 = 0;
3915 constexpr uint8_t hdmi2 = 1;
3916 const std::string SECONDARY_UNIQUE_ID = "local:1";
3917
3918 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3919 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3920
3921 // No associated display viewport found, should disable the device.
3922 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3923 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3924 ASSERT_FALSE(device2->isEnabled());
3925
3926 // Prepare second display.
3927 constexpr int32_t newDisplayId = 2;
3928 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003929 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003930 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003931 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003932 // Default device will reconfigure above, need additional reconfiguration for another device.
3933 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3934 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3935
3936 // Device should be enabled after the associated display is found.
3937 ASSERT_TRUE(mDevice->isEnabled());
3938 ASSERT_TRUE(device2->isEnabled());
3939
3940 // Test pad key events
3941 ASSERT_NO_FATAL_FAILURE(
3942 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3943 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3944 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3945 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3946 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3947 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3948 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3949
3950 ASSERT_NO_FATAL_FAILURE(
3951 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3952 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3953 AKEYCODE_DPAD_RIGHT, newDisplayId));
3954 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3955 AKEYCODE_DPAD_DOWN, newDisplayId));
3956 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3957 AKEYCODE_DPAD_LEFT, newDisplayId));
3958}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959
arthurhungc903df12020-08-11 15:08:42 +08003960TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3961 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3962 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3963 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3965 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3966 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3967
3968 KeyboardInputMapper& mapper =
3969 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3970 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003971 // Initial metastate is AMETA_NONE.
3972 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003973
3974 // Initialization should have turned all of the lights off.
3975 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3976 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3977 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3978
3979 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003982 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3983 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3984
3985 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003988 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3989 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3990
3991 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003994 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3995 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3996
3997 mFakeEventHub->removeDevice(EVENTHUB_ID);
3998 mReader->loopOnce();
3999
4000 // keyboard 2 should default toggle keys.
4001 const std::string USB2 = "USB2";
4002 const std::string DEVICE_NAME2 = "KEYBOARD2";
4003 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4004 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4005 std::shared_ptr<InputDevice> device2 =
4006 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004007 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004008 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4009 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4010 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4011 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4012 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4013 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4014
arthurhung6fe95782020-10-05 22:41:16 +08004015 KeyboardInputMapper& mapper2 =
4016 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4017 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004018 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4019 device2->reset(ARBITRARY_TIME);
4020
4021 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4022 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4023 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004024 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4025 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004026}
4027
Arthur Hungcb40a002021-08-03 14:31:01 +00004028TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4029 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4030 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4032
4033 // Suppose we have two mappers. (DPAD + KEYBOARD)
4034 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4035 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4036 KeyboardInputMapper& mapper =
4037 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4038 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004039 // Initial metastate is AMETA_NONE.
4040 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004041
4042 mReader->toggleCapsLockState(DEVICE_ID);
4043 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4044}
4045
Arthur Hungfb3cc112022-04-13 07:39:50 +00004046TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4047 // keyboard 1.
4048 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4049 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4050 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4052 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4054
4055 KeyboardInputMapper& mapper1 =
4056 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4057 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4058
4059 // keyboard 2.
4060 const std::string USB2 = "USB2";
4061 const std::string DEVICE_NAME2 = "KEYBOARD2";
4062 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4063 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4064 std::shared_ptr<InputDevice> device2 =
4065 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4066 ftl::Flags<InputDeviceClass>(0));
4067 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4068 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4069 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4070 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4071 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4072 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4073
4074 KeyboardInputMapper& mapper2 =
4075 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4076 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4077 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4078 device2->reset(ARBITRARY_TIME);
4079
Arthur Hung95f68612022-04-07 14:08:22 +08004080 // Initial metastate is AMETA_NONE.
4081 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4082 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4083
4084 // Toggle num lock on and off.
4085 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4086 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004087 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4088 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4089 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4090
4091 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4092 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4093 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4094 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4095 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4096
4097 // Toggle caps lock on and off.
4098 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4099 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4100 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4101 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4102 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4103
4104 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4105 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4106 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4107 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4108 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4109
4110 // Toggle scroll lock on and off.
4111 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4112 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4113 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4114 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4115 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4116
4117 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4118 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4119 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4120 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4121 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4122}
4123
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004124// --- KeyboardInputMapperTest_ExternalDevice ---
4125
4126class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4127protected:
Chris Yea52ade12020-08-27 16:49:20 -07004128 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004129};
4130
4131TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004132 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4133 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004134
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4138 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140 KeyboardInputMapper& mapper =
4141 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4142 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 NotifyKeyArgs args;
4146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4147 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4148
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4151 ASSERT_EQ(uint32_t(0), args.policyFlags);
4152
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4155 ASSERT_EQ(uint32_t(0), args.policyFlags);
4156
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4159 ASSERT_EQ(uint32_t(0), args.policyFlags);
4160
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4168}
4169
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004170TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004171 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004172
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004173 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4174 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4175 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004176
Powei Fengd041c5d2019-05-03 17:11:33 -07004177 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004178 KeyboardInputMapper& mapper =
4179 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4180 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004181
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004183 NotifyKeyArgs args;
4184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4185 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4186
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004187 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4189 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4190
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4193 ASSERT_EQ(uint32_t(0), args.policyFlags);
4194
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004195 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4197 ASSERT_EQ(uint32_t(0), args.policyFlags);
4198
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4201 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4202
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004203 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4205 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4206}
4207
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208// --- CursorInputMapperTest ---
4209
4210class CursorInputMapperTest : public InputMapperTest {
4211protected:
4212 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4213
Michael Wright17db18e2020-06-26 20:51:44 +01004214 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
Chris Yea52ade12020-08-27 16:49:20 -07004216 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 InputMapperTest::SetUp();
4218
Michael Wright17db18e2020-06-26 20:51:44 +01004219 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004220 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221 }
4222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4224 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004225
4226 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004227 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4228 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4229 }
4230
4231 void prepareSecondaryDisplay() {
4232 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4233 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4234 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004235 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004236
4237 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4238 float pressure) {
4239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4240 0.0f, 0.0f, 0.0f, EPSILON));
4241 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242};
4243
4244const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4245
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004246void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4247 int32_t originalY, int32_t rotatedX,
4248 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 NotifyMotionArgs args;
4250
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004256 ASSERT_NO_FATAL_FAILURE(
4257 assertCursorPointerCoords(args.pointerCoords[0],
4258 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4259 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260}
4261
4262TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004264 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004266 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267}
4268
4269TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004271 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004273 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274}
4275
4276TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004278 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
4280 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004281 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
4283 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004284 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4285 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4287 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4288
4289 // When the bounds are set, then there should be a valid motion range.
4290 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4291
4292 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004293 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294
4295 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4296 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4297 1, 800 - 1, 0.0f, 0.0f));
4298 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4299 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4300 2, 480 - 1, 0.0f, 0.0f));
4301 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4302 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4303 0.0f, 1.0f, 0.0f, 0.0f));
4304}
4305
4306TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004308 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
4310 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004311 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312
4313 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4314 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4315 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4316 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4317 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4318 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4319 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4320 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4321 0.0f, 1.0f, 0.0f, 0.0f));
4322}
4323
4324TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004326 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327
arthurhungdcef2dc2020-08-11 14:47:50 +08004328 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329
4330 NotifyMotionArgs args;
4331
4332 // Button press.
4333 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4337 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4338 ASSERT_EQ(DEVICE_ID, args.deviceId);
4339 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4340 ASSERT_EQ(uint32_t(0), args.policyFlags);
4341 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4342 ASSERT_EQ(0, args.flags);
4343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4344 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4345 ASSERT_EQ(0, args.edgeFlags);
4346 ASSERT_EQ(uint32_t(1), args.pointerCount);
4347 ASSERT_EQ(0, args.pointerProperties[0].id);
4348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004349 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4351 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4352 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4353
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4355 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4356 ASSERT_EQ(DEVICE_ID, args.deviceId);
4357 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4358 ASSERT_EQ(uint32_t(0), args.policyFlags);
4359 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4360 ASSERT_EQ(0, args.flags);
4361 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4362 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4363 ASSERT_EQ(0, args.edgeFlags);
4364 ASSERT_EQ(uint32_t(1), args.pointerCount);
4365 ASSERT_EQ(0, args.pointerProperties[0].id);
4366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004367 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4369 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4370 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4371
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004373 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4374 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4376 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4377 ASSERT_EQ(DEVICE_ID, args.deviceId);
4378 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4379 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4381 ASSERT_EQ(0, args.flags);
4382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4383 ASSERT_EQ(0, args.buttonState);
4384 ASSERT_EQ(0, args.edgeFlags);
4385 ASSERT_EQ(uint32_t(1), args.pointerCount);
4386 ASSERT_EQ(0, args.pointerProperties[0].id);
4387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004388 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4390 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4391 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4392
4393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4394 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4395 ASSERT_EQ(DEVICE_ID, args.deviceId);
4396 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4397 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4399 ASSERT_EQ(0, args.flags);
4400 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4401 ASSERT_EQ(0, args.buttonState);
4402 ASSERT_EQ(0, args.edgeFlags);
4403 ASSERT_EQ(uint32_t(1), args.pointerCount);
4404 ASSERT_EQ(0, args.pointerProperties[0].id);
4405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004406 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4408 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4409 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4410}
4411
4412TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004414 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415
4416 NotifyMotionArgs args;
4417
4418 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004423 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4424 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4425 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426
4427 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004432 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4433 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434}
4435
4436TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004438 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
4440 NotifyMotionArgs args;
4441
4442 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004447 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4450 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004451 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004452
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004458 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459
4460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463}
4464
4465TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004467 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468
4469 NotifyMotionArgs args;
4470
4471 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4477 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004478 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4479 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4480 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004484 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4485 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4486 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004494 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4495 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4496 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497
4498 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004503 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004504
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004507 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508}
4509
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004510TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004511 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004513 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4514 // need to be rotated.
4515 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004516 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004518 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4527}
4528
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004529TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004530 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004532 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4533 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004534 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004536 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004537 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4539 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4540 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4541 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4542 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4543 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4544 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4545 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4546
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004547 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004548 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4553 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4554 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4555 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4556 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004558 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004559 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4561 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4564 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4565 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4566 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4567 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4568
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004569 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004570 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004571 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4575 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4576 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4577 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4578 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579}
4580
4581TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004583 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584
4585 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4586 mFakePointerController->setPosition(100, 200);
4587 mFakePointerController->setButtonState(0);
4588
4589 NotifyMotionArgs motionArgs;
4590 NotifyKeyArgs keyArgs;
4591
4592 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4596 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4597 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4598 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004599 ASSERT_NO_FATAL_FAILURE(
4600 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4604 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4605 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004606 ASSERT_NO_FATAL_FAILURE(
4607 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004608
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_EQ(0, motionArgs.buttonState);
4614 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004615 ASSERT_NO_FATAL_FAILURE(
4616 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617
4618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004619 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_EQ(0, motionArgs.buttonState);
4621 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004622 ASSERT_NO_FATAL_FAILURE(
4623 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004624
4625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627 ASSERT_EQ(0, motionArgs.buttonState);
4628 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004629 ASSERT_NO_FATAL_FAILURE(
4630 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631
4632 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4637 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4638 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4639 motionArgs.buttonState);
4640 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4641 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004642 ASSERT_NO_FATAL_FAILURE(
4643 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4647 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4648 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4649 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004650 ASSERT_NO_FATAL_FAILURE(
4651 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4655 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4656 motionArgs.buttonState);
4657 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4658 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004659 ASSERT_NO_FATAL_FAILURE(
4660 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4667 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(
4669 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004670
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004673 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4674 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004675 ASSERT_NO_FATAL_FAILURE(
4676 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4682 ASSERT_EQ(0, motionArgs.buttonState);
4683 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004684 ASSERT_NO_FATAL_FAILURE(
4685 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004688
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690 ASSERT_EQ(0, motionArgs.buttonState);
4691 ASSERT_EQ(0, mFakePointerController->getButtonState());
4692 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004693 ASSERT_NO_FATAL_FAILURE(
4694 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004695
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4697 ASSERT_EQ(0, motionArgs.buttonState);
4698 ASSERT_EQ(0, mFakePointerController->getButtonState());
4699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004700 ASSERT_NO_FATAL_FAILURE(
4701 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702
4703 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4707 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4708 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004709
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004711 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4713 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004714 ASSERT_NO_FATAL_FAILURE(
4715 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004716
4717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4719 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4720 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004721 ASSERT_NO_FATAL_FAILURE(
4722 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_EQ(0, motionArgs.buttonState);
4729 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004730 ASSERT_NO_FATAL_FAILURE(
4731 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004732
4733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004735 ASSERT_EQ(0, motionArgs.buttonState);
4736 ASSERT_EQ(0, mFakePointerController->getButtonState());
4737
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004738 ASSERT_NO_FATAL_FAILURE(
4739 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4741 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4742 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4743
4744 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4748 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4749 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004750
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004753 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4754 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004755 ASSERT_NO_FATAL_FAILURE(
4756 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004757
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4759 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4760 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4761 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004762 ASSERT_NO_FATAL_FAILURE(
4763 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004768 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 ASSERT_EQ(0, motionArgs.buttonState);
4770 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004771 ASSERT_NO_FATAL_FAILURE(
4772 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004773
4774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4776 ASSERT_EQ(0, motionArgs.buttonState);
4777 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004778 ASSERT_NO_FATAL_FAILURE(
4779 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004780
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4782 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4783 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4784
4785 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4789 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4790 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004791
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004793 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4795 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004796 ASSERT_NO_FATAL_FAILURE(
4797 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004798
4799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4801 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4802 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004803 ASSERT_NO_FATAL_FAILURE(
4804 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_EQ(0, motionArgs.buttonState);
4811 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004812 ASSERT_NO_FATAL_FAILURE(
4813 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4817 ASSERT_EQ(0, motionArgs.buttonState);
4818 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004819 ASSERT_NO_FATAL_FAILURE(
4820 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004821
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4823 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4824 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4825
4826 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4830 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4831 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004832
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4836 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004837 ASSERT_NO_FATAL_FAILURE(
4838 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004839
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4842 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4843 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004844 ASSERT_NO_FATAL_FAILURE(
4845 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004850 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 ASSERT_EQ(0, motionArgs.buttonState);
4852 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004853 ASSERT_NO_FATAL_FAILURE(
4854 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004855
4856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4857 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4858 ASSERT_EQ(0, motionArgs.buttonState);
4859 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004860 ASSERT_NO_FATAL_FAILURE(
4861 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004862
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4864 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4865 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4866}
4867
4868TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004870 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871
4872 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4873 mFakePointerController->setPosition(100, 200);
4874 mFakePointerController->setButtonState(0);
4875
4876 NotifyMotionArgs args;
4877
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004882 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4883 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4885 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 +01004886 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004887}
4888
4889TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004890 addConfigurationProperty("cursor.mode", "pointer");
4891 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004892 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004893
4894 NotifyDeviceResetArgs resetArgs;
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4896 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4897 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4898
4899 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4900 mFakePointerController->setPosition(100, 200);
4901 mFakePointerController->setButtonState(0);
4902
4903 NotifyMotionArgs args;
4904
4905 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4910 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4913 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 +01004914 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004915
4916 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4920 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4921 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4923 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4925 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4926 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4928 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4929
4930 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004931 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4932 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4934 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4937 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4939 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4940 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4942 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4943
4944 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4949 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4952 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 +01004953 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004954
4955 // Disable pointer capture and check that the device generation got bumped
4956 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004957 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004958 mFakePolicy->setPointerCapture(false);
4959 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004960 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004961
4962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004963 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4964
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4969 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4972 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 +01004973 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974}
4975
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004976/**
4977 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4978 * pointer acceleration or speed processing should not be applied.
4979 */
4980TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4981 addConfigurationProperty("cursor.mode", "pointer");
4982 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4983 100.f /*high threshold*/, 10.f /*acceleration*/);
4984 mFakePolicy->setVelocityControlParams(testParams);
4985 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4986
4987 NotifyDeviceResetArgs resetArgs;
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4989 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4990 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4991
4992 NotifyMotionArgs args;
4993
4994 // Move and verify scale is applied.
4995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4999 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5001 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5002 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5003 ASSERT_GT(relX, 10);
5004 ASSERT_GT(relY, 20);
5005
5006 // Enable Pointer Capture
5007 mFakePolicy->setPointerCapture(true);
5008 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5009 NotifyPointerCaptureChangedArgs captureArgs;
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5011 ASSERT_TRUE(captureArgs.request.enable);
5012
5013 // Move and verify scale is not applied.
5014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5018 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5020 ASSERT_EQ(10, args.pointerCoords[0].getX());
5021 ASSERT_EQ(20, args.pointerCoords[0].getY());
5022}
5023
Prabir Pradhan258e2b92022-06-24 18:37:04 +00005024TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5025 addConfigurationProperty("cursor.mode", "pointer");
5026 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5027
5028 NotifyDeviceResetArgs resetArgs;
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5030 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5031 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5032
5033 // Ensure the display is rotated.
5034 prepareDisplay(DISPLAY_ORIENTATION_90);
5035
5036 NotifyMotionArgs args;
5037
5038 // Verify that the coordinates are rotated.
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5043 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5044 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5045 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5046 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5047
5048 // Enable Pointer Capture.
5049 mFakePolicy->setPointerCapture(true);
5050 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5051 NotifyPointerCaptureChangedArgs captureArgs;
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5053 ASSERT_TRUE(captureArgs.request.enable);
5054
5055 // Move and verify rotation is not applied.
5056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5060 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5062 ASSERT_EQ(10, args.pointerCoords[0].getX());
5063 ASSERT_EQ(20, args.pointerCoords[0].getY());
5064}
5065
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005066TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005067 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005068
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005069 // Set up the default display.
5070 prepareDisplay(DISPLAY_ORIENTATION_90);
5071
5072 // Set up the secondary display as the display on which the pointer should be shown.
5073 // The InputDevice is not associated with any display.
5074 prepareSecondaryDisplay();
5075 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005076 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5077
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005078 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005079 mFakePointerController->setPosition(100, 200);
5080 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005081
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005082 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5087 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5088 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005089 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005090}
5091
5092TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5093 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5094
5095 // Set up the default display.
5096 prepareDisplay(DISPLAY_ORIENTATION_90);
5097
5098 // Set up the secondary display as the display on which the pointer should be shown,
5099 // and associate the InputDevice with the secondary display.
5100 prepareSecondaryDisplay();
5101 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5102 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5103 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5104
5105 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5106 mFakePointerController->setPosition(100, 200);
5107 mFakePointerController->setButtonState(0);
5108
5109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5113 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5114 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
5115 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5116}
5117
5118TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5119 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5120
5121 // Set up the default display as the display on which the pointer should be shown.
5122 prepareDisplay(DISPLAY_ORIENTATION_90);
5123 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5124
5125 // Associate the InputDevice with the secondary display.
5126 prepareSecondaryDisplay();
5127 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5128 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5129
5130 // The mapper should not generate any events because it is associated with a display that is
5131 // different from the pointer display.
5132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005136}
5137
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138// --- TouchInputMapperTest ---
5139
5140class TouchInputMapperTest : public InputMapperTest {
5141protected:
5142 static const int32_t RAW_X_MIN;
5143 static const int32_t RAW_X_MAX;
5144 static const int32_t RAW_Y_MIN;
5145 static const int32_t RAW_Y_MAX;
5146 static const int32_t RAW_TOUCH_MIN;
5147 static const int32_t RAW_TOUCH_MAX;
5148 static const int32_t RAW_TOOL_MIN;
5149 static const int32_t RAW_TOOL_MAX;
5150 static const int32_t RAW_PRESSURE_MIN;
5151 static const int32_t RAW_PRESSURE_MAX;
5152 static const int32_t RAW_ORIENTATION_MIN;
5153 static const int32_t RAW_ORIENTATION_MAX;
5154 static const int32_t RAW_DISTANCE_MIN;
5155 static const int32_t RAW_DISTANCE_MAX;
5156 static const int32_t RAW_TILT_MIN;
5157 static const int32_t RAW_TILT_MAX;
5158 static const int32_t RAW_ID_MIN;
5159 static const int32_t RAW_ID_MAX;
5160 static const int32_t RAW_SLOT_MIN;
5161 static const int32_t RAW_SLOT_MAX;
5162 static const float X_PRECISION;
5163 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005164 static const float X_PRECISION_VIRTUAL;
5165 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166
5167 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005168 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169
5170 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5171
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005172 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005173 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005174
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175 enum Axes {
5176 POSITION = 1 << 0,
5177 TOUCH = 1 << 1,
5178 TOOL = 1 << 2,
5179 PRESSURE = 1 << 3,
5180 ORIENTATION = 1 << 4,
5181 MINOR = 1 << 5,
5182 ID = 1 << 6,
5183 DISTANCE = 1 << 7,
5184 TILT = 1 << 8,
5185 SLOT = 1 << 9,
5186 TOOL_TYPE = 1 << 10,
5187 };
5188
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005189 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5190 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005191 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005193 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194 int32_t toRawX(float displayX);
5195 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005196 int32_t toRotatedRawX(float displayX);
5197 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005198 float toCookedX(float rawX, float rawY);
5199 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005201 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005202 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005203 float toDisplayY(int32_t rawY, int32_t displayHeight);
5204
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205};
5206
5207const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5208const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5209const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5210const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5211const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5212const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5213const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5214const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005215const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5216const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5218const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5219const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5220const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5221const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5222const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5223const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5224const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5225const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5226const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5227const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5228const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005229const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5230 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5231const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5232 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005233const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5234 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235
5236const float TouchInputMapperTest::GEOMETRIC_SCALE =
5237 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5238 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5239
5240const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5241 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5242 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5243};
5244
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005245void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005246 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5247 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005248}
5249
5250void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5251 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5252 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253}
5254
Santos Cordonfa5cf462017-04-05 10:37:00 -07005255void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005256 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5257 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5258 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005259}
5260
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005262 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5263 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5264 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5265 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266}
5267
Jason Gerecke489fda82012-09-07 17:19:40 -07005268void TouchInputMapperTest::prepareLocationCalibration() {
5269 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5270}
5271
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272int32_t TouchInputMapperTest::toRawX(float displayX) {
5273 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5274}
5275
5276int32_t TouchInputMapperTest::toRawY(float displayY) {
5277 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5278}
5279
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005280int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5281 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5282}
5283
5284int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5285 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5286}
5287
Jason Gerecke489fda82012-09-07 17:19:40 -07005288float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5289 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5290 return rawX;
5291}
5292
5293float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5294 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5295 return rawY;
5296}
5297
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005299 return toDisplayX(rawX, DISPLAY_WIDTH);
5300}
5301
5302float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5303 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304}
5305
5306float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005307 return toDisplayY(rawY, DISPLAY_HEIGHT);
5308}
5309
5310float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5311 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312}
5313
5314
5315// --- SingleTouchInputMapperTest ---
5316
5317class SingleTouchInputMapperTest : public TouchInputMapperTest {
5318protected:
5319 void prepareButtons();
5320 void prepareAxes(int axes);
5321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005322 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5323 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5324 void processUp(SingleTouchInputMapper& mappery);
5325 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5326 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5327 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5328 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5329 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5330 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331};
5332
5333void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005334 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335}
5336
5337void SingleTouchInputMapperTest::prepareAxes(int axes) {
5338 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005339 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5340 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341 }
5342 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005343 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5344 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345 }
5346 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005347 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5348 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349 }
5350 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005351 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5352 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353 }
5354 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005355 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5356 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357 }
5358}
5359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005360void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364}
5365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369}
5370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005371void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373}
5374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377}
5378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005379void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5380 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382}
5383
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386}
5387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005388void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5389 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392}
5393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005394void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5395 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397}
5398
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005399void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401}
5402
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 prepareButtons();
5405 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005406 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005408 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409}
5410
5411TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005412 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5413 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 prepareButtons();
5415 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005416 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005418 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419}
5420
5421TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 prepareButtons();
5423 prepareAxes(POSITION);
5424 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005425 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428}
5429
5430TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 prepareButtons();
5432 prepareAxes(POSITION);
5433 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005434 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005436 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437}
5438
5439TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 addConfigurationProperty("touch.deviceType", "touchScreen");
5441 prepareDisplay(DISPLAY_ORIENTATION_0);
5442 prepareButtons();
5443 prepareAxes(POSITION);
5444 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005445 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446
5447 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005448 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449
5450 // Virtual key is down.
5451 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5452 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5453 processDown(mapper, x, y);
5454 processSync(mapper);
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5456
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005457 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458
5459 // Virtual key is up.
5460 processUp(mapper);
5461 processSync(mapper);
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005464 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465}
5466
5467TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468 addConfigurationProperty("touch.deviceType", "touchScreen");
5469 prepareDisplay(DISPLAY_ORIENTATION_0);
5470 prepareButtons();
5471 prepareAxes(POSITION);
5472 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005473 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474
5475 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005476 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477
5478 // Virtual key is down.
5479 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5480 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5481 processDown(mapper, x, y);
5482 processSync(mapper);
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5484
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005485 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486
5487 // Virtual key is up.
5488 processUp(mapper);
5489 processSync(mapper);
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493}
5494
5495TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496 addConfigurationProperty("touch.deviceType", "touchScreen");
5497 prepareDisplay(DISPLAY_ORIENTATION_0);
5498 prepareButtons();
5499 prepareAxes(POSITION);
5500 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005501 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502
5503 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5504 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005505 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506 ASSERT_TRUE(flags[0]);
5507 ASSERT_FALSE(flags[1]);
5508}
5509
5510TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 addConfigurationProperty("touch.deviceType", "touchScreen");
5512 prepareDisplay(DISPLAY_ORIENTATION_0);
5513 prepareButtons();
5514 prepareAxes(POSITION);
5515 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005516 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517
arthurhungdcef2dc2020-08-11 14:47:50 +08005518 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519
5520 NotifyKeyArgs args;
5521
5522 // Press virtual key.
5523 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5524 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5525 processDown(mapper, x, y);
5526 processSync(mapper);
5527
5528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5529 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5530 ASSERT_EQ(DEVICE_ID, args.deviceId);
5531 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5532 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5533 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5534 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5535 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5536 ASSERT_EQ(KEY_HOME, args.scanCode);
5537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5538 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5539
5540 // Release virtual key.
5541 processUp(mapper);
5542 processSync(mapper);
5543
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5545 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5546 ASSERT_EQ(DEVICE_ID, args.deviceId);
5547 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5548 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5549 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5550 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5551 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5552 ASSERT_EQ(KEY_HOME, args.scanCode);
5553 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5554 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5555
5556 // Should not have sent any motions.
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5558}
5559
5560TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561 addConfigurationProperty("touch.deviceType", "touchScreen");
5562 prepareDisplay(DISPLAY_ORIENTATION_0);
5563 prepareButtons();
5564 prepareAxes(POSITION);
5565 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005566 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567
arthurhungdcef2dc2020-08-11 14:47:50 +08005568 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569
5570 NotifyKeyArgs keyArgs;
5571
5572 // Press virtual key.
5573 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5574 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5575 processDown(mapper, x, y);
5576 processSync(mapper);
5577
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5579 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5580 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5581 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5582 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5583 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5584 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5585 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5586 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5588 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5589
5590 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5591 // into the display area.
5592 y -= 100;
5593 processMove(mapper, x, y);
5594 processSync(mapper);
5595
5596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5597 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5598 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5599 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5600 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5601 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5602 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5603 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5604 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5605 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5607 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5608
5609 NotifyMotionArgs motionArgs;
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5612 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5613 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5614 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5616 ASSERT_EQ(0, motionArgs.flags);
5617 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5618 ASSERT_EQ(0, motionArgs.buttonState);
5619 ASSERT_EQ(0, motionArgs.edgeFlags);
5620 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5621 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5624 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5625 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5626 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5628
5629 // Keep moving out of bounds. Should generate a pointer move.
5630 y -= 50;
5631 processMove(mapper, x, y);
5632 processSync(mapper);
5633
5634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5635 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5636 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5637 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5638 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5640 ASSERT_EQ(0, motionArgs.flags);
5641 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5642 ASSERT_EQ(0, motionArgs.buttonState);
5643 ASSERT_EQ(0, motionArgs.edgeFlags);
5644 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5645 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5648 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5649 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5650 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5651 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5652
5653 // Release out of bounds. Should generate a pointer up.
5654 processUp(mapper);
5655 processSync(mapper);
5656
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5659 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5660 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5661 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5662 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5663 ASSERT_EQ(0, motionArgs.flags);
5664 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5665 ASSERT_EQ(0, motionArgs.buttonState);
5666 ASSERT_EQ(0, motionArgs.edgeFlags);
5667 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5668 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5671 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5672 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5673 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5674 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5675
5676 // Should not have sent any more keys or motions.
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5679}
5680
5681TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682 addConfigurationProperty("touch.deviceType", "touchScreen");
5683 prepareDisplay(DISPLAY_ORIENTATION_0);
5684 prepareButtons();
5685 prepareAxes(POSITION);
5686 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005687 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688
arthurhungdcef2dc2020-08-11 14:47:50 +08005689 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690
5691 NotifyMotionArgs motionArgs;
5692
5693 // Initially go down out of bounds.
5694 int32_t x = -10;
5695 int32_t y = -10;
5696 processDown(mapper, x, y);
5697 processSync(mapper);
5698
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5700
5701 // Move into the display area. Should generate a pointer down.
5702 x = 50;
5703 y = 75;
5704 processMove(mapper, x, y);
5705 processSync(mapper);
5706
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5709 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5710 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5711 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5713 ASSERT_EQ(0, motionArgs.flags);
5714 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5715 ASSERT_EQ(0, motionArgs.buttonState);
5716 ASSERT_EQ(0, motionArgs.edgeFlags);
5717 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5721 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5722 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5723 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5724 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5725
5726 // Release. Should generate a pointer up.
5727 processUp(mapper);
5728 processSync(mapper);
5729
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5732 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5733 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5734 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5735 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5736 ASSERT_EQ(0, motionArgs.flags);
5737 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5738 ASSERT_EQ(0, motionArgs.buttonState);
5739 ASSERT_EQ(0, motionArgs.edgeFlags);
5740 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5741 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5744 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5745 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5746 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5747 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5748
5749 // Should not have sent any more keys or motions.
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5752}
5753
Santos Cordonfa5cf462017-04-05 10:37:00 -07005754TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005755 addConfigurationProperty("touch.deviceType", "touchScreen");
5756 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5757
5758 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5759 prepareButtons();
5760 prepareAxes(POSITION);
5761 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005762 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005763
arthurhungdcef2dc2020-08-11 14:47:50 +08005764 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005765
5766 NotifyMotionArgs motionArgs;
5767
5768 // Down.
5769 int32_t x = 100;
5770 int32_t y = 125;
5771 processDown(mapper, x, y);
5772 processSync(mapper);
5773
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5775 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5776 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5777 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5778 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5779 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5780 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5781 ASSERT_EQ(0, motionArgs.flags);
5782 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5783 ASSERT_EQ(0, motionArgs.buttonState);
5784 ASSERT_EQ(0, motionArgs.edgeFlags);
5785 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5786 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5789 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5790 1, 0, 0, 0, 0, 0, 0, 0));
5791 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5792 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5793 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5794
5795 // Move.
5796 x += 50;
5797 y += 75;
5798 processMove(mapper, x, y);
5799 processSync(mapper);
5800
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5802 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5803 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5804 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5805 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5806 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5808 ASSERT_EQ(0, motionArgs.flags);
5809 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5810 ASSERT_EQ(0, motionArgs.buttonState);
5811 ASSERT_EQ(0, motionArgs.edgeFlags);
5812 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5813 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5816 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5817 1, 0, 0, 0, 0, 0, 0, 0));
5818 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5819 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5821
5822 // Up.
5823 processUp(mapper);
5824 processSync(mapper);
5825
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5827 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5828 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5829 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5830 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5831 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5832 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5833 ASSERT_EQ(0, motionArgs.flags);
5834 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5835 ASSERT_EQ(0, motionArgs.buttonState);
5836 ASSERT_EQ(0, motionArgs.edgeFlags);
5837 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5838 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5839 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5841 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5842 1, 0, 0, 0, 0, 0, 0, 0));
5843 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5844 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5846
5847 // Should not have sent any more keys or motions.
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5850}
5851
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853 addConfigurationProperty("touch.deviceType", "touchScreen");
5854 prepareDisplay(DISPLAY_ORIENTATION_0);
5855 prepareButtons();
5856 prepareAxes(POSITION);
5857 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005858 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859
arthurhungdcef2dc2020-08-11 14:47:50 +08005860 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861
5862 NotifyMotionArgs motionArgs;
5863
5864 // Down.
5865 int32_t x = 100;
5866 int32_t y = 125;
5867 processDown(mapper, x, y);
5868 processSync(mapper);
5869
5870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5871 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5872 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5873 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5874 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5875 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5876 ASSERT_EQ(0, motionArgs.flags);
5877 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5878 ASSERT_EQ(0, motionArgs.buttonState);
5879 ASSERT_EQ(0, motionArgs.edgeFlags);
5880 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5881 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5884 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5885 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5886 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5887 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5888
5889 // Move.
5890 x += 50;
5891 y += 75;
5892 processMove(mapper, x, y);
5893 processSync(mapper);
5894
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5896 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5897 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5898 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5899 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5901 ASSERT_EQ(0, motionArgs.flags);
5902 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5903 ASSERT_EQ(0, motionArgs.buttonState);
5904 ASSERT_EQ(0, motionArgs.edgeFlags);
5905 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5906 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5909 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5910 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5911 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5913
5914 // Up.
5915 processUp(mapper);
5916 processSync(mapper);
5917
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5920 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5921 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5922 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5923 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5924 ASSERT_EQ(0, motionArgs.flags);
5925 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5926 ASSERT_EQ(0, motionArgs.buttonState);
5927 ASSERT_EQ(0, motionArgs.edgeFlags);
5928 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5929 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5932 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5933 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5934 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5935 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5936
5937 // Should not have sent any more keys or motions.
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5940}
5941
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005942TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943 addConfigurationProperty("touch.deviceType", "touchScreen");
5944 prepareButtons();
5945 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005946 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5947 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005948 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949
5950 NotifyMotionArgs args;
5951
5952 // Rotation 90.
5953 prepareDisplay(DISPLAY_ORIENTATION_90);
5954 processDown(mapper, toRawX(50), toRawY(75));
5955 processSync(mapper);
5956
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5958 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5959 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5960
5961 processUp(mapper);
5962 processSync(mapper);
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5964}
5965
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005966TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 addConfigurationProperty("touch.deviceType", "touchScreen");
5968 prepareButtons();
5969 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005970 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5971 // orientation-aware are affected by display rotation.
5972 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005973 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974
5975 NotifyMotionArgs args;
5976
5977 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005978 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 prepareDisplay(DISPLAY_ORIENTATION_0);
5980 processDown(mapper, toRawX(50), toRawY(75));
5981 processSync(mapper);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5984 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5985 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5986
5987 processUp(mapper);
5988 processSync(mapper);
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5990
5991 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005992 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005994 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 processSync(mapper);
5996
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5998 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5999 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6000
6001 processUp(mapper);
6002 processSync(mapper);
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6004
6005 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006006 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007 prepareDisplay(DISPLAY_ORIENTATION_180);
6008 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6009 processSync(mapper);
6010
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6012 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6013 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6014
6015 processUp(mapper);
6016 processSync(mapper);
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6018
6019 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006020 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006021 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006022 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 processSync(mapper);
6024
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6026 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6027 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6028
6029 processUp(mapper);
6030 processSync(mapper);
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6032}
6033
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006034TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6035 addConfigurationProperty("touch.deviceType", "touchScreen");
6036 prepareButtons();
6037 prepareAxes(POSITION);
6038 addConfigurationProperty("touch.orientationAware", "1");
6039 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6040 clearViewports();
6041 prepareDisplay(DISPLAY_ORIENTATION_0);
6042 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6043 NotifyMotionArgs args;
6044
6045 // Orientation 0.
6046 processDown(mapper, toRawX(50), toRawY(75));
6047 processSync(mapper);
6048
6049 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6050 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6051 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6052
6053 processUp(mapper);
6054 processSync(mapper);
6055 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6056}
6057
6058TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6059 addConfigurationProperty("touch.deviceType", "touchScreen");
6060 prepareButtons();
6061 prepareAxes(POSITION);
6062 addConfigurationProperty("touch.orientationAware", "1");
6063 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6064 clearViewports();
6065 prepareDisplay(DISPLAY_ORIENTATION_0);
6066 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6067 NotifyMotionArgs args;
6068
6069 // Orientation 90.
6070 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6071 processSync(mapper);
6072
6073 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6074 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6075 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6076
6077 processUp(mapper);
6078 processSync(mapper);
6079 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6080}
6081
6082TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6083 addConfigurationProperty("touch.deviceType", "touchScreen");
6084 prepareButtons();
6085 prepareAxes(POSITION);
6086 addConfigurationProperty("touch.orientationAware", "1");
6087 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6088 clearViewports();
6089 prepareDisplay(DISPLAY_ORIENTATION_0);
6090 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6091 NotifyMotionArgs args;
6092
6093 // Orientation 180.
6094 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6095 processSync(mapper);
6096
6097 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6098 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6099 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6100
6101 processUp(mapper);
6102 processSync(mapper);
6103 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6104}
6105
6106TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6107 addConfigurationProperty("touch.deviceType", "touchScreen");
6108 prepareButtons();
6109 prepareAxes(POSITION);
6110 addConfigurationProperty("touch.orientationAware", "1");
6111 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6112 clearViewports();
6113 prepareDisplay(DISPLAY_ORIENTATION_0);
6114 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6115 NotifyMotionArgs args;
6116
6117 // Orientation 270.
6118 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6119 processSync(mapper);
6120
6121 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6122 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6123 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6124
6125 processUp(mapper);
6126 processSync(mapper);
6127 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6128}
6129
6130TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6131 addConfigurationProperty("touch.deviceType", "touchScreen");
6132 prepareButtons();
6133 prepareAxes(POSITION);
6134 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6135 // orientation-aware are affected by display rotation.
6136 addConfigurationProperty("touch.orientationAware", "0");
6137 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6138 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6139
6140 NotifyMotionArgs args;
6141
6142 // Orientation 90, Rotation 0.
6143 clearViewports();
6144 prepareDisplay(DISPLAY_ORIENTATION_0);
6145 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6146 processSync(mapper);
6147
6148 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6149 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6150 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6151
6152 processUp(mapper);
6153 processSync(mapper);
6154 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6155
6156 // Orientation 90, Rotation 90.
6157 clearViewports();
6158 prepareDisplay(DISPLAY_ORIENTATION_90);
6159 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6160 processSync(mapper);
6161
6162 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6163 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6164 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6165
6166 processUp(mapper);
6167 processSync(mapper);
6168 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6169
6170 // Orientation 90, Rotation 180.
6171 clearViewports();
6172 prepareDisplay(DISPLAY_ORIENTATION_180);
6173 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6174 processSync(mapper);
6175
6176 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6177 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6178 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6179
6180 processUp(mapper);
6181 processSync(mapper);
6182 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6183
6184 // Orientation 90, Rotation 270.
6185 clearViewports();
6186 prepareDisplay(DISPLAY_ORIENTATION_270);
6187 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6188 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6189 processSync(mapper);
6190
6191 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6192 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6193 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6194
6195 processUp(mapper);
6196 processSync(mapper);
6197 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6198}
6199
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201 addConfigurationProperty("touch.deviceType", "touchScreen");
6202 prepareDisplay(DISPLAY_ORIENTATION_0);
6203 prepareButtons();
6204 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006205 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206
6207 // These calculations are based on the input device calibration documentation.
6208 int32_t rawX = 100;
6209 int32_t rawY = 200;
6210 int32_t rawPressure = 10;
6211 int32_t rawToolMajor = 12;
6212 int32_t rawDistance = 2;
6213 int32_t rawTiltX = 30;
6214 int32_t rawTiltY = 110;
6215
6216 float x = toDisplayX(rawX);
6217 float y = toDisplayY(rawY);
6218 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6219 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6220 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6221 float distance = float(rawDistance);
6222
6223 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6224 float tiltScale = M_PI / 180;
6225 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6226 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6227 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6228 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6229
6230 processDown(mapper, rawX, rawY);
6231 processPressure(mapper, rawPressure);
6232 processToolMajor(mapper, rawToolMajor);
6233 processDistance(mapper, rawDistance);
6234 processTilt(mapper, rawTiltX, rawTiltY);
6235 processSync(mapper);
6236
6237 NotifyMotionArgs args;
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6240 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6241 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6242}
6243
Jason Gerecke489fda82012-09-07 17:19:40 -07006244TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006245 addConfigurationProperty("touch.deviceType", "touchScreen");
6246 prepareDisplay(DISPLAY_ORIENTATION_0);
6247 prepareLocationCalibration();
6248 prepareButtons();
6249 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006250 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006251
6252 int32_t rawX = 100;
6253 int32_t rawY = 200;
6254
6255 float x = toDisplayX(toCookedX(rawX, rawY));
6256 float y = toDisplayY(toCookedY(rawX, rawY));
6257
6258 processDown(mapper, rawX, rawY);
6259 processSync(mapper);
6260
6261 NotifyMotionArgs args;
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6264 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6265}
6266
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006268 addConfigurationProperty("touch.deviceType", "touchScreen");
6269 prepareDisplay(DISPLAY_ORIENTATION_0);
6270 prepareButtons();
6271 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006272 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 NotifyMotionArgs motionArgs;
6275 NotifyKeyArgs keyArgs;
6276
6277 processDown(mapper, 100, 200);
6278 processSync(mapper);
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6281 ASSERT_EQ(0, motionArgs.buttonState);
6282
6283 // press BTN_LEFT, release BTN_LEFT
6284 processKey(mapper, BTN_LEFT, 1);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6288 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6289
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6292 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6293
Michael Wrightd02c5b62014-02-10 15:10:22 -08006294 processKey(mapper, BTN_LEFT, 0);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303
6304 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6305 processKey(mapper, BTN_RIGHT, 1);
6306 processKey(mapper, BTN_MIDDLE, 1);
6307 processSync(mapper);
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6310 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6311 motionArgs.buttonState);
6312
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6315 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6316
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6319 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6320 motionArgs.buttonState);
6321
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322 processKey(mapper, BTN_RIGHT, 0);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331
6332 processKey(mapper, BTN_MIDDLE, 0);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006340 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341
6342 // press BTN_BACK, release BTN_BACK
6343 processKey(mapper, BTN_BACK, 1);
6344 processSync(mapper);
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6346 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6347 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006348
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006351 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6352
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6355 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356
6357 processKey(mapper, BTN_BACK, 0);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006365 ASSERT_EQ(0, motionArgs.buttonState);
6366
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6368 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6369 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6370
6371 // press BTN_SIDE, release BTN_SIDE
6372 processKey(mapper, BTN_SIDE, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6375 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6376 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006377
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006380 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6381
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6384 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385
6386 processKey(mapper, BTN_SIDE, 0);
6387 processSync(mapper);
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006394 ASSERT_EQ(0, motionArgs.buttonState);
6395
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6397 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6398 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6399
6400 // press BTN_FORWARD, release BTN_FORWARD
6401 processKey(mapper, BTN_FORWARD, 1);
6402 processSync(mapper);
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6404 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6405 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006409 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6410
6411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6413 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414
6415 processKey(mapper, BTN_FORWARD, 0);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006420
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006423 ASSERT_EQ(0, motionArgs.buttonState);
6424
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6426 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6427 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6428
6429 // press BTN_EXTRA, release BTN_EXTRA
6430 processKey(mapper, BTN_EXTRA, 1);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6433 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6434 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006435
Michael Wrightd02c5b62014-02-10 15:10:22 -08006436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6439
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6442 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443
6444 processKey(mapper, BTN_EXTRA, 0);
6445 processSync(mapper);
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006448 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006449
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006452 ASSERT_EQ(0, motionArgs.buttonState);
6453
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6455 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6456 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6457
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6459
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 // press BTN_STYLUS, release BTN_STYLUS
6461 processKey(mapper, BTN_STYLUS, 1);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006465 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6466
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6469 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006470
6471 processKey(mapper, BTN_STYLUS, 0);
6472 processSync(mapper);
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006476
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480
6481 // press BTN_STYLUS2, release BTN_STYLUS2
6482 processKey(mapper, BTN_STYLUS2, 1);
6483 processSync(mapper);
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006486 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6487
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6490 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006491
6492 processKey(mapper, BTN_STYLUS2, 0);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006497
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006500 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501
6502 // release touch
6503 processUp(mapper);
6504 processSync(mapper);
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6507 ASSERT_EQ(0, motionArgs.buttonState);
6508}
6509
6510TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 addConfigurationProperty("touch.deviceType", "touchScreen");
6512 prepareDisplay(DISPLAY_ORIENTATION_0);
6513 prepareButtons();
6514 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006515 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516
6517 NotifyMotionArgs motionArgs;
6518
6519 // default tool type is finger
6520 processDown(mapper, 100, 200);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6525
6526 // eraser
6527 processKey(mapper, BTN_TOOL_RUBBER, 1);
6528 processSync(mapper);
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6532
6533 // stylus
6534 processKey(mapper, BTN_TOOL_RUBBER, 0);
6535 processKey(mapper, BTN_TOOL_PEN, 1);
6536 processSync(mapper);
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6540
6541 // brush
6542 processKey(mapper, BTN_TOOL_PEN, 0);
6543 processKey(mapper, BTN_TOOL_BRUSH, 1);
6544 processSync(mapper);
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6548
6549 // pencil
6550 processKey(mapper, BTN_TOOL_BRUSH, 0);
6551 processKey(mapper, BTN_TOOL_PENCIL, 1);
6552 processSync(mapper);
6553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6556
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006557 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006558 processKey(mapper, BTN_TOOL_PENCIL, 0);
6559 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6560 processSync(mapper);
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6564
6565 // mouse
6566 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6567 processKey(mapper, BTN_TOOL_MOUSE, 1);
6568 processSync(mapper);
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6572
6573 // lens
6574 processKey(mapper, BTN_TOOL_MOUSE, 0);
6575 processKey(mapper, BTN_TOOL_LENS, 1);
6576 processSync(mapper);
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6580
6581 // double-tap
6582 processKey(mapper, BTN_TOOL_LENS, 0);
6583 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6588
6589 // triple-tap
6590 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6591 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6596
6597 // quad-tap
6598 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6599 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6600 processSync(mapper);
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6604
6605 // finger
6606 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6607 processKey(mapper, BTN_TOOL_FINGER, 1);
6608 processSync(mapper);
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6612
6613 // stylus trumps finger
6614 processKey(mapper, BTN_TOOL_PEN, 1);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6619
6620 // eraser trumps stylus
6621 processKey(mapper, BTN_TOOL_RUBBER, 1);
6622 processSync(mapper);
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6626
6627 // mouse trumps eraser
6628 processKey(mapper, BTN_TOOL_MOUSE, 1);
6629 processSync(mapper);
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6633
6634 // back to default tool type
6635 processKey(mapper, BTN_TOOL_MOUSE, 0);
6636 processKey(mapper, BTN_TOOL_RUBBER, 0);
6637 processKey(mapper, BTN_TOOL_PEN, 0);
6638 processKey(mapper, BTN_TOOL_FINGER, 0);
6639 processSync(mapper);
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6643}
6644
6645TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006646 addConfigurationProperty("touch.deviceType", "touchScreen");
6647 prepareDisplay(DISPLAY_ORIENTATION_0);
6648 prepareButtons();
6649 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006650 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006651 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006652
6653 NotifyMotionArgs motionArgs;
6654
6655 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6656 processKey(mapper, BTN_TOOL_FINGER, 1);
6657 processMove(mapper, 100, 200);
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6662 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6663
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6667 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6668
6669 // move a little
6670 processMove(mapper, 150, 250);
6671 processSync(mapper);
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6675 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6676
6677 // down when BTN_TOUCH is pressed, pressure defaults to 1
6678 processKey(mapper, BTN_TOUCH, 1);
6679 processSync(mapper);
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6683 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6684
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6688 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6689
6690 // up when BTN_TOUCH is released, hover restored
6691 processKey(mapper, BTN_TOUCH, 0);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6697
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6701 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6702
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6704 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6706 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6707
6708 // exit hover when pointer goes away
6709 processKey(mapper, BTN_TOOL_FINGER, 0);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6712 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6714 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6715}
6716
6717TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006718 addConfigurationProperty("touch.deviceType", "touchScreen");
6719 prepareDisplay(DISPLAY_ORIENTATION_0);
6720 prepareButtons();
6721 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006722 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006723
6724 NotifyMotionArgs motionArgs;
6725
6726 // initially hovering because pressure is 0
6727 processDown(mapper, 100, 200);
6728 processPressure(mapper, 0);
6729 processSync(mapper);
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6733 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6734
6735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6736 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6738 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6739
6740 // move a little
6741 processMove(mapper, 150, 250);
6742 processSync(mapper);
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6744 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6746 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6747
6748 // down when pressure is non-zero
6749 processPressure(mapper, RAW_PRESSURE_MAX);
6750 processSync(mapper);
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6754 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6755
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6759 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6760
6761 // up when pressure becomes 0, hover restored
6762 processPressure(mapper, 0);
6763 processSync(mapper);
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6765 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6767 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6768
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6772 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6773
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6777 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6778
6779 // exit hover when pointer goes away
6780 processUp(mapper);
6781 processSync(mapper);
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6785 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6786}
6787
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006788TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6789 addConfigurationProperty("touch.deviceType", "touchScreen");
6790 prepareDisplay(DISPLAY_ORIENTATION_0);
6791 prepareButtons();
6792 prepareAxes(POSITION | PRESSURE);
6793 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6794 NotifyMotionArgs motionArgs;
6795
6796 // Set the initial state for the touch pointer.
6797 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6798 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6799 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6800 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6801
6802 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
6803 // state by reading the current axis values.
6804 mapper.reset(ARBITRARY_TIME);
6805
6806 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6807 // the recreated touch state to generate a down event.
6808 processSync(mapper);
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6810 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6811
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6813}
6814
lilinnane74b35f2022-07-19 16:00:50 +08006815TEST_F(SingleTouchInputMapperTest,
6816 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6817 addConfigurationProperty("touch.deviceType", "touchScreen");
6818 prepareDisplay(DISPLAY_ORIENTATION_0);
6819 prepareButtons();
6820 prepareAxes(POSITION);
6821 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6822 NotifyMotionArgs motionArgs;
6823
6824 // Down.
6825 processDown(mapper, 100, 200);
6826 processSync(mapper);
6827
6828 // We should receive a down event
6829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6830 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6831
6832 // Change display id
6833 clearViewports();
6834 prepareSecondaryDisplay(ViewportType::INTERNAL);
6835
6836 // We should receive a cancel event
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6838 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6839 // Then receive reset called
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6841}
6842
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006843TEST_F(SingleTouchInputMapperTest,
6844 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6845 addConfigurationProperty("touch.deviceType", "touchScreen");
6846 prepareDisplay(DISPLAY_ORIENTATION_0);
6847 prepareButtons();
6848 prepareAxes(POSITION);
6849 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6851 NotifyMotionArgs motionArgs;
6852
6853 // Start a new gesture.
6854 processDown(mapper, 100, 200);
6855 processSync(mapper);
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6857 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6858
6859 // Make the viewport inactive. This will put the device in disabled mode.
6860 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6861 viewport->isActive = false;
6862 mFakePolicy->updateViewport(*viewport);
6863 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6864
6865 // We should receive a cancel event for the ongoing gesture.
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6867 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6868 // Then we should be notified that the device was reset.
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6870
6871 // No events are generated while the viewport is inactive.
6872 processMove(mapper, 101, 201);
6873 processSync(mapper);
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006874 processUp(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006875 processSync(mapper);
6876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6877
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006878 // Start a new gesture while the viewport is still inactive.
6879 processDown(mapper, 300, 400);
6880 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6881 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6882 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6883 processSync(mapper);
6884
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006885 // Make the viewport active again. The device should resume processing events.
6886 viewport->isActive = true;
6887 mFakePolicy->updateViewport(*viewport);
6888 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6889
6890 // The device is reset because it changes back to direct mode, without generating any events.
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6893
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006894 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006895 processSync(mapper);
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6897 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6898
6899 // No more events.
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6902}
6903
Prabir Pradhan5632d622021-09-06 07:57:20 -07006904// --- TouchDisplayProjectionTest ---
6905
6906class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6907public:
6908 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6909 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6910 // rotated equivalent of the given un-rotated physical display bounds.
6911 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6912 uint32_t inverseRotationFlags;
6913 auto width = DISPLAY_WIDTH;
6914 auto height = DISPLAY_HEIGHT;
6915 switch (orientation) {
6916 case DISPLAY_ORIENTATION_90:
6917 inverseRotationFlags = ui::Transform::ROT_270;
6918 std::swap(width, height);
6919 break;
6920 case DISPLAY_ORIENTATION_180:
6921 inverseRotationFlags = ui::Transform::ROT_180;
6922 break;
6923 case DISPLAY_ORIENTATION_270:
6924 inverseRotationFlags = ui::Transform::ROT_90;
6925 std::swap(width, height);
6926 break;
6927 case DISPLAY_ORIENTATION_0:
6928 inverseRotationFlags = ui::Transform::ROT_0;
6929 break;
6930 default:
6931 FAIL() << "Invalid orientation: " << orientation;
6932 }
6933
6934 const ui::Transform rotation(inverseRotationFlags, width, height);
6935 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6936
6937 std::optional<DisplayViewport> internalViewport =
6938 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6939 DisplayViewport& v = *internalViewport;
6940 v.displayId = DISPLAY_ID;
6941 v.orientation = orientation;
6942
6943 v.logicalLeft = 0;
6944 v.logicalTop = 0;
6945 v.logicalRight = 100;
6946 v.logicalBottom = 100;
6947
6948 v.physicalLeft = rotatedPhysicalDisplay.left;
6949 v.physicalTop = rotatedPhysicalDisplay.top;
6950 v.physicalRight = rotatedPhysicalDisplay.right;
6951 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6952
6953 v.deviceWidth = width;
6954 v.deviceHeight = height;
6955
6956 v.isActive = true;
6957 v.uniqueId = UNIQUE_ID;
6958 v.type = ViewportType::INTERNAL;
6959 mFakePolicy->updateViewport(v);
6960 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6961 }
6962
6963 void assertReceivedMove(const Point& point) {
6964 NotifyMotionArgs motionArgs;
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6967 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6969 1, 0, 0, 0, 0, 0, 0, 0));
6970 }
6971};
6972
6973TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6974 addConfigurationProperty("touch.deviceType", "touchScreen");
6975 prepareDisplay(DISPLAY_ORIENTATION_0);
6976
6977 prepareButtons();
6978 prepareAxes(POSITION);
6979 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6980
6981 NotifyMotionArgs motionArgs;
6982
6983 // Configure the DisplayViewport such that the logical display maps to a subsection of
6984 // the display panel called the physical display. Here, the physical display is bounded by the
6985 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6986 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6987 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6988 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6989
6990 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6991 DISPLAY_ORIENTATION_270}) {
6992 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6993
6994 // Touches outside the physical display should be ignored, and should not generate any
6995 // events. Ensure touches at the following points that lie outside of the physical display
6996 // area do not generate any events.
6997 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6998 processDown(mapper, toRawX(point.x), toRawY(point.y));
6999 processSync(mapper);
7000 processUp(mapper);
7001 processSync(mapper);
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7003 << "Unexpected event generated for touch outside physical display at point: "
7004 << point.x << ", " << point.y;
7005 }
7006 }
7007}
7008
7009TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7010 addConfigurationProperty("touch.deviceType", "touchScreen");
7011 prepareDisplay(DISPLAY_ORIENTATION_0);
7012
7013 prepareButtons();
7014 prepareAxes(POSITION);
7015 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7016
7017 NotifyMotionArgs motionArgs;
7018
7019 // Configure the DisplayViewport such that the logical display maps to a subsection of
7020 // the display panel called the physical display. Here, the physical display is bounded by the
7021 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7022 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7023
7024 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7025 DISPLAY_ORIENTATION_270}) {
7026 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7027
7028 // Touches that start outside the physical display should be ignored until it enters the
7029 // physical display bounds, at which point it should generate a down event. Start a touch at
7030 // the point (5, 100), which is outside the physical display bounds.
7031 static const Point kOutsidePoint{5, 100};
7032 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7033 processSync(mapper);
7034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7035
7036 // Move the touch into the physical display area. This should generate a pointer down.
7037 processMove(mapper, toRawX(11), toRawY(21));
7038 processSync(mapper);
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7040 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7041 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7042 ASSERT_NO_FATAL_FAILURE(
7043 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7044
7045 // Move the touch inside the physical display area. This should generate a pointer move.
7046 processMove(mapper, toRawX(69), toRawY(159));
7047 processSync(mapper);
7048 assertReceivedMove({69, 159});
7049
7050 // Move outside the physical display area. Since the pointer is already down, this should
7051 // now continue generating events.
7052 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7053 processSync(mapper);
7054 assertReceivedMove(kOutsidePoint);
7055
7056 // Release. This should generate a pointer up.
7057 processUp(mapper);
7058 processSync(mapper);
7059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7060 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7062 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7063
7064 // Ensure no more events were generated.
7065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7067 }
7068}
7069
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070// --- MultiTouchInputMapperTest ---
7071
7072class MultiTouchInputMapperTest : public TouchInputMapperTest {
7073protected:
7074 void prepareAxes(int axes);
7075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007076 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7077 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7078 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7079 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7080 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7081 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7082 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7083 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7084 void processId(MultiTouchInputMapper& mapper, int32_t id);
7085 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7086 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7087 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7088 void processMTSync(MultiTouchInputMapper& mapper);
7089 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090};
7091
7092void MultiTouchInputMapperTest::prepareAxes(int axes) {
7093 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7095 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096 }
7097 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7099 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007101 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7102 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103 }
7104 }
7105 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007106 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7107 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007110 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 }
7112 }
7113 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007114 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7115 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116 }
7117 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007118 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7119 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120 }
7121 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007122 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7123 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124 }
7125 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007126 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7127 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 }
7129 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7131 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132 }
7133 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007134 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135 }
7136}
7137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007138void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7139 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007142}
7143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007144void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7145 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147}
7148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007149void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7150 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152}
7153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007154void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156}
7157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007158void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160}
7161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007162void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7163 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165}
7166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007167void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169}
7170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007171void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173}
7174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007175void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177}
7178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007179void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181}
7182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007183void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007185}
7186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007187void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7188 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190}
7191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007192void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194}
7195
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007196void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198}
7199
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007201 addConfigurationProperty("touch.deviceType", "touchScreen");
7202 prepareDisplay(DISPLAY_ORIENTATION_0);
7203 prepareAxes(POSITION);
7204 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007205 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206
arthurhungdcef2dc2020-08-11 14:47:50 +08007207 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208
7209 NotifyMotionArgs motionArgs;
7210
7211 // Two fingers down at once.
7212 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7213 processPosition(mapper, x1, y1);
7214 processMTSync(mapper);
7215 processPosition(mapper, x2, y2);
7216 processMTSync(mapper);
7217 processSync(mapper);
7218
7219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7220 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7221 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7222 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7223 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7224 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7225 ASSERT_EQ(0, motionArgs.flags);
7226 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7227 ASSERT_EQ(0, motionArgs.buttonState);
7228 ASSERT_EQ(0, motionArgs.edgeFlags);
7229 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7230 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7233 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7234 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7235 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7237
7238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7240 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7241 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7242 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007243 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007244 ASSERT_EQ(0, motionArgs.flags);
7245 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7246 ASSERT_EQ(0, motionArgs.buttonState);
7247 ASSERT_EQ(0, motionArgs.edgeFlags);
7248 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7249 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7251 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7254 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7256 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7257 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7258 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7260
7261 // Move.
7262 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7263 processPosition(mapper, x1, y1);
7264 processMTSync(mapper);
7265 processPosition(mapper, x2, y2);
7266 processMTSync(mapper);
7267 processSync(mapper);
7268
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7275 ASSERT_EQ(0, motionArgs.flags);
7276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7277 ASSERT_EQ(0, motionArgs.buttonState);
7278 ASSERT_EQ(0, motionArgs.edgeFlags);
7279 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7282 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7285 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7287 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7288 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7289 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7290 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7291
7292 // First finger up.
7293 x2 += 15; y2 -= 20;
7294 processPosition(mapper, x2, y2);
7295 processMTSync(mapper);
7296 processSync(mapper);
7297
7298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7299 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7300 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7301 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7302 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007303 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304 ASSERT_EQ(0, motionArgs.flags);
7305 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7306 ASSERT_EQ(0, motionArgs.buttonState);
7307 ASSERT_EQ(0, motionArgs.edgeFlags);
7308 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7309 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7311 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7314 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7316 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7317 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7318 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7320
7321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7322 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7323 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7324 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7325 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7327 ASSERT_EQ(0, motionArgs.flags);
7328 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7329 ASSERT_EQ(0, motionArgs.buttonState);
7330 ASSERT_EQ(0, motionArgs.edgeFlags);
7331 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7332 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7335 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7336 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7337 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7339
7340 // Move.
7341 x2 += 20; y2 -= 25;
7342 processPosition(mapper, x2, y2);
7343 processMTSync(mapper);
7344 processSync(mapper);
7345
7346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7347 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7348 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7349 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7350 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7352 ASSERT_EQ(0, motionArgs.flags);
7353 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7354 ASSERT_EQ(0, motionArgs.buttonState);
7355 ASSERT_EQ(0, motionArgs.edgeFlags);
7356 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7357 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7360 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7361 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7362 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7364
7365 // New finger down.
7366 int32_t x3 = 700, y3 = 300;
7367 processPosition(mapper, x2, y2);
7368 processMTSync(mapper);
7369 processPosition(mapper, x3, y3);
7370 processMTSync(mapper);
7371 processSync(mapper);
7372
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7375 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7376 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7377 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007378 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007379 ASSERT_EQ(0, motionArgs.flags);
7380 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7381 ASSERT_EQ(0, motionArgs.buttonState);
7382 ASSERT_EQ(0, motionArgs.edgeFlags);
7383 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7384 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7386 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7389 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7391 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7392 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7393 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7395
7396 // Second finger up.
7397 x3 += 30; y3 -= 20;
7398 processPosition(mapper, x3, y3);
7399 processMTSync(mapper);
7400 processSync(mapper);
7401
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7404 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7405 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7406 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007407 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007408 ASSERT_EQ(0, motionArgs.flags);
7409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7410 ASSERT_EQ(0, motionArgs.buttonState);
7411 ASSERT_EQ(0, motionArgs.edgeFlags);
7412 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7413 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7415 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7418 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7420 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7421 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7422 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7424
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7426 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7427 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7428 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7429 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7431 ASSERT_EQ(0, motionArgs.flags);
7432 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7433 ASSERT_EQ(0, motionArgs.buttonState);
7434 ASSERT_EQ(0, motionArgs.edgeFlags);
7435 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7436 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7439 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7440 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7441 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7442 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7443
7444 // Last finger up.
7445 processMTSync(mapper);
7446 processSync(mapper);
7447
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7450 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7451 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7452 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7453 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7454 ASSERT_EQ(0, motionArgs.flags);
7455 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7456 ASSERT_EQ(0, motionArgs.buttonState);
7457 ASSERT_EQ(0, motionArgs.edgeFlags);
7458 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7459 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7462 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7463 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7464 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7466
7467 // Should not have sent any more keys or motions.
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7470}
7471
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007472TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7473 addConfigurationProperty("touch.deviceType", "touchScreen");
7474 prepareDisplay(DISPLAY_ORIENTATION_0);
7475
7476 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7477 /*fuzz*/ 0, /*resolution*/ 10);
7478 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7479 /*fuzz*/ 0, /*resolution*/ 11);
7480 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7481 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7482 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7483 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7484 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7485 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7486 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7487 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7488
7489 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7490
7491 // X and Y axes
7492 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7493 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7494 // Touch major and minor
7495 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7496 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7497 // Tool major and minor
7498 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7499 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7500}
7501
7502TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7503 addConfigurationProperty("touch.deviceType", "touchScreen");
7504 prepareDisplay(DISPLAY_ORIENTATION_0);
7505
7506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7507 /*fuzz*/ 0, /*resolution*/ 10);
7508 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7509 /*fuzz*/ 0, /*resolution*/ 11);
7510
7511 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7512
7513 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7514
7515 // Touch major and minor
7516 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7517 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7518 // Tool major and minor
7519 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7520 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7521}
7522
Michael Wrightd02c5b62014-02-10 15:10:22 -08007523TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007524 addConfigurationProperty("touch.deviceType", "touchScreen");
7525 prepareDisplay(DISPLAY_ORIENTATION_0);
7526 prepareAxes(POSITION | ID);
7527 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007528 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007529
arthurhungdcef2dc2020-08-11 14:47:50 +08007530 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007531
7532 NotifyMotionArgs motionArgs;
7533
7534 // Two fingers down at once.
7535 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7536 processPosition(mapper, x1, y1);
7537 processId(mapper, 1);
7538 processMTSync(mapper);
7539 processPosition(mapper, x2, y2);
7540 processId(mapper, 2);
7541 processMTSync(mapper);
7542 processSync(mapper);
7543
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7546 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7547 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7550 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7551
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007553 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007554 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7557 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7560 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7563
7564 // Move.
7565 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7566 processPosition(mapper, x1, y1);
7567 processId(mapper, 1);
7568 processMTSync(mapper);
7569 processPosition(mapper, x2, y2);
7570 processId(mapper, 2);
7571 processMTSync(mapper);
7572 processSync(mapper);
7573
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7576 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7577 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7579 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7582 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7584 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7585
7586 // First finger up.
7587 x2 += 15; y2 -= 20;
7588 processPosition(mapper, x2, y2);
7589 processId(mapper, 2);
7590 processMTSync(mapper);
7591 processSync(mapper);
7592
7593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007594 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007595 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7596 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7598 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7601 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7604
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7607 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7608 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7611 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7612
7613 // Move.
7614 x2 += 20; y2 -= 25;
7615 processPosition(mapper, x2, y2);
7616 processId(mapper, 2);
7617 processMTSync(mapper);
7618 processSync(mapper);
7619
7620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7622 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7623 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7626 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7627
7628 // New finger down.
7629 int32_t x3 = 700, y3 = 300;
7630 processPosition(mapper, x2, y2);
7631 processId(mapper, 2);
7632 processMTSync(mapper);
7633 processPosition(mapper, x3, y3);
7634 processId(mapper, 3);
7635 processMTSync(mapper);
7636 processSync(mapper);
7637
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007639 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007640 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7641 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7643 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7646 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7648 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7649
7650 // Second finger up.
7651 x3 += 30; y3 -= 20;
7652 processPosition(mapper, x3, y3);
7653 processId(mapper, 3);
7654 processMTSync(mapper);
7655 processSync(mapper);
7656
7657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007658 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007659 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7660 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7662 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7665 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7667 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7668
7669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7671 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7672 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7675 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7676
7677 // Last finger up.
7678 processMTSync(mapper);
7679 processSync(mapper);
7680
7681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7682 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7683 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7684 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7687 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7688
7689 // Should not have sent any more keys or motions.
7690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7692}
7693
7694TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007695 addConfigurationProperty("touch.deviceType", "touchScreen");
7696 prepareDisplay(DISPLAY_ORIENTATION_0);
7697 prepareAxes(POSITION | ID | SLOT);
7698 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007699 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007700
arthurhungdcef2dc2020-08-11 14:47:50 +08007701 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007702
7703 NotifyMotionArgs motionArgs;
7704
7705 // Two fingers down at once.
7706 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7707 processPosition(mapper, x1, y1);
7708 processId(mapper, 1);
7709 processSlot(mapper, 1);
7710 processPosition(mapper, x2, y2);
7711 processId(mapper, 2);
7712 processSync(mapper);
7713
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7715 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7716 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7717 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7720 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7721
7722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007723 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007724 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7725 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7727 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7730 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7732 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7733
7734 // Move.
7735 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7736 processSlot(mapper, 0);
7737 processPosition(mapper, x1, y1);
7738 processSlot(mapper, 1);
7739 processPosition(mapper, x2, y2);
7740 processSync(mapper);
7741
7742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7744 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7745 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7747 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7750 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7752 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7753
7754 // First finger up.
7755 x2 += 15; y2 -= 20;
7756 processSlot(mapper, 0);
7757 processId(mapper, -1);
7758 processSlot(mapper, 1);
7759 processPosition(mapper, x2, y2);
7760 processSync(mapper);
7761
7762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007763 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007764 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7765 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7767 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7770 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7772 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7773
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7776 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7777 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7780 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7781
7782 // Move.
7783 x2 += 20; y2 -= 25;
7784 processPosition(mapper, x2, y2);
7785 processSync(mapper);
7786
7787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7789 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7790 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7793 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7794
7795 // New finger down.
7796 int32_t x3 = 700, y3 = 300;
7797 processPosition(mapper, x2, y2);
7798 processSlot(mapper, 0);
7799 processId(mapper, 3);
7800 processPosition(mapper, x3, y3);
7801 processSync(mapper);
7802
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007804 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007805 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7806 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7808 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7811 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7813 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7814
7815 // Second finger up.
7816 x3 += 30; y3 -= 20;
7817 processSlot(mapper, 1);
7818 processId(mapper, -1);
7819 processSlot(mapper, 0);
7820 processPosition(mapper, x3, y3);
7821 processSync(mapper);
7822
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007824 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7826 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7828 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7831 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7833 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7834
7835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7837 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7838 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7839 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7841 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7842
7843 // Last finger up.
7844 processId(mapper, -1);
7845 processSync(mapper);
7846
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7848 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7849 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7850 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7853 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7854
7855 // Should not have sent any more keys or motions.
7856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7858}
7859
7860TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007861 addConfigurationProperty("touch.deviceType", "touchScreen");
7862 prepareDisplay(DISPLAY_ORIENTATION_0);
7863 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007864 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007865
7866 // These calculations are based on the input device calibration documentation.
7867 int32_t rawX = 100;
7868 int32_t rawY = 200;
7869 int32_t rawTouchMajor = 7;
7870 int32_t rawTouchMinor = 6;
7871 int32_t rawToolMajor = 9;
7872 int32_t rawToolMinor = 8;
7873 int32_t rawPressure = 11;
7874 int32_t rawDistance = 0;
7875 int32_t rawOrientation = 3;
7876 int32_t id = 5;
7877
7878 float x = toDisplayX(rawX);
7879 float y = toDisplayY(rawY);
7880 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7881 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7882 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7883 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7884 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7885 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7886 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7887 float distance = float(rawDistance);
7888
7889 processPosition(mapper, rawX, rawY);
7890 processTouchMajor(mapper, rawTouchMajor);
7891 processTouchMinor(mapper, rawTouchMinor);
7892 processToolMajor(mapper, rawToolMajor);
7893 processToolMinor(mapper, rawToolMinor);
7894 processPressure(mapper, rawPressure);
7895 processOrientation(mapper, rawOrientation);
7896 processDistance(mapper, rawDistance);
7897 processId(mapper, id);
7898 processMTSync(mapper);
7899 processSync(mapper);
7900
7901 NotifyMotionArgs args;
7902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7903 ASSERT_EQ(0, args.pointerProperties[0].id);
7904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7905 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7906 orientation, distance));
7907}
7908
7909TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 addConfigurationProperty("touch.deviceType", "touchScreen");
7911 prepareDisplay(DISPLAY_ORIENTATION_0);
7912 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7913 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007914 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915
7916 // These calculations are based on the input device calibration documentation.
7917 int32_t rawX = 100;
7918 int32_t rawY = 200;
7919 int32_t rawTouchMajor = 140;
7920 int32_t rawTouchMinor = 120;
7921 int32_t rawToolMajor = 180;
7922 int32_t rawToolMinor = 160;
7923
7924 float x = toDisplayX(rawX);
7925 float y = toDisplayY(rawY);
7926 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7927 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7928 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7929 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7930 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7931
7932 processPosition(mapper, rawX, rawY);
7933 processTouchMajor(mapper, rawTouchMajor);
7934 processTouchMinor(mapper, rawTouchMinor);
7935 processToolMajor(mapper, rawToolMajor);
7936 processToolMinor(mapper, rawToolMinor);
7937 processMTSync(mapper);
7938 processSync(mapper);
7939
7940 NotifyMotionArgs args;
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7943 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7944}
7945
7946TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007947 addConfigurationProperty("touch.deviceType", "touchScreen");
7948 prepareDisplay(DISPLAY_ORIENTATION_0);
7949 prepareAxes(POSITION | TOUCH | TOOL);
7950 addConfigurationProperty("touch.size.calibration", "diameter");
7951 addConfigurationProperty("touch.size.scale", "10");
7952 addConfigurationProperty("touch.size.bias", "160");
7953 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007954 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007955
7956 // These calculations are based on the input device calibration documentation.
7957 // Note: We only provide a single common touch/tool value because the device is assumed
7958 // not to emit separate values for each pointer (isSummed = 1).
7959 int32_t rawX = 100;
7960 int32_t rawY = 200;
7961 int32_t rawX2 = 150;
7962 int32_t rawY2 = 250;
7963 int32_t rawTouchMajor = 5;
7964 int32_t rawToolMajor = 8;
7965
7966 float x = toDisplayX(rawX);
7967 float y = toDisplayY(rawY);
7968 float x2 = toDisplayX(rawX2);
7969 float y2 = toDisplayY(rawY2);
7970 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7971 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7972 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7973
7974 processPosition(mapper, rawX, rawY);
7975 processTouchMajor(mapper, rawTouchMajor);
7976 processToolMajor(mapper, rawToolMajor);
7977 processMTSync(mapper);
7978 processPosition(mapper, rawX2, rawY2);
7979 processTouchMajor(mapper, rawTouchMajor);
7980 processToolMajor(mapper, rawToolMajor);
7981 processMTSync(mapper);
7982 processSync(mapper);
7983
7984 NotifyMotionArgs args;
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7986 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7987
7988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007989 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990 ASSERT_EQ(size_t(2), args.pointerCount);
7991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7992 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7994 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7995}
7996
7997TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998 addConfigurationProperty("touch.deviceType", "touchScreen");
7999 prepareDisplay(DISPLAY_ORIENTATION_0);
8000 prepareAxes(POSITION | TOUCH | TOOL);
8001 addConfigurationProperty("touch.size.calibration", "area");
8002 addConfigurationProperty("touch.size.scale", "43");
8003 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008004 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008005
8006 // These calculations are based on the input device calibration documentation.
8007 int32_t rawX = 100;
8008 int32_t rawY = 200;
8009 int32_t rawTouchMajor = 5;
8010 int32_t rawToolMajor = 8;
8011
8012 float x = toDisplayX(rawX);
8013 float y = toDisplayY(rawY);
8014 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8015 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8016 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8017
8018 processPosition(mapper, rawX, rawY);
8019 processTouchMajor(mapper, rawTouchMajor);
8020 processToolMajor(mapper, rawToolMajor);
8021 processMTSync(mapper);
8022 processSync(mapper);
8023
8024 NotifyMotionArgs args;
8025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8027 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8028}
8029
8030TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031 addConfigurationProperty("touch.deviceType", "touchScreen");
8032 prepareDisplay(DISPLAY_ORIENTATION_0);
8033 prepareAxes(POSITION | PRESSURE);
8034 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8035 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008036 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008037
Michael Wrightaa449c92017-12-13 21:21:43 +00008038 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008039 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008040 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8041 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8042 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8043
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044 // These calculations are based on the input device calibration documentation.
8045 int32_t rawX = 100;
8046 int32_t rawY = 200;
8047 int32_t rawPressure = 60;
8048
8049 float x = toDisplayX(rawX);
8050 float y = toDisplayY(rawY);
8051 float pressure = float(rawPressure) * 0.01f;
8052
8053 processPosition(mapper, rawX, rawY);
8054 processPressure(mapper, rawPressure);
8055 processMTSync(mapper);
8056 processSync(mapper);
8057
8058 NotifyMotionArgs args;
8059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8061 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8062}
8063
8064TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065 addConfigurationProperty("touch.deviceType", "touchScreen");
8066 prepareDisplay(DISPLAY_ORIENTATION_0);
8067 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008068 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069
8070 NotifyMotionArgs motionArgs;
8071 NotifyKeyArgs keyArgs;
8072
8073 processId(mapper, 1);
8074 processPosition(mapper, 100, 200);
8075 processSync(mapper);
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8077 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8078 ASSERT_EQ(0, motionArgs.buttonState);
8079
8080 // press BTN_LEFT, release BTN_LEFT
8081 processKey(mapper, BTN_LEFT, 1);
8082 processSync(mapper);
8083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8085 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8086
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8089 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8090
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091 processKey(mapper, BTN_LEFT, 0);
8092 processSync(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008099 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100
8101 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8102 processKey(mapper, BTN_RIGHT, 1);
8103 processKey(mapper, BTN_MIDDLE, 1);
8104 processSync(mapper);
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8107 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8108 motionArgs.buttonState);
8109
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8112 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8113
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8116 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8117 motionArgs.buttonState);
8118
Michael Wrightd02c5b62014-02-10 15:10:22 -08008119 processKey(mapper, BTN_RIGHT, 0);
8120 processSync(mapper);
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008124
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008127 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128
8129 processKey(mapper, BTN_MIDDLE, 0);
8130 processSync(mapper);
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008133 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008134
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138
8139 // press BTN_BACK, release BTN_BACK
8140 processKey(mapper, BTN_BACK, 1);
8141 processSync(mapper);
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8143 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8144 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008145
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008148 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8149
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8151 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8152 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153
8154 processKey(mapper, BTN_BACK, 0);
8155 processSync(mapper);
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162 ASSERT_EQ(0, motionArgs.buttonState);
8163
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8165 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8166 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8167
8168 // press BTN_SIDE, release BTN_SIDE
8169 processKey(mapper, BTN_SIDE, 1);
8170 processSync(mapper);
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8172 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8173 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008174
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008177 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8178
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8180 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8181 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182
8183 processKey(mapper, BTN_SIDE, 0);
8184 processSync(mapper);
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008188
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191 ASSERT_EQ(0, motionArgs.buttonState);
8192
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8194 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8195 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8196
8197 // press BTN_FORWARD, release BTN_FORWARD
8198 processKey(mapper, BTN_FORWARD, 1);
8199 processSync(mapper);
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8201 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8202 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008203
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008206 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8207
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8209 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8210 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211
8212 processKey(mapper, BTN_FORWARD, 0);
8213 processSync(mapper);
8214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008216 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008217
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220 ASSERT_EQ(0, motionArgs.buttonState);
8221
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8223 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8224 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8225
8226 // press BTN_EXTRA, release BTN_EXTRA
8227 processKey(mapper, BTN_EXTRA, 1);
8228 processSync(mapper);
8229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8230 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8231 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008232
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008235 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8236
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8239 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240
8241 processKey(mapper, BTN_EXTRA, 0);
8242 processSync(mapper);
8243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008246
8247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008249 ASSERT_EQ(0, motionArgs.buttonState);
8250
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8252 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8253 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8254
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8256
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257 // press BTN_STYLUS, release BTN_STYLUS
8258 processKey(mapper, BTN_STYLUS, 1);
8259 processSync(mapper);
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008262 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8263
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8266 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267
8268 processKey(mapper, BTN_STYLUS, 0);
8269 processSync(mapper);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008273
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008276 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008277
8278 // press BTN_STYLUS2, release BTN_STYLUS2
8279 processKey(mapper, BTN_STYLUS2, 1);
8280 processSync(mapper);
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008283 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8284
8285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8287 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008288
8289 processKey(mapper, BTN_STYLUS2, 0);
8290 processSync(mapper);
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008292 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008293 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008294
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008297 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008298
8299 // release touch
8300 processId(mapper, -1);
8301 processSync(mapper);
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8303 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8304 ASSERT_EQ(0, motionArgs.buttonState);
8305}
8306
8307TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008308 addConfigurationProperty("touch.deviceType", "touchScreen");
8309 prepareDisplay(DISPLAY_ORIENTATION_0);
8310 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008311 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312
8313 NotifyMotionArgs motionArgs;
8314
8315 // default tool type is finger
8316 processId(mapper, 1);
8317 processPosition(mapper, 100, 200);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8322
8323 // eraser
8324 processKey(mapper, BTN_TOOL_RUBBER, 1);
8325 processSync(mapper);
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8329
8330 // stylus
8331 processKey(mapper, BTN_TOOL_RUBBER, 0);
8332 processKey(mapper, BTN_TOOL_PEN, 1);
8333 processSync(mapper);
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8337
8338 // brush
8339 processKey(mapper, BTN_TOOL_PEN, 0);
8340 processKey(mapper, BTN_TOOL_BRUSH, 1);
8341 processSync(mapper);
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8345
8346 // pencil
8347 processKey(mapper, BTN_TOOL_BRUSH, 0);
8348 processKey(mapper, BTN_TOOL_PENCIL, 1);
8349 processSync(mapper);
8350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8353
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008354 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 processKey(mapper, BTN_TOOL_PENCIL, 0);
8356 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8357 processSync(mapper);
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8361
8362 // mouse
8363 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8364 processKey(mapper, BTN_TOOL_MOUSE, 1);
8365 processSync(mapper);
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8369
8370 // lens
8371 processKey(mapper, BTN_TOOL_MOUSE, 0);
8372 processKey(mapper, BTN_TOOL_LENS, 1);
8373 processSync(mapper);
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8377
8378 // double-tap
8379 processKey(mapper, BTN_TOOL_LENS, 0);
8380 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8381 processSync(mapper);
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8385
8386 // triple-tap
8387 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8388 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8389 processSync(mapper);
8390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8393
8394 // quad-tap
8395 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8396 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8397 processSync(mapper);
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8401
8402 // finger
8403 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8404 processKey(mapper, BTN_TOOL_FINGER, 1);
8405 processSync(mapper);
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8409
8410 // stylus trumps finger
8411 processKey(mapper, BTN_TOOL_PEN, 1);
8412 processSync(mapper);
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8416
8417 // eraser trumps stylus
8418 processKey(mapper, BTN_TOOL_RUBBER, 1);
8419 processSync(mapper);
8420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8423
8424 // mouse trumps eraser
8425 processKey(mapper, BTN_TOOL_MOUSE, 1);
8426 processSync(mapper);
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8430
8431 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8432 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8433 processSync(mapper);
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8437
8438 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8439 processToolType(mapper, MT_TOOL_PEN);
8440 processSync(mapper);
8441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8444
8445 // back to default tool type
8446 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8447 processKey(mapper, BTN_TOOL_MOUSE, 0);
8448 processKey(mapper, BTN_TOOL_RUBBER, 0);
8449 processKey(mapper, BTN_TOOL_PEN, 0);
8450 processKey(mapper, BTN_TOOL_FINGER, 0);
8451 processSync(mapper);
8452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8455}
8456
8457TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008458 addConfigurationProperty("touch.deviceType", "touchScreen");
8459 prepareDisplay(DISPLAY_ORIENTATION_0);
8460 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008461 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008462 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008463
8464 NotifyMotionArgs motionArgs;
8465
8466 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8467 processId(mapper, 1);
8468 processPosition(mapper, 100, 200);
8469 processSync(mapper);
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8473 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8474
8475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8476 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8478 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8479
8480 // move a little
8481 processPosition(mapper, 150, 250);
8482 processSync(mapper);
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8486 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8487
8488 // down when BTN_TOUCH is pressed, pressure defaults to 1
8489 processKey(mapper, BTN_TOUCH, 1);
8490 processSync(mapper);
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8494 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8495
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8497 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8499 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8500
8501 // up when BTN_TOUCH is released, hover restored
8502 processKey(mapper, BTN_TOUCH, 0);
8503 processSync(mapper);
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8505 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8507 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8508
8509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8512 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8513
8514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8515 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8517 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8518
8519 // exit hover when pointer goes away
8520 processId(mapper, -1);
8521 processSync(mapper);
8522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8525 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8526}
8527
8528TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008529 addConfigurationProperty("touch.deviceType", "touchScreen");
8530 prepareDisplay(DISPLAY_ORIENTATION_0);
8531 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008532 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008533
8534 NotifyMotionArgs motionArgs;
8535
8536 // initially hovering because pressure is 0
8537 processId(mapper, 1);
8538 processPosition(mapper, 100, 200);
8539 processPressure(mapper, 0);
8540 processSync(mapper);
8541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8544 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8545
8546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8547 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8549 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8550
8551 // move a little
8552 processPosition(mapper, 150, 250);
8553 processSync(mapper);
8554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8555 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8557 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8558
8559 // down when pressure becomes non-zero
8560 processPressure(mapper, RAW_PRESSURE_MAX);
8561 processSync(mapper);
8562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8563 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8565 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8566
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8568 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8570 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8571
8572 // up when pressure becomes 0, hover restored
8573 processPressure(mapper, 0);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8576 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8578 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8579
8580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8583 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8584
8585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8588 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8589
8590 // exit hover when pointer goes away
8591 processId(mapper, -1);
8592 processSync(mapper);
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8594 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8596 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8597}
8598
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008599/**
8600 * Set the input device port <--> display port associations, and check that the
8601 * events are routed to the display that matches the display port.
8602 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8603 */
8604TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008605 const std::string usb2 = "USB2";
8606 const uint8_t hdmi1 = 0;
8607 const uint8_t hdmi2 = 1;
8608 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008609 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008610
8611 addConfigurationProperty("touch.deviceType", "touchScreen");
8612 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008613 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008614
8615 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8616 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8617
8618 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8619 // for this input device is specified, and the matching viewport is not present,
8620 // the input device should be disabled (at the mapper level).
8621
8622 // Add viewport for display 2 on hdmi2
8623 prepareSecondaryDisplay(type, hdmi2);
8624 // Send a touch event
8625 processPosition(mapper, 100, 100);
8626 processSync(mapper);
8627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8628
8629 // Add viewport for display 1 on hdmi1
8630 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8631 // Send a touch event again
8632 processPosition(mapper, 100, 100);
8633 processSync(mapper);
8634
8635 NotifyMotionArgs args;
8636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8637 ASSERT_EQ(DISPLAY_ID, args.displayId);
8638}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008639
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008640TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8641 addConfigurationProperty("touch.deviceType", "touchScreen");
8642 prepareAxes(POSITION);
8643 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8644
8645 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8646
8647 prepareDisplay(DISPLAY_ORIENTATION_0);
8648 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8649
8650 // Send a touch event
8651 processPosition(mapper, 100, 100);
8652 processSync(mapper);
8653
8654 NotifyMotionArgs args;
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8656 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8657}
8658
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008659TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008660 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008661 std::shared_ptr<FakePointerController> fakePointerController =
8662 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008663 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008664 fakePointerController->setPosition(100, 200);
8665 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008666 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008667
Garfield Tan888a6a42020-01-09 11:39:16 -08008668 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008669 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008670
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008671 prepareDisplay(DISPLAY_ORIENTATION_0);
8672 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008673 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008674
8675 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008676 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008677
8678 NotifyMotionArgs motionArgs;
8679 processPosition(mapper, 100, 100);
8680 processSync(mapper);
8681
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8683 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8684 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8685}
8686
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008687/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008688 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8689 */
8690TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8691 addConfigurationProperty("touch.deviceType", "touchScreen");
8692 prepareAxes(POSITION);
8693 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8694
8695 prepareDisplay(DISPLAY_ORIENTATION_0);
8696 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8697 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8698 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8699 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8700
8701 NotifyMotionArgs args;
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8703 ASSERT_EQ(26, args.readTime);
8704
8705 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8706 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8707 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8708
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8710 ASSERT_EQ(33, args.readTime);
8711}
8712
8713/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008714 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8715 * events should not be delivered to the listener.
8716 */
8717TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8718 addConfigurationProperty("touch.deviceType", "touchScreen");
8719 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8720 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8721 ViewportType::INTERNAL);
8722 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8723 prepareAxes(POSITION);
8724 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8725
8726 NotifyMotionArgs motionArgs;
8727 processPosition(mapper, 100, 100);
8728 processSync(mapper);
8729
8730 mFakeListener->assertNotifyMotionWasNotCalled();
8731}
8732
Garfield Tanc734e4f2021-01-15 20:01:39 -08008733TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8734 addConfigurationProperty("touch.deviceType", "touchScreen");
8735 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8736 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8737 ViewportType::INTERNAL);
8738 std::optional<DisplayViewport> optionalDisplayViewport =
8739 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8740 ASSERT_TRUE(optionalDisplayViewport.has_value());
8741 DisplayViewport displayViewport = *optionalDisplayViewport;
8742
8743 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8744 prepareAxes(POSITION);
8745 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8746
8747 // Finger down
8748 int32_t x = 100, y = 100;
8749 processPosition(mapper, x, y);
8750 processSync(mapper);
8751
8752 NotifyMotionArgs motionArgs;
8753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8754 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8755
8756 // Deactivate display viewport
8757 displayViewport.isActive = false;
8758 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8759 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8760
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008761 // The ongoing touch should be canceled immediately
8762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8763 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8764
8765 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008766 x += 10, y += 10;
8767 processPosition(mapper, x, y);
8768 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008770
8771 // Reactivate display viewport
8772 displayViewport.isActive = true;
8773 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8774 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8775
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008776 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008777 x += 10, y += 10;
8778 processPosition(mapper, x, y);
8779 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8781 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008782}
8783
Arthur Hung7c645402019-01-25 17:45:42 +08008784TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8785 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008786 prepareAxes(POSITION | ID | SLOT);
8787 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008788 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008789
8790 // Create the second touch screen device, and enable multi fingers.
8791 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008792 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008793 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008794 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008795 std::shared_ptr<InputDevice> device2 =
8796 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008797 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008798
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008799 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8800 0 /*flat*/, 0 /*fuzz*/);
8801 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8802 0 /*flat*/, 0 /*fuzz*/);
8803 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8804 0 /*flat*/, 0 /*fuzz*/);
8805 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8806 0 /*flat*/, 0 /*fuzz*/);
8807 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8808 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8809 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008810
8811 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008812 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008813 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8814 device2->reset(ARBITRARY_TIME);
8815
8816 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008817 std::shared_ptr<FakePointerController> fakePointerController =
8818 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008819 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008820
8821 // Setup policy for associated displays and show touches.
8822 const uint8_t hdmi1 = 0;
8823 const uint8_t hdmi2 = 1;
8824 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8825 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8826 mFakePolicy->setShowTouches(true);
8827
8828 // Create displays.
8829 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008830 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008831
8832 // Default device will reconfigure above, need additional reconfiguration for another device.
8833 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008834 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008835
8836 // Two fingers down at default display.
8837 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8838 processPosition(mapper, x1, y1);
8839 processId(mapper, 1);
8840 processSlot(mapper, 1);
8841 processPosition(mapper, x2, y2);
8842 processId(mapper, 2);
8843 processSync(mapper);
8844
8845 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8846 fakePointerController->getSpots().find(DISPLAY_ID);
8847 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8848 ASSERT_EQ(size_t(2), iter->second.size());
8849
8850 // Two fingers down at second display.
8851 processPosition(mapper2, x1, y1);
8852 processId(mapper2, 1);
8853 processSlot(mapper2, 1);
8854 processPosition(mapper2, x2, y2);
8855 processId(mapper2, 2);
8856 processSync(mapper2);
8857
8858 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8859 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8860 ASSERT_EQ(size_t(2), iter->second.size());
8861}
8862
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008863TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008864 prepareAxes(POSITION);
8865 addConfigurationProperty("touch.deviceType", "touchScreen");
8866 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008867 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008868
8869 NotifyMotionArgs motionArgs;
8870 // Unrotated video frame
8871 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8872 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008873 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008874 processPosition(mapper, 100, 200);
8875 processSync(mapper);
8876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8877 ASSERT_EQ(frames, motionArgs.videoFrames);
8878
8879 // Subsequent touch events should not have any videoframes
8880 // This is implemented separately in FakeEventHub,
8881 // but that should match the behaviour of TouchVideoDevice.
8882 processPosition(mapper, 200, 200);
8883 processSync(mapper);
8884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8885 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8886}
8887
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008888TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008889 prepareAxes(POSITION);
8890 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008891 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008892 // Unrotated video frame
8893 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8894 NotifyMotionArgs motionArgs;
8895
8896 // Test all 4 orientations
8897 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008898 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8899 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8900 clearViewports();
8901 prepareDisplay(orientation);
8902 std::vector<TouchVideoFrame> frames{frame};
8903 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8904 processPosition(mapper, 100, 200);
8905 processSync(mapper);
8906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8907 ASSERT_EQ(frames, motionArgs.videoFrames);
8908 }
8909}
8910
8911TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8912 prepareAxes(POSITION);
8913 addConfigurationProperty("touch.deviceType", "touchScreen");
8914 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8915 // orientation-aware are affected by display rotation.
8916 addConfigurationProperty("touch.orientationAware", "0");
8917 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8918 // Unrotated video frame
8919 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8920 NotifyMotionArgs motionArgs;
8921
8922 // Test all 4 orientations
8923 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008924 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8925 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8926 clearViewports();
8927 prepareDisplay(orientation);
8928 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008929 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008930 processPosition(mapper, 100, 200);
8931 processSync(mapper);
8932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008933 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8934 // compared to the display. This is so that when the window transform (which contains the
8935 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8936 // window's coordinate space.
8937 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008938 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnane74b35f2022-07-19 16:00:50 +08008939
8940 // Release finger.
8941 processSync(mapper);
8942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008943 }
8944}
8945
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008946TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008947 prepareAxes(POSITION);
8948 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008949 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008950 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8951 // so mix these.
8952 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8953 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8954 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8955 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8956 NotifyMotionArgs motionArgs;
8957
8958 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008959 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008960 processPosition(mapper, 100, 200);
8961 processSync(mapper);
8962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008963 ASSERT_EQ(frames, motionArgs.videoFrames);
8964}
8965
8966TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8967 prepareAxes(POSITION);
8968 addConfigurationProperty("touch.deviceType", "touchScreen");
8969 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8970 // orientation-aware are affected by display rotation.
8971 addConfigurationProperty("touch.orientationAware", "0");
8972 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8973 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8974 // so mix these.
8975 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8976 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8977 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8978 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8979 NotifyMotionArgs motionArgs;
8980
8981 prepareDisplay(DISPLAY_ORIENTATION_90);
8982 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8983 processPosition(mapper, 100, 200);
8984 processSync(mapper);
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8986 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8987 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8988 // compared to the display. This is so that when the window transform (which contains the
8989 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8990 // window's coordinate space.
8991 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8992 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008993 ASSERT_EQ(frames, motionArgs.videoFrames);
8994}
8995
Arthur Hung9da14732019-09-02 16:16:58 +08008996/**
8997 * If we had defined port associations, but the viewport is not ready, the touch device would be
8998 * expected to be disabled, and it should be enabled after the viewport has found.
8999 */
9000TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009001 constexpr uint8_t hdmi2 = 1;
9002 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009003 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009004
9005 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9006
9007 addConfigurationProperty("touch.deviceType", "touchScreen");
9008 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009009 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009010
9011 ASSERT_EQ(mDevice->isEnabled(), false);
9012
9013 // Add display on hdmi2, the device should be enabled and can receive touch event.
9014 prepareSecondaryDisplay(type, hdmi2);
9015 ASSERT_EQ(mDevice->isEnabled(), true);
9016
9017 // Send a touch event.
9018 processPosition(mapper, 100, 100);
9019 processSync(mapper);
9020
9021 NotifyMotionArgs args;
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9023 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9024}
9025
Arthur Hung421eb1c2020-01-16 00:09:42 +08009026TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009027 addConfigurationProperty("touch.deviceType", "touchScreen");
9028 prepareDisplay(DISPLAY_ORIENTATION_0);
9029 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009030 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009031
9032 NotifyMotionArgs motionArgs;
9033
9034 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9035 // finger down
9036 processId(mapper, 1);
9037 processPosition(mapper, x1, y1);
9038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9040 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9042
9043 // finger move
9044 processId(mapper, 1);
9045 processPosition(mapper, x2, y2);
9046 processSync(mapper);
9047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9050
9051 // finger up.
9052 processId(mapper, -1);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9057
9058 // new finger down
9059 processId(mapper, 1);
9060 processPosition(mapper, x3, y3);
9061 processSync(mapper);
9062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9063 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9065}
9066
9067/**
arthurhungcc7f9802020-04-30 17:55:40 +08009068 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9069 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009070 */
arthurhungcc7f9802020-04-30 17:55:40 +08009071TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009072 addConfigurationProperty("touch.deviceType", "touchScreen");
9073 prepareDisplay(DISPLAY_ORIENTATION_0);
9074 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009075 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009076
9077 NotifyMotionArgs motionArgs;
9078
9079 // default tool type is finger
9080 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009081 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009082 processPosition(mapper, x1, y1);
9083 processSync(mapper);
9084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9085 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9087
9088 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9089 processToolType(mapper, MT_TOOL_PALM);
9090 processSync(mapper);
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9092 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9093
9094 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009095 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009096 processPosition(mapper, x2, y2);
9097 processSync(mapper);
9098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9099
9100 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009101 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009102 processSync(mapper);
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9104
9105 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009106 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009107 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009108 processPosition(mapper, x3, y3);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9111 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9113}
9114
arthurhungbf89a482020-04-17 17:37:55 +08009115/**
arthurhungcc7f9802020-04-30 17:55:40 +08009116 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9117 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009118 */
arthurhungcc7f9802020-04-30 17:55:40 +08009119TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009120 addConfigurationProperty("touch.deviceType", "touchScreen");
9121 prepareDisplay(DISPLAY_ORIENTATION_0);
9122 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9123 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9124
9125 NotifyMotionArgs motionArgs;
9126
9127 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009128 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9129 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009130 processPosition(mapper, x1, y1);
9131 processSync(mapper);
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9135
9136 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009137 processSlot(mapper, SECOND_SLOT);
9138 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009139 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009140 processSync(mapper);
9141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009142 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9144
9145 // If the tool type of the first finger changes to MT_TOOL_PALM,
9146 // we expect to receive ACTION_POINTER_UP with cancel flag.
9147 processSlot(mapper, FIRST_SLOT);
9148 processId(mapper, FIRST_TRACKING_ID);
9149 processToolType(mapper, MT_TOOL_PALM);
9150 processSync(mapper);
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009152 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009153 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9154
9155 // The following MOVE events of second finger should be processed.
9156 processSlot(mapper, SECOND_SLOT);
9157 processId(mapper, SECOND_TRACKING_ID);
9158 processPosition(mapper, x2 + 1, y2 + 1);
9159 processSync(mapper);
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9162 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9163
9164 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9165 // it. Second finger receive move.
9166 processSlot(mapper, FIRST_SLOT);
9167 processId(mapper, INVALID_TRACKING_ID);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9171 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9172
9173 // Second finger keeps moving.
9174 processSlot(mapper, SECOND_SLOT);
9175 processId(mapper, SECOND_TRACKING_ID);
9176 processPosition(mapper, x2 + 2, y2 + 2);
9177 processSync(mapper);
9178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9180 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9181
9182 // Second finger up.
9183 processId(mapper, INVALID_TRACKING_ID);
9184 processSync(mapper);
9185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9187 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9188}
9189
9190/**
9191 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9192 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9193 */
9194TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9195 addConfigurationProperty("touch.deviceType", "touchScreen");
9196 prepareDisplay(DISPLAY_ORIENTATION_0);
9197 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9198 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9199
9200 NotifyMotionArgs motionArgs;
9201
9202 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9203 // First finger down.
9204 processId(mapper, FIRST_TRACKING_ID);
9205 processPosition(mapper, x1, y1);
9206 processSync(mapper);
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9210
9211 // Second finger down.
9212 processSlot(mapper, SECOND_SLOT);
9213 processId(mapper, SECOND_TRACKING_ID);
9214 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009215 processSync(mapper);
9216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009217 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9219
arthurhungcc7f9802020-04-30 17:55:40 +08009220 // If the tool type of the first finger changes to MT_TOOL_PALM,
9221 // we expect to receive ACTION_POINTER_UP with cancel flag.
9222 processSlot(mapper, FIRST_SLOT);
9223 processId(mapper, FIRST_TRACKING_ID);
9224 processToolType(mapper, MT_TOOL_PALM);
9225 processSync(mapper);
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009227 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009228 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9229
9230 // Second finger keeps moving.
9231 processSlot(mapper, SECOND_SLOT);
9232 processId(mapper, SECOND_TRACKING_ID);
9233 processPosition(mapper, x2 + 1, y2 + 1);
9234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9237
9238 // second finger becomes palm, receive cancel due to only 1 finger is active.
9239 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009240 processToolType(mapper, MT_TOOL_PALM);
9241 processSync(mapper);
9242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9243 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9244
arthurhungcc7f9802020-04-30 17:55:40 +08009245 // third finger down.
9246 processSlot(mapper, THIRD_SLOT);
9247 processId(mapper, THIRD_TRACKING_ID);
9248 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009249 processPosition(mapper, x3, y3);
9250 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9252 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009254 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9255
9256 // third finger move
9257 processId(mapper, THIRD_TRACKING_ID);
9258 processPosition(mapper, x3 + 1, y3 + 1);
9259 processSync(mapper);
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9262
9263 // first finger up, third finger receive move.
9264 processSlot(mapper, FIRST_SLOT);
9265 processId(mapper, INVALID_TRACKING_ID);
9266 processSync(mapper);
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9269 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9270
9271 // second finger up, third finger receive move.
9272 processSlot(mapper, SECOND_SLOT);
9273 processId(mapper, INVALID_TRACKING_ID);
9274 processSync(mapper);
9275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9277 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9278
9279 // third finger up.
9280 processSlot(mapper, THIRD_SLOT);
9281 processId(mapper, INVALID_TRACKING_ID);
9282 processSync(mapper);
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9284 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9285 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9286}
9287
9288/**
9289 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9290 * and the active finger could still be allowed to receive the events
9291 */
9292TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9293 addConfigurationProperty("touch.deviceType", "touchScreen");
9294 prepareDisplay(DISPLAY_ORIENTATION_0);
9295 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9296 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9297
9298 NotifyMotionArgs motionArgs;
9299
9300 // default tool type is finger
9301 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9302 processId(mapper, FIRST_TRACKING_ID);
9303 processPosition(mapper, x1, y1);
9304 processSync(mapper);
9305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9306 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9308
9309 // Second finger down.
9310 processSlot(mapper, SECOND_SLOT);
9311 processId(mapper, SECOND_TRACKING_ID);
9312 processPosition(mapper, x2, y2);
9313 processSync(mapper);
9314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009315 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9317
9318 // If the tool type of the second finger changes to MT_TOOL_PALM,
9319 // we expect to receive ACTION_POINTER_UP with cancel flag.
9320 processId(mapper, SECOND_TRACKING_ID);
9321 processToolType(mapper, MT_TOOL_PALM);
9322 processSync(mapper);
9323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009324 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009325 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9326
9327 // The following MOVE event should be processed.
9328 processSlot(mapper, FIRST_SLOT);
9329 processId(mapper, FIRST_TRACKING_ID);
9330 processPosition(mapper, x1 + 1, y1 + 1);
9331 processSync(mapper);
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9334 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9335
9336 // second finger up.
9337 processSlot(mapper, SECOND_SLOT);
9338 processId(mapper, INVALID_TRACKING_ID);
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9342
9343 // first finger keep moving
9344 processSlot(mapper, FIRST_SLOT);
9345 processId(mapper, FIRST_TRACKING_ID);
9346 processPosition(mapper, x1 + 2, y1 + 2);
9347 processSync(mapper);
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9350
9351 // first finger up.
9352 processId(mapper, INVALID_TRACKING_ID);
9353 processSync(mapper);
9354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9355 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9356 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009357}
9358
Arthur Hung9ad18942021-06-19 02:04:46 +00009359/**
9360 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9361 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9362 * cause slot be valid again.
9363 */
9364TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9365 addConfigurationProperty("touch.deviceType", "touchScreen");
9366 prepareDisplay(DISPLAY_ORIENTATION_0);
9367 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9368 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9369
9370 NotifyMotionArgs motionArgs;
9371
9372 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9373 // First finger down.
9374 processId(mapper, FIRST_TRACKING_ID);
9375 processPosition(mapper, x1, y1);
9376 processPressure(mapper, RAW_PRESSURE_MAX);
9377 processSync(mapper);
9378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9379 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9380 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9381
9382 // First finger move.
9383 processId(mapper, FIRST_TRACKING_ID);
9384 processPosition(mapper, x1 + 1, y1 + 1);
9385 processPressure(mapper, RAW_PRESSURE_MAX);
9386 processSync(mapper);
9387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9389 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9390
9391 // Second finger down.
9392 processSlot(mapper, SECOND_SLOT);
9393 processId(mapper, SECOND_TRACKING_ID);
9394 processPosition(mapper, x2, y2);
9395 processPressure(mapper, RAW_PRESSURE_MAX);
9396 processSync(mapper);
9397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009398 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009399 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9400
9401 // second finger up with some unexpected data.
9402 processSlot(mapper, SECOND_SLOT);
9403 processId(mapper, INVALID_TRACKING_ID);
9404 processPosition(mapper, x2, y2);
9405 processSync(mapper);
9406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009407 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009408 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9409
9410 // first finger up with some unexpected data.
9411 processSlot(mapper, FIRST_SLOT);
9412 processId(mapper, INVALID_TRACKING_ID);
9413 processPosition(mapper, x2, y2);
9414 processPressure(mapper, RAW_PRESSURE_MAX);
9415 processSync(mapper);
9416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9417 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9418 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9419}
9420
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00009421TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9422 addConfigurationProperty("touch.deviceType", "touchScreen");
9423 prepareDisplay(DISPLAY_ORIENTATION_0);
9424 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9425 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9426
9427 NotifyMotionArgs motionArgs;
9428
9429 // First finger down.
9430 processId(mapper, FIRST_TRACKING_ID);
9431 processPosition(mapper, 100, 200);
9432 processPressure(mapper, RAW_PRESSURE_MAX);
9433 processSync(mapper);
9434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9436
9437 // Second finger down.
9438 processSlot(mapper, SECOND_SLOT);
9439 processId(mapper, SECOND_TRACKING_ID);
9440 processPosition(mapper, 300, 400);
9441 processPressure(mapper, RAW_PRESSURE_MAX);
9442 processSync(mapper);
9443 ASSERT_NO_FATAL_FAILURE(
9444 mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9445 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
9446
9447 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
9448 // preserved. Resetting should not generate any events.
9449 mapper.reset(ARBITRARY_TIME);
9450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9451
9452 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9453 // the existing touch state to generate a down event.
9454 processPosition(mapper, 301, 302);
9455 processSync(mapper);
9456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9457 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9459 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
9460
9461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9462}
9463
9464TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9465 addConfigurationProperty("touch.deviceType", "touchScreen");
9466 prepareDisplay(DISPLAY_ORIENTATION_0);
9467 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9468 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9469
9470 NotifyMotionArgs motionArgs;
9471
9472 // First finger touches down and releases.
9473 processId(mapper, FIRST_TRACKING_ID);
9474 processPosition(mapper, 100, 200);
9475 processPressure(mapper, RAW_PRESSURE_MAX);
9476 processSync(mapper);
9477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9479 processId(mapper, INVALID_TRACKING_ID);
9480 processSync(mapper);
9481 ASSERT_NO_FATAL_FAILURE(
9482 mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9483 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9484
9485 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9486 // raw state where no pointers are down.
9487 mapper.reset(ARBITRARY_TIME);
9488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9489
9490 // Send an empty sync frame. Since there are no pointers, no events are generated.
9491 processSync(mapper);
9492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9493}
9494
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009495// --- MultiTouchInputMapperTest_ExternalDevice ---
9496
9497class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9498protected:
Chris Yea52ade12020-08-27 16:49:20 -07009499 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009500};
9501
9502/**
9503 * Expect fallback to internal viewport if device is external and external viewport is not present.
9504 */
9505TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9506 prepareAxes(POSITION);
9507 addConfigurationProperty("touch.deviceType", "touchScreen");
9508 prepareDisplay(DISPLAY_ORIENTATION_0);
9509 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9510
9511 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9512
9513 NotifyMotionArgs motionArgs;
9514
9515 // Expect the event to be sent to the internal viewport,
9516 // because an external viewport is not present.
9517 processPosition(mapper, 100, 100);
9518 processSync(mapper);
9519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9520 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9521
9522 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009523 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009524 processPosition(mapper, 100, 100);
9525 processSync(mapper);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9527 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9528}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009529
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009530TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9531 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9532 std::shared_ptr<FakePointerController> fakePointerController =
9533 std::make_shared<FakePointerController>();
9534 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9535 fakePointerController->setPosition(0, 0);
9536 fakePointerController->setButtonState(0);
9537
9538 // prepare device and capture
9539 prepareDisplay(DISPLAY_ORIENTATION_0);
9540 prepareAxes(POSITION | ID | SLOT);
9541 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9542 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9543 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009544 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009545 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9546
9547 // captured touchpad should be a touchpad source
9548 NotifyDeviceResetArgs resetArgs;
9549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9550 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9551
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009552 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009553
9554 const InputDeviceInfo::MotionRange* relRangeX =
9555 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9556 ASSERT_NE(relRangeX, nullptr);
9557 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9558 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9559 const InputDeviceInfo::MotionRange* relRangeY =
9560 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9561 ASSERT_NE(relRangeY, nullptr);
9562 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9563 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9564
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009565 // run captured pointer tests - note that this is unscaled, so input listener events should be
9566 // identical to what the hardware sends (accounting for any
9567 // calibration).
9568 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009569 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009570 processId(mapper, 1);
9571 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9572 processKey(mapper, BTN_TOUCH, 1);
9573 processSync(mapper);
9574
9575 // expect coord[0] to contain initial location of touch 0
9576 NotifyMotionArgs args;
9577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9578 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9579 ASSERT_EQ(1U, args.pointerCount);
9580 ASSERT_EQ(0, args.pointerProperties[0].id);
9581 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9582 ASSERT_NO_FATAL_FAILURE(
9583 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9584
9585 // FINGER 1 DOWN
9586 processSlot(mapper, 1);
9587 processId(mapper, 2);
9588 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9589 processSync(mapper);
9590
9591 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009593 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009594 ASSERT_EQ(2U, args.pointerCount);
9595 ASSERT_EQ(0, args.pointerProperties[0].id);
9596 ASSERT_EQ(1, args.pointerProperties[1].id);
9597 ASSERT_NO_FATAL_FAILURE(
9598 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9599 ASSERT_NO_FATAL_FAILURE(
9600 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9601
9602 // FINGER 1 MOVE
9603 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9604 processSync(mapper);
9605
9606 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9607 // from move
9608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9610 ASSERT_NO_FATAL_FAILURE(
9611 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9612 ASSERT_NO_FATAL_FAILURE(
9613 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9614
9615 // FINGER 0 MOVE
9616 processSlot(mapper, 0);
9617 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9618 processSync(mapper);
9619
9620 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9623 ASSERT_NO_FATAL_FAILURE(
9624 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9625 ASSERT_NO_FATAL_FAILURE(
9626 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9627
9628 // BUTTON DOWN
9629 processKey(mapper, BTN_LEFT, 1);
9630 processSync(mapper);
9631
9632 // touchinputmapper design sends a move before button press
9633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9637
9638 // BUTTON UP
9639 processKey(mapper, BTN_LEFT, 0);
9640 processSync(mapper);
9641
9642 // touchinputmapper design sends a move after button release
9643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9647
9648 // FINGER 0 UP
9649 processId(mapper, -1);
9650 processSync(mapper);
9651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9652 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9653
9654 // FINGER 1 MOVE
9655 processSlot(mapper, 1);
9656 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9657 processSync(mapper);
9658
9659 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9662 ASSERT_EQ(1U, args.pointerCount);
9663 ASSERT_EQ(1, args.pointerProperties[0].id);
9664 ASSERT_NO_FATAL_FAILURE(
9665 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9666
9667 // FINGER 1 UP
9668 processId(mapper, -1);
9669 processKey(mapper, BTN_TOUCH, 0);
9670 processSync(mapper);
9671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9672 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9673
9674 // non captured touchpad should be a mouse source
9675 mFakePolicy->setPointerCapture(false);
9676 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9678 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9679}
9680
9681TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9682 std::shared_ptr<FakePointerController> fakePointerController =
9683 std::make_shared<FakePointerController>();
9684 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9685 fakePointerController->setPosition(0, 0);
9686 fakePointerController->setButtonState(0);
9687
9688 // prepare device and capture
9689 prepareDisplay(DISPLAY_ORIENTATION_0);
9690 prepareAxes(POSITION | ID | SLOT);
9691 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9692 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009693 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009694 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9695 // run uncaptured pointer tests - pushes out generic events
9696 // FINGER 0 DOWN
9697 processId(mapper, 3);
9698 processPosition(mapper, 100, 100);
9699 processKey(mapper, BTN_TOUCH, 1);
9700 processSync(mapper);
9701
9702 // start at (100,100), cursor should be at (0,0) * scale
9703 NotifyMotionArgs args;
9704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9706 ASSERT_NO_FATAL_FAILURE(
9707 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9708
9709 // FINGER 0 MOVE
9710 processPosition(mapper, 200, 200);
9711 processSync(mapper);
9712
9713 // compute scaling to help with touch position checking
9714 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9715 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9716 float scale =
9717 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9718
9719 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9721 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9723 0, 0, 0, 0, 0, 0, 0));
9724}
9725
9726TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9727 std::shared_ptr<FakePointerController> fakePointerController =
9728 std::make_shared<FakePointerController>();
9729
9730 prepareDisplay(DISPLAY_ORIENTATION_0);
9731 prepareAxes(POSITION | ID | SLOT);
9732 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009733 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009734 mFakePolicy->setPointerCapture(false);
9735 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9736
9737 // uncaptured touchpad should be a pointer device
9738 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9739
9740 // captured touchpad should be a touchpad device
9741 mFakePolicy->setPointerCapture(true);
9742 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9743 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9744}
9745
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009746// --- JoystickInputMapperTest ---
9747
9748class JoystickInputMapperTest : public InputMapperTest {
9749protected:
9750 static const int32_t RAW_X_MIN;
9751 static const int32_t RAW_X_MAX;
9752 static const int32_t RAW_Y_MIN;
9753 static const int32_t RAW_Y_MAX;
9754
9755 void SetUp() override {
9756 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9757 }
9758 void prepareAxes() {
9759 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9760 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9761 }
9762
9763 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9765 }
9766
9767 void processSync(JoystickInputMapper& mapper) {
9768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9769 }
9770
9771 void prepareVirtualDisplay(int32_t orientation) {
9772 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9773 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9774 NO_PORT, ViewportType::VIRTUAL);
9775 }
9776};
9777
9778const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9779const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9780const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9781const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9782
9783TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9784 prepareAxes();
9785 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9786
9787 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9788
9789 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9790
9791 // Send an axis event
9792 processAxis(mapper, ABS_X, 100);
9793 processSync(mapper);
9794
9795 NotifyMotionArgs args;
9796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9797 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9798
9799 // Send another axis event
9800 processAxis(mapper, ABS_Y, 100);
9801 processSync(mapper);
9802
9803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9804 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9805}
9806
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009807// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009808
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009809class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009810protected:
9811 static const char* DEVICE_NAME;
9812 static const char* DEVICE_LOCATION;
9813 static const int32_t DEVICE_ID;
9814 static const int32_t DEVICE_GENERATION;
9815 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009816 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009817 static const int32_t EVENTHUB_ID;
9818
9819 std::shared_ptr<FakeEventHub> mFakeEventHub;
9820 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009821 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009822 std::unique_ptr<InstrumentedInputReader> mReader;
9823 std::shared_ptr<InputDevice> mDevice;
9824
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009825 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009826 mFakeEventHub = std::make_unique<FakeEventHub>();
9827 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009828 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009829 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009830 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009831 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9832 }
9833
9834 void SetUp() override { SetUp(DEVICE_CLASSES); }
9835
9836 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009837 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009838 mFakePolicy.clear();
9839 }
9840
9841 void configureDevice(uint32_t changes) {
9842 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9843 mReader->requestRefreshConfiguration(changes);
9844 mReader->loopOnce();
9845 }
9846 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9847 }
9848
9849 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9850 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009851 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009852 InputDeviceIdentifier identifier;
9853 identifier.name = name;
9854 identifier.location = location;
9855 std::shared_ptr<InputDevice> device =
9856 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9857 identifier);
9858 mReader->pushNextDevice(device);
9859 mFakeEventHub->addDevice(eventHubId, name, classes);
9860 mReader->loopOnce();
9861 return device;
9862 }
9863
9864 template <class T, typename... Args>
9865 T& addControllerAndConfigure(Args... args) {
9866 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9867
9868 return controller;
9869 }
9870};
9871
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009872const char* PeripheralControllerTest::DEVICE_NAME = "device";
9873const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9874const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9875const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9876const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009877const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9878 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009879const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009880
9881// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009882class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009883protected:
9884 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009885 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009886 }
9887};
9888
9889TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009890 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009891
9892 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9893 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9894}
9895
9896TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009897 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009898
9899 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9900 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9901}
9902
9903// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009904class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009905protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009906 void SetUp() override {
9907 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9908 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009909};
9910
Chris Ye85758332021-05-16 23:05:17 -07009911TEST_F(LightControllerTest, MonoLight) {
9912 RawLightInfo infoMono = {.id = 1,
9913 .name = "Mono",
9914 .maxBrightness = 255,
9915 .flags = InputLightClass::BRIGHTNESS,
9916 .path = ""};
9917 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009918
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009919 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009920 InputDeviceInfo info;
9921 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009922 std::vector<InputDeviceLightInfo> lights = info.getLights();
9923 ASSERT_EQ(1U, lights.size());
9924 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009925
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009926 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9927 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009928}
9929
9930TEST_F(LightControllerTest, RGBLight) {
9931 RawLightInfo infoRed = {.id = 1,
9932 .name = "red",
9933 .maxBrightness = 255,
9934 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9935 .path = ""};
9936 RawLightInfo infoGreen = {.id = 2,
9937 .name = "green",
9938 .maxBrightness = 255,
9939 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9940 .path = ""};
9941 RawLightInfo infoBlue = {.id = 3,
9942 .name = "blue",
9943 .maxBrightness = 255,
9944 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9945 .path = ""};
9946 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9947 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9948 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9949
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009950 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009951 InputDeviceInfo info;
9952 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009953 std::vector<InputDeviceLightInfo> lights = info.getLights();
9954 ASSERT_EQ(1U, lights.size());
9955 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009956
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009957 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9958 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009959}
9960
9961TEST_F(LightControllerTest, MultiColorRGBLight) {
9962 RawLightInfo infoColor = {.id = 1,
9963 .name = "red",
9964 .maxBrightness = 255,
9965 .flags = InputLightClass::BRIGHTNESS |
9966 InputLightClass::MULTI_INTENSITY |
9967 InputLightClass::MULTI_INDEX,
9968 .path = ""};
9969
9970 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9971
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009972 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009973 InputDeviceInfo info;
9974 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009975 std::vector<InputDeviceLightInfo> lights = info.getLights();
9976 ASSERT_EQ(1U, lights.size());
9977 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009978
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009979 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9980 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009981}
9982
9983TEST_F(LightControllerTest, PlayerIdLight) {
9984 RawLightInfo info1 = {.id = 1,
9985 .name = "player1",
9986 .maxBrightness = 255,
9987 .flags = InputLightClass::BRIGHTNESS,
9988 .path = ""};
9989 RawLightInfo info2 = {.id = 2,
9990 .name = "player2",
9991 .maxBrightness = 255,
9992 .flags = InputLightClass::BRIGHTNESS,
9993 .path = ""};
9994 RawLightInfo info3 = {.id = 3,
9995 .name = "player3",
9996 .maxBrightness = 255,
9997 .flags = InputLightClass::BRIGHTNESS,
9998 .path = ""};
9999 RawLightInfo info4 = {.id = 4,
10000 .name = "player4",
10001 .maxBrightness = 255,
10002 .flags = InputLightClass::BRIGHTNESS,
10003 .path = ""};
10004 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10005 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10006 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10007 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10008
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010009 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010010 InputDeviceInfo info;
10011 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010012 std::vector<InputDeviceLightInfo> lights = info.getLights();
10013 ASSERT_EQ(1U, lights.size());
10014 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010015
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010016 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10017 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10018 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010019}
10020
Michael Wrightd02c5b62014-02-10 15:10:22 -080010021} // namespace android