blob: 697fb207c94fc8b17d236df5dfac9191c7276a49 [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
Prabir Pradhan197e0862022-07-01 14:28:00 +0000242 void clearSpots() override { mSpotsByDisplay.clear(); }
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
Andy Chen22c330c2022-08-29 20:07:10 -04001010 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) override {
1011 return {DEFAULT_BATTERY};
1012 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001013
1014 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
1015 return std::nullopt;
1016 }
Kim Low03ea0352020-11-06 12:45:07 -08001017
Chris Ye3fdbfef2021-01-06 18:45:18 -08001018 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1019 std::vector<int32_t> ids;
1020 for (const auto& [rawId, info] : mRawLightInfos) {
1021 ids.push_back(rawId);
1022 }
1023 return ids;
1024 }
1025
1026 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1027 auto it = mRawLightInfos.find(lightId);
1028 if (it == mRawLightInfos.end()) {
1029 return std::nullopt;
1030 }
1031 return it->second;
1032 }
1033
1034 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1035 mLightBrightness.emplace(lightId, brightness);
1036 }
1037
1038 void setLightIntensities(int32_t deviceId, int32_t lightId,
1039 std::unordered_map<LightColor, int32_t> intensities) override {
1040 mLightIntensities.emplace(lightId, intensities);
1041 };
1042
1043 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1044 auto lightIt = mLightBrightness.find(lightId);
1045 if (lightIt == mLightBrightness.end()) {
1046 return std::nullopt;
1047 }
1048 return lightIt->second;
1049 }
1050
1051 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1052 int32_t deviceId, int32_t lightId) override {
1053 auto lightIt = mLightIntensities.find(lightId);
1054 if (lightIt == mLightIntensities.end()) {
1055 return std::nullopt;
1056 }
1057 return lightIt->second;
1058 };
1059
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001060 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061 return false;
1062 }
1063
Chris Yea52ade12020-08-27 16:49:20 -07001064 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Chris Yea52ade12020-08-27 16:49:20 -07001066 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067
Chris Yea52ade12020-08-27 16:49:20 -07001068 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069
Chris Yea52ade12020-08-27 16:49:20 -07001070 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071};
1072
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073// --- FakeInputMapper ---
1074
1075class FakeInputMapper : public InputMapper {
1076 uint32_t mSources;
1077 int32_t mKeyboardType;
1078 int32_t mMetaState;
1079 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1080 KeyedVector<int32_t, int32_t> mScanCodeStates;
1081 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001082 // fake mapping which would normally come from keyCharacterMap
1083 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001084 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001086 std::mutex mLock;
1087 std::condition_variable mStateChangedCondition;
1088 bool mConfigureWasCalled GUARDED_BY(mLock);
1089 bool mResetWasCalled GUARDED_BY(mLock);
1090 bool mProcessWasCalled GUARDED_BY(mLock);
1091 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092
Arthur Hungc23540e2018-11-29 20:42:11 +08001093 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001095 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1096 : InputMapper(deviceContext),
1097 mSources(sources),
1098 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001100 mConfigureWasCalled(false),
1101 mResetWasCalled(false),
1102 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103
Chris Yea52ade12020-08-27 16:49:20 -07001104 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105
1106 void setKeyboardType(int32_t keyboardType) {
1107 mKeyboardType = keyboardType;
1108 }
1109
1110 void setMetaState(int32_t metaState) {
1111 mMetaState = metaState;
1112 }
1113
1114 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001115 std::unique_lock<std::mutex> lock(mLock);
1116 base::ScopedLockAssertion assumeLocked(mLock);
1117 const bool configureCalled =
1118 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1119 return mConfigureWasCalled;
1120 });
1121 if (!configureCalled) {
1122 FAIL() << "Expected configure() to have been called.";
1123 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001124 mConfigureWasCalled = false;
1125 }
1126
1127 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001128 std::unique_lock<std::mutex> lock(mLock);
1129 base::ScopedLockAssertion assumeLocked(mLock);
1130 const bool resetCalled =
1131 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1132 return mResetWasCalled;
1133 });
1134 if (!resetCalled) {
1135 FAIL() << "Expected reset() to have been called.";
1136 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137 mResetWasCalled = false;
1138 }
1139
Yi Kong9b14ac62018-07-17 13:48:38 -07001140 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001141 std::unique_lock<std::mutex> lock(mLock);
1142 base::ScopedLockAssertion assumeLocked(mLock);
1143 const bool processCalled =
1144 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1145 return mProcessWasCalled;
1146 });
1147 if (!processCalled) {
1148 FAIL() << "Expected process() to have been called.";
1149 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150 if (outLastEvent) {
1151 *outLastEvent = mLastEvent;
1152 }
1153 mProcessWasCalled = false;
1154 }
1155
1156 void setKeyCodeState(int32_t keyCode, int32_t state) {
1157 mKeyCodeStates.replaceValueFor(keyCode, state);
1158 }
1159
1160 void setScanCodeState(int32_t scanCode, int32_t state) {
1161 mScanCodeStates.replaceValueFor(scanCode, state);
1162 }
1163
1164 void setSwitchState(int32_t switchCode, int32_t state) {
1165 mSwitchStates.replaceValueFor(switchCode, state);
1166 }
1167
1168 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001169 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001170 }
1171
Philip Junker4af3b3d2021-12-14 10:36:55 +01001172 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1173 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1174 }
1175
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001177 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178
Chris Yea52ade12020-08-27 16:49:20 -07001179 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 InputMapper::populateDeviceInfo(deviceInfo);
1181
1182 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1183 deviceInfo->setKeyboardType(mKeyboardType);
1184 }
1185 }
1186
Chris Yea52ade12020-08-27 16:49:20 -07001187 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001188 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001190
1191 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001192 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001193 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1194 mViewport = config->getDisplayViewportByPort(*displayPort);
1195 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001196
1197 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001198 }
1199
Chris Yea52ade12020-08-27 16:49:20 -07001200 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001201 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001203 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 }
1205
Chris Yea52ade12020-08-27 16:49:20 -07001206 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001207 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 mLastEvent = *rawEvent;
1209 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001210 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 }
1212
Chris Yea52ade12020-08-27 16:49:20 -07001213 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1215 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1216 }
1217
Philip Junker4af3b3d2021-12-14 10:36:55 +01001218 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1219 auto it = mKeyCodeMapping.find(locationKeyCode);
1220 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1221 }
1222
Chris Yea52ade12020-08-27 16:49:20 -07001223 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1225 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1226 }
1227
Chris Yea52ade12020-08-27 16:49:20 -07001228 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001229 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1230 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1231 }
1232
Chris Yea52ade12020-08-27 16:49:20 -07001233 // Return true if the device has non-empty key layout.
1234 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1235 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001236 for (size_t i = 0; i < numCodes; i++) {
1237 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1238 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1239 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001240 }
1241 }
1242 }
Chris Yea52ade12020-08-27 16:49:20 -07001243 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244 return result;
1245 }
1246
1247 virtual int32_t getMetaState() {
1248 return mMetaState;
1249 }
1250
1251 virtual void fadePointer() {
1252 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001253
1254 virtual std::optional<int32_t> getAssociatedDisplay() {
1255 if (mViewport) {
1256 return std::make_optional(mViewport->displayId);
1257 }
1258 return std::nullopt;
1259 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001260};
1261
1262
1263// --- InstrumentedInputReader ---
1264
1265class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001266 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267
1268public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001269 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1270 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001271 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001272 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001274 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001276 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001278 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001279 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280 InputDeviceIdentifier identifier;
1281 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001282 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001284 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001285 }
1286
Prabir Pradhan28efc192019-11-05 01:10:04 +00001287 // Make the protected loopOnce method accessible to tests.
1288 using InputReader::loopOnce;
1289
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001291 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1292 const InputDeviceIdentifier& identifier)
1293 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001294 if (!mNextDevices.empty()) {
1295 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1296 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001297 return device;
1298 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001299 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001300 }
1301
arthurhungdcef2dc2020-08-11 14:47:50 +08001302 // --- FakeInputReaderContext ---
1303 class FakeInputReaderContext : public ContextImpl {
1304 int32_t mGlobalMetaState;
1305 bool mUpdateGlobalMetaStateWasCalled;
1306 int32_t mGeneration;
1307
1308 public:
1309 FakeInputReaderContext(InputReader* reader)
1310 : ContextImpl(reader),
1311 mGlobalMetaState(0),
1312 mUpdateGlobalMetaStateWasCalled(false),
1313 mGeneration(1) {}
1314
1315 virtual ~FakeInputReaderContext() {}
1316
1317 void assertUpdateGlobalMetaStateWasCalled() {
1318 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1319 << "Expected updateGlobalMetaState() to have been called.";
1320 mUpdateGlobalMetaStateWasCalled = false;
1321 }
1322
1323 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1324
1325 uint32_t getGeneration() { return mGeneration; }
1326
1327 void updateGlobalMetaState() override {
1328 mUpdateGlobalMetaStateWasCalled = true;
1329 ContextImpl::updateGlobalMetaState();
1330 }
1331
1332 int32_t getGlobalMetaState() override {
1333 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1334 }
1335
1336 int32_t bumpGeneration() override {
1337 mGeneration = ContextImpl::bumpGeneration();
1338 return mGeneration;
1339 }
1340 } mFakeContext;
1341
Michael Wrightd02c5b62014-02-10 15:10:22 -08001342 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001343
1344public:
1345 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001346};
1347
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348// --- InputReaderPolicyTest ---
1349class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001350protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001351 sp<FakeInputReaderPolicy> mFakePolicy;
1352
Chris Yea52ade12020-08-27 16:49:20 -07001353 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1354 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355};
1356
1357/**
1358 * Check that empty set of viewports is an acceptable configuration.
1359 * Also try to get internal viewport two different ways - by type and by uniqueId.
1360 *
1361 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1362 * Such configuration is not currently allowed.
1363 */
1364TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001365 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366
1367 // We didn't add any viewports yet, so there shouldn't be any.
1368 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001369 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 ASSERT_FALSE(internalViewport);
1371
1372 // Add an internal viewport, then clear it
1373 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001374 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001375 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001376
1377 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001378 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001380 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001381
1382 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001383 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001385 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386
1387 mFakePolicy->clearViewports();
1388 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001389 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001391 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001392 ASSERT_FALSE(internalViewport);
1393}
1394
1395TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1396 const std::string internalUniqueId = "local:0";
1397 const std::string externalUniqueId = "local:1";
1398 const std::string virtualUniqueId1 = "virtual:2";
1399 const std::string virtualUniqueId2 = "virtual:3";
1400 constexpr int32_t virtualDisplayId1 = 2;
1401 constexpr int32_t virtualDisplayId2 = 3;
1402
1403 // Add an internal viewport
1404 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001405 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1406 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 // Add an external viewport
1408 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001409 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1410 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411 // Add an virtual viewport
1412 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001413 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1414 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 // Add another virtual viewport
1416 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001417 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1418 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419
1420 // Check matching by type for internal
1421 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001422 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001423 ASSERT_TRUE(internalViewport);
1424 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1425
1426 // Check matching by type for external
1427 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001428 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001429 ASSERT_TRUE(externalViewport);
1430 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1431
1432 // Check matching by uniqueId for virtual viewport #1
1433 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001434 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001436 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001437 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1438 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1439
1440 // Check matching by uniqueId for virtual viewport #2
1441 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001442 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001444 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1446 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1447}
1448
1449
1450/**
1451 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1452 * that lookup works by checking display id.
1453 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1454 */
1455TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1456 const std::string uniqueId1 = "uniqueId1";
1457 const std::string uniqueId2 = "uniqueId2";
1458 constexpr int32_t displayId1 = 2;
1459 constexpr int32_t displayId2 = 3;
1460
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001461 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1462 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001463 for (const ViewportType& type : types) {
1464 mFakePolicy->clearViewports();
1465 // Add a viewport
1466 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001467 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1468 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001469 // Add another viewport
1470 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001471 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1472 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001473
1474 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001475 std::optional<DisplayViewport> viewport1 =
1476 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001477 ASSERT_TRUE(viewport1);
1478 ASSERT_EQ(displayId1, viewport1->displayId);
1479 ASSERT_EQ(type, viewport1->type);
1480
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001481 std::optional<DisplayViewport> viewport2 =
1482 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001483 ASSERT_TRUE(viewport2);
1484 ASSERT_EQ(displayId2, viewport2->displayId);
1485 ASSERT_EQ(type, viewport2->type);
1486
1487 // When there are multiple viewports of the same kind, and uniqueId is not specified
1488 // in the call to getDisplayViewport, then that situation is not supported.
1489 // The viewports can be stored in any order, so we cannot rely on the order, since that
1490 // is just implementation detail.
1491 // However, we can check that it still returns *a* viewport, we just cannot assert
1492 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001493 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001494 ASSERT_TRUE(someViewport);
1495 }
1496}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001498/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001499 * When we have multiple internal displays make sure we always return the default display when
1500 * querying by type.
1501 */
1502TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1503 const std::string uniqueId1 = "uniqueId1";
1504 const std::string uniqueId2 = "uniqueId2";
1505 constexpr int32_t nonDefaultDisplayId = 2;
1506 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1507 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1508
1509 // Add the default display first and ensure it gets returned.
1510 mFakePolicy->clearViewports();
1511 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001512 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001513 ViewportType::INTERNAL);
1514 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001515 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001516 ViewportType::INTERNAL);
1517
1518 std::optional<DisplayViewport> viewport =
1519 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1520 ASSERT_TRUE(viewport);
1521 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1522 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1523
1524 // Add the default display second to make sure order doesn't matter.
1525 mFakePolicy->clearViewports();
1526 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001527 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001528 ViewportType::INTERNAL);
1529 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001530 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001531 ViewportType::INTERNAL);
1532
1533 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1534 ASSERT_TRUE(viewport);
1535 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1536 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1537}
1538
1539/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001540 * Check getDisplayViewportByPort
1541 */
1542TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001543 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001544 const std::string uniqueId1 = "uniqueId1";
1545 const std::string uniqueId2 = "uniqueId2";
1546 constexpr int32_t displayId1 = 1;
1547 constexpr int32_t displayId2 = 2;
1548 const uint8_t hdmi1 = 0;
1549 const uint8_t hdmi2 = 1;
1550 const uint8_t hdmi3 = 2;
1551
1552 mFakePolicy->clearViewports();
1553 // Add a viewport that's associated with some display port that's not of interest.
1554 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001555 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1556 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001557 // Add another viewport, connected to HDMI1 port
1558 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001559 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1560 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001561
1562 // Check that correct display viewport was returned by comparing the display ports.
1563 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1564 ASSERT_TRUE(hdmi1Viewport);
1565 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1566 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1567
1568 // Check that we can still get the same viewport using the uniqueId
1569 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1570 ASSERT_TRUE(hdmi1Viewport);
1571 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1572 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1573 ASSERT_EQ(type, hdmi1Viewport->type);
1574
1575 // Check that we cannot find a port with "HDMI2", because we never added one
1576 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1577 ASSERT_FALSE(hdmi2Viewport);
1578}
1579
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580// --- InputReaderTest ---
1581
1582class InputReaderTest : public testing::Test {
1583protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001584 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001586 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001587 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588
Chris Yea52ade12020-08-27 16:49:20 -07001589 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001590 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001592 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593
Prabir Pradhan28efc192019-11-05 01:10:04 +00001594 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001595 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 }
1597
Chris Yea52ade12020-08-27 16:49:20 -07001598 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001599 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001600 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601 }
1602
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001603 void addDevice(int32_t eventHubId, const std::string& name,
1604 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606
1607 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001608 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001609 }
1610 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001611 mReader->loopOnce();
1612 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001613 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1614 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615 }
1616
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001617 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001618 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001619 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001620 }
1621
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001622 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001624 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001625 }
1626
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001628 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001629 ftl::Flags<InputDeviceClass> classes,
1630 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001631 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1633 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001634 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001635 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001636 return mapper;
1637 }
1638};
1639
Chris Ye98d3f532020-10-01 21:48:59 -07001640TEST_F(InputReaderTest, PolicyGetInputDevices) {
1641 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001642 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001643 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644
1645 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001646 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001647 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001649 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1651 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001652 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001653}
1654
Chris Yee7310032020-09-22 15:36:28 -07001655TEST_F(InputReaderTest, GetMergedInputDevices) {
1656 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1657 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1658 // Add two subdevices to device
1659 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1660 // Must add at least one mapper or the device will be ignored!
1661 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1662 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1663
1664 // Push same device instance for next device to be added, so they'll have same identifier.
1665 mReader->pushNextDevice(device);
1666 mReader->pushNextDevice(device);
1667 ASSERT_NO_FATAL_FAILURE(
1668 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1669 ASSERT_NO_FATAL_FAILURE(
1670 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1671
1672 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001673 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001674}
1675
Chris Yee14523a2020-12-19 13:46:00 -08001676TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1677 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1678 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1679 // Add two subdevices to device
1680 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1681 // Must add at least one mapper or the device will be ignored!
1682 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1683 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1684
1685 // Push same device instance for next device to be added, so they'll have same identifier.
1686 mReader->pushNextDevice(device);
1687 mReader->pushNextDevice(device);
1688 // Sensor device is initially disabled
1689 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1690 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1691 nullptr));
1692 // Device is disabled because the only sub device is a sensor device and disabled initially.
1693 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1694 ASSERT_FALSE(device->isEnabled());
1695 ASSERT_NO_FATAL_FAILURE(
1696 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1697 // The merged device is enabled if any sub device is enabled
1698 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1699 ASSERT_TRUE(device->isEnabled());
1700}
1701
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001702TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001703 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001704 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001705 constexpr int32_t eventHubId = 1;
1706 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001707 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001709 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001711
Yi Kong9b14ac62018-07-17 13:48:38 -07001712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001713
1714 NotifyDeviceResetArgs resetArgs;
1715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001716 ASSERT_EQ(deviceId, resetArgs.deviceId);
1717
1718 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001719 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001720 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001721
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001723 ASSERT_EQ(deviceId, resetArgs.deviceId);
1724 ASSERT_EQ(device->isEnabled(), false);
1725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001726 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001727 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001730 ASSERT_EQ(device->isEnabled(), false);
1731
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001732 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001733 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001735 ASSERT_EQ(deviceId, resetArgs.deviceId);
1736 ASSERT_EQ(device->isEnabled(), true);
1737}
1738
Michael Wrightd02c5b62014-02-10 15:10:22 -08001739TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001741 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001743 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001744 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001745 AINPUT_SOURCE_KEYBOARD, nullptr);
1746 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001747
1748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1749 AINPUT_SOURCE_ANY, AKEYCODE_A))
1750 << "Should return unknown when the device id is >= 0 but unknown.";
1751
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001752 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1753 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1754 << "Should return unknown when the device id is valid but the sources are not "
1755 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001756
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001757 ASSERT_EQ(AKEY_STATE_DOWN,
1758 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1759 AKEYCODE_A))
1760 << "Should return value provided by mapper when device id is valid and the device "
1761 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001762
1763 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1764 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1765 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1766
1767 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1768 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1769 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1770}
1771
Philip Junker4af3b3d2021-12-14 10:36:55 +01001772TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1773 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1774 constexpr int32_t eventHubId = 1;
1775 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1776 InputDeviceClass::KEYBOARD,
1777 AINPUT_SOURCE_KEYBOARD, nullptr);
1778 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1779
1780 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1781 << "Should return unknown when the device with the specified id is not found.";
1782
1783 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1784 << "Should return correct mapping when device id is valid and mapping exists.";
1785
1786 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1787 << "Should return the location key code when device id is valid and there's no "
1788 "mapping.";
1789}
1790
1791TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1792 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1793 constexpr int32_t eventHubId = 1;
1794 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1795 InputDeviceClass::JOYSTICK,
1796 AINPUT_SOURCE_GAMEPAD, nullptr);
1797 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1798
1799 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1800 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1801}
1802
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001805 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001809 AINPUT_SOURCE_KEYBOARD, nullptr);
1810 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811
1812 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1813 AINPUT_SOURCE_ANY, KEY_A))
1814 << "Should return unknown when the device id is >= 0 but unknown.";
1815
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1817 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1818 << "Should return unknown when the device id is valid but the sources are not "
1819 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 ASSERT_EQ(AKEY_STATE_DOWN,
1822 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1823 KEY_A))
1824 << "Should return value provided by mapper when device id is valid and the device "
1825 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1828 AINPUT_SOURCE_TRACKBALL, KEY_A))
1829 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1830
1831 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1832 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1833 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1834}
1835
1836TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001838 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001839 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001840 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001841 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001842 AINPUT_SOURCE_KEYBOARD, nullptr);
1843 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001844
1845 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1846 AINPUT_SOURCE_ANY, SW_LID))
1847 << "Should return unknown when the device id is >= 0 but unknown.";
1848
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1850 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1851 << "Should return unknown when the device id is valid but the sources are not "
1852 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001853
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 ASSERT_EQ(AKEY_STATE_DOWN,
1855 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1856 SW_LID))
1857 << "Should return value provided by mapper when device id is valid and the device "
1858 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859
1860 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1861 AINPUT_SOURCE_TRACKBALL, SW_LID))
1862 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1863
1864 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1865 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1866 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1867}
1868
1869TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001871 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001874 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001875 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001877 mapper.addSupportedKeyCode(AKEYCODE_A);
1878 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001879
1880 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1881 uint8_t flags[4] = { 0, 0, 0, 1 };
1882
1883 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1884 << "Should return false when device id is >= 0 but unknown.";
1885 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1886
1887 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001888 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1889 << "Should return false when device id is valid but the sources are not supported by "
1890 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001891 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1892
1893 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001894 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1895 keyCodes, flags))
1896 << "Should return value provided by mapper when device id is valid and the device "
1897 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001898 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1899
1900 flags[3] = 1;
1901 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1902 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1903 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1904
1905 flags[3] = 1;
1906 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1907 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1908 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1909}
1910
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001911TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001912 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001913 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914
1915 NotifyConfigurationChangedArgs args;
1916
1917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1918 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1919}
1920
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001921TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001922 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001923 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001924 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001925 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001926 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001927 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001928 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001929 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001931 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001932 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001933 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1934
1935 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001936 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001937 ASSERT_EQ(when, event.when);
1938 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001939 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001940 ASSERT_EQ(EV_KEY, event.type);
1941 ASSERT_EQ(KEY_A, event.code);
1942 ASSERT_EQ(1, event.value);
1943}
1944
Garfield Tan1c7bc862020-01-28 13:24:04 -08001945TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001946 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001947 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001948 constexpr int32_t eventHubId = 1;
1949 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001950 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001951 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001952 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001954
1955 NotifyDeviceResetArgs resetArgs;
1956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001957 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001958
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001959 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001960 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001962 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001963 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001965 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001966 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001968 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001969 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001970
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001971 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001972 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001974 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001975 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001976}
1977
Garfield Tan1c7bc862020-01-28 13:24:04 -08001978TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1979 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001980 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001981 constexpr int32_t eventHubId = 1;
1982 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1983 // Must add at least one mapper or the device will be ignored!
1984 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001985 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001986 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1987
1988 NotifyDeviceResetArgs resetArgs;
1989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1990 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1991}
1992
Arthur Hungc23540e2018-11-29 20:42:11 +08001993TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001994 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001995 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001996 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001997 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001998 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1999 FakeInputMapper& mapper =
2000 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002001 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002002
2003 const uint8_t hdmi1 = 1;
2004
2005 // Associated touch screen with second display.
2006 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2007
2008 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002009 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002010 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002011 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002012 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002013 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002014 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002015 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002016 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002017 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002018
2019 // Add the device, and make sure all of the callbacks are triggered.
2020 // The device is added after the input port associations are processed since
2021 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002022 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002025 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002026
Arthur Hung2c9a3342019-07-23 14:18:59 +08002027 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002028 ASSERT_EQ(deviceId, device->getId());
2029 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2030 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002031
2032 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002033 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002034 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002035 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002036}
2037
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002038TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2039 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002040 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002041 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2042 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2043 // Must add at least one mapper or the device will be ignored!
2044 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2045 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2046 mReader->pushNextDevice(device);
2047 mReader->pushNextDevice(device);
2048 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2049 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2050
2051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2052
2053 NotifyDeviceResetArgs resetArgs;
2054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2055 ASSERT_EQ(deviceId, resetArgs.deviceId);
2056 ASSERT_TRUE(device->isEnabled());
2057 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2058 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2059
2060 disableDevice(deviceId);
2061 mReader->loopOnce();
2062
2063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2064 ASSERT_EQ(deviceId, resetArgs.deviceId);
2065 ASSERT_FALSE(device->isEnabled());
2066 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2067 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2068
2069 enableDevice(deviceId);
2070 mReader->loopOnce();
2071
2072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2073 ASSERT_EQ(deviceId, resetArgs.deviceId);
2074 ASSERT_TRUE(device->isEnabled());
2075 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2076 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2077}
2078
2079TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2080 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002081 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002082 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2083 // Add two subdevices to device
2084 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2085 FakeInputMapper& mapperDevice1 =
2086 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2087 FakeInputMapper& mapperDevice2 =
2088 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2089 mReader->pushNextDevice(device);
2090 mReader->pushNextDevice(device);
2091 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2092 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2093
2094 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2095 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2096
2097 ASSERT_EQ(AKEY_STATE_DOWN,
2098 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2099 ASSERT_EQ(AKEY_STATE_DOWN,
2100 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2101 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2102 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2103}
2104
Prabir Pradhan7e186182020-11-10 13:56:45 -08002105TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2106 NotifyPointerCaptureChangedArgs args;
2107
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002108 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002109 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2110 mReader->loopOnce();
2111 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002112 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2113 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002114
2115 mFakePolicy->setPointerCapture(false);
2116 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2117 mReader->loopOnce();
2118 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002119 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002120
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002121 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002122 // does not change.
2123 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2124 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002125 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002126}
2127
Chris Ye87143712020-11-10 05:05:58 +00002128class FakeVibratorInputMapper : public FakeInputMapper {
2129public:
2130 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2131 : FakeInputMapper(deviceContext, sources) {}
2132
2133 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2134};
2135
2136TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2137 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002138 ftl::Flags<InputDeviceClass> deviceClass =
2139 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002140 constexpr int32_t eventHubId = 1;
2141 const char* DEVICE_LOCATION = "BLUETOOTH";
2142 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2143 FakeVibratorInputMapper& mapper =
2144 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2145 mReader->pushNextDevice(device);
2146
2147 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2148 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2149
2150 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2151 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2152}
2153
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002154// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002155
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002156class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002157public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002158 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002159
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002160 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002161
Andy Chen22c330c2022-08-29 20:07:10 -04002162 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2163
Chris Yee2b1e5c2021-03-10 22:45:12 -08002164 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2165
2166 void dump(std::string& dump) override {}
2167
2168 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2169 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002170 }
2171
Chris Yee2b1e5c2021-03-10 22:45:12 -08002172 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2173 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002174 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002175
2176 bool setLightColor(int32_t lightId, int32_t color) override {
2177 getDeviceContext().setLightBrightness(lightId, color >> 24);
2178 return true;
2179 }
2180
2181 std::optional<int32_t> getLightColor(int32_t lightId) override {
2182 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2183 if (!result.has_value()) {
2184 return std::nullopt;
2185 }
2186 return result.value() << 24;
2187 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002188
2189 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2190
2191 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2192
2193private:
2194 InputDeviceContext& mDeviceContext;
2195 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2196 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chen22c330c2022-08-29 20:07:10 -04002197 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002198};
2199
Chris Yee2b1e5c2021-03-10 22:45:12 -08002200TEST_F(InputReaderTest, BatteryGetCapacity) {
2201 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002202 ftl::Flags<InputDeviceClass> deviceClass =
2203 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002204 constexpr int32_t eventHubId = 1;
2205 const char* DEVICE_LOCATION = "BLUETOOTH";
2206 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002207 FakePeripheralController& controller =
2208 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002209 mReader->pushNextDevice(device);
2210
2211 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2212
2213 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2214 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2215}
2216
2217TEST_F(InputReaderTest, BatteryGetStatus) {
2218 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002219 ftl::Flags<InputDeviceClass> deviceClass =
2220 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002221 constexpr int32_t eventHubId = 1;
2222 const char* DEVICE_LOCATION = "BLUETOOTH";
2223 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002224 FakePeripheralController& controller =
2225 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002226 mReader->pushNextDevice(device);
2227
2228 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2229
2230 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2231 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2232}
2233
Chris Ye3fdbfef2021-01-06 18:45:18 -08002234TEST_F(InputReaderTest, LightGetColor) {
2235 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002236 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002237 constexpr int32_t eventHubId = 1;
2238 const char* DEVICE_LOCATION = "BLUETOOTH";
2239 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002240 FakePeripheralController& controller =
2241 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002242 mReader->pushNextDevice(device);
2243 RawLightInfo info = {.id = 1,
2244 .name = "Mono",
2245 .maxBrightness = 255,
2246 .flags = InputLightClass::BRIGHTNESS,
2247 .path = ""};
2248 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2249 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2250
2251 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002252
Chris Yee2b1e5c2021-03-10 22:45:12 -08002253 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2254 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002255 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2256 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2257}
2258
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002259// --- InputReaderIntegrationTest ---
2260
2261// These tests create and interact with the InputReader only through its interface.
2262// The InputReader is started during SetUp(), which starts its processing in its own
2263// thread. The tests use linux uinput to emulate input devices.
2264// NOTE: Interacting with the physical device while these tests are running may cause
2265// the tests to fail.
2266class InputReaderIntegrationTest : public testing::Test {
2267protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002268 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002269 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002270 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002271
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002272 std::shared_ptr<FakePointerController> mFakePointerController;
2273
Chris Yea52ade12020-08-27 16:49:20 -07002274 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002275 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002276 mFakePointerController = std::make_shared<FakePointerController>();
2277 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002278 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2279 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002280
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002281 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2282 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002283 ASSERT_EQ(mReader->start(), OK);
2284
2285 // Since this test is run on a real device, all the input devices connected
2286 // to the test device will show up in mReader. We wait for those input devices to
2287 // show up before beginning the tests.
2288 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2289 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2290 }
2291
Chris Yea52ade12020-08-27 16:49:20 -07002292 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002293 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002294 mReader.reset();
2295 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002296 mFakePolicy.clear();
2297 }
2298};
2299
2300TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2301 // An invalid input device that is only used for this test.
2302 class InvalidUinputDevice : public UinputDevice {
2303 public:
2304 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2305
2306 private:
2307 void configureDevice(int fd, uinput_user_dev* device) override {}
2308 };
2309
2310 const size_t numDevices = mFakePolicy->getInputDevices().size();
2311
2312 // UinputDevice does not set any event or key bits, so InputReader should not
2313 // consider it as a valid device.
2314 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2315 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2316 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2317 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2318
2319 invalidDevice.reset();
2320 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2321 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2322 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2323}
2324
2325TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2326 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2327
2328 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2329 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2331 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2332
2333 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002334 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002335 const auto& it =
2336 std::find_if(inputDevices.begin(), inputDevices.end(),
2337 [&keyboard](const InputDeviceInfo& info) {
2338 return info.getIdentifier().name == keyboard->getName();
2339 });
2340
2341 ASSERT_NE(it, inputDevices.end());
2342 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2343 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2344 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002345
2346 keyboard.reset();
2347 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2349 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2350}
2351
2352TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2353 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2355
2356 NotifyConfigurationChangedArgs configChangedArgs;
2357 ASSERT_NO_FATAL_FAILURE(
2358 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002359 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002360 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2361
2362 NotifyKeyArgs keyArgs;
2363 keyboard->pressAndReleaseHomeKey();
2364 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2365 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002366 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002367 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002368 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002369 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002370 prevTimestamp = keyArgs.eventTime;
2371
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2373 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002374 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002375 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002376 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002377}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002379/**
2380 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2381 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2382 * are passed to the listener.
2383 */
2384static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2385TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2386 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2387 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2388 NotifyKeyArgs keyArgs;
2389
2390 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2392 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2393 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2394
2395 controller->pressAndReleaseKey(BTN_GEAR_UP);
2396 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2398 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2399}
2400
Arthur Hungaab25622020-01-16 11:22:11 +08002401// --- TouchProcessTest ---
2402class TouchIntegrationTest : public InputReaderIntegrationTest {
2403protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002404 const std::string UNIQUE_ID = "local:0";
2405
Chris Yea52ade12020-08-27 16:49:20 -07002406 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002407 InputReaderIntegrationTest::SetUp();
2408 // At least add an internal display.
2409 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2410 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002411 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002412
2413 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2414 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2416 }
2417
2418 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2419 int32_t orientation, const std::string& uniqueId,
2420 std::optional<uint8_t> physicalPort,
2421 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002422 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2423 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002424 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2425 }
2426
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002427 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2428 NotifyMotionArgs args;
2429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2430 EXPECT_EQ(action, args.action);
2431 ASSERT_EQ(points.size(), args.pointerCount);
2432 for (size_t i = 0; i < args.pointerCount; i++) {
2433 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2434 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2435 }
2436 }
2437
Arthur Hungaab25622020-01-16 11:22:11 +08002438 std::unique_ptr<UinputTouchScreen> mDevice;
2439};
2440
2441TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2442 NotifyMotionArgs args;
2443 const Point centerPoint = mDevice->getCenterPoint();
2444
2445 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002446 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002447 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002448 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002449 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2451
2452 // ACTION_MOVE
2453 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002454 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002455 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2457
2458 // ACTION_UP
2459 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002460 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2462 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2463}
2464
2465TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2466 NotifyMotionArgs args;
2467 const Point centerPoint = mDevice->getCenterPoint();
2468
2469 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002470 mDevice->sendSlot(FIRST_SLOT);
2471 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002472 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002473 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2476
2477 // ACTION_POINTER_DOWN (Second slot)
2478 const Point secondPoint = centerPoint + Point(100, 100);
2479 mDevice->sendSlot(SECOND_SLOT);
2480 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002481 mDevice->sendDown(secondPoint);
2482 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002483 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002484 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002485
2486 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002487 mDevice->sendMove(secondPoint + Point(1, 1));
2488 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2491
2492 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002493 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002494 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002495 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002496 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002497
2498 // ACTION_UP
2499 mDevice->sendSlot(FIRST_SLOT);
2500 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002501 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002502 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2503 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2504}
2505
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002506/**
2507 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2508 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2509 * data?
2510 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2511 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2512 * for Pointer 0 only is generated after.
2513 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2514 * events, we will not miss any information.
2515 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2516 * event generated afterwards that contains the newest movement of pointer 0.
2517 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2518 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2519 * losing information about non-palm pointers.
2520 */
2521TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2522 NotifyMotionArgs args;
2523 const Point centerPoint = mDevice->getCenterPoint();
2524
2525 // ACTION_DOWN
2526 mDevice->sendSlot(FIRST_SLOT);
2527 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2528 mDevice->sendDown(centerPoint);
2529 mDevice->sendSync();
2530 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2531
2532 // ACTION_POINTER_DOWN (Second slot)
2533 const Point secondPoint = centerPoint + Point(100, 100);
2534 mDevice->sendSlot(SECOND_SLOT);
2535 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2536 mDevice->sendDown(secondPoint);
2537 mDevice->sendSync();
2538 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2539
2540 // ACTION_MOVE (First slot)
2541 mDevice->sendSlot(FIRST_SLOT);
2542 mDevice->sendMove(centerPoint + Point(5, 5));
2543 // ACTION_POINTER_UP (Second slot)
2544 mDevice->sendSlot(SECOND_SLOT);
2545 mDevice->sendPointerUp();
2546 // Send a single sync for the above 2 pointer updates
2547 mDevice->sendSync();
2548
2549 // First, we should get POINTER_UP for the second pointer
2550 assertReceivedMotion(ACTION_POINTER_1_UP,
2551 {/*first pointer */ centerPoint + Point(5, 5),
2552 /*second pointer*/ secondPoint});
2553
2554 // Next, the MOVE event for the first pointer
2555 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2556}
2557
2558/**
2559 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2560 * move, and then it will go up, all in the same frame.
2561 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2562 * gets sent to the listener.
2563 */
2564TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2565 NotifyMotionArgs args;
2566 const Point centerPoint = mDevice->getCenterPoint();
2567
2568 // ACTION_DOWN
2569 mDevice->sendSlot(FIRST_SLOT);
2570 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2571 mDevice->sendDown(centerPoint);
2572 mDevice->sendSync();
2573 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2574
2575 // ACTION_POINTER_DOWN (Second slot)
2576 const Point secondPoint = centerPoint + Point(100, 100);
2577 mDevice->sendSlot(SECOND_SLOT);
2578 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2579 mDevice->sendDown(secondPoint);
2580 mDevice->sendSync();
2581 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2582
2583 // ACTION_MOVE (First slot)
2584 mDevice->sendSlot(FIRST_SLOT);
2585 mDevice->sendMove(centerPoint + Point(5, 5));
2586 // ACTION_POINTER_UP (Second slot)
2587 mDevice->sendSlot(SECOND_SLOT);
2588 mDevice->sendMove(secondPoint + Point(6, 6));
2589 mDevice->sendPointerUp();
2590 // Send a single sync for the above 2 pointer updates
2591 mDevice->sendSync();
2592
2593 // First, we should get POINTER_UP for the second pointer
2594 // The movement of the second pointer during the liftoff frame is ignored.
2595 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2596 assertReceivedMotion(ACTION_POINTER_1_UP,
2597 {/*first pointer */ centerPoint + Point(5, 5),
2598 /*second pointer*/ secondPoint});
2599
2600 // Next, the MOVE event for the first pointer
2601 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2602}
2603
Arthur Hungaab25622020-01-16 11:22:11 +08002604TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2605 NotifyMotionArgs args;
2606 const Point centerPoint = mDevice->getCenterPoint();
2607
2608 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002609 mDevice->sendSlot(FIRST_SLOT);
2610 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002611 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002612 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002613 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2614 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2615
arthurhungcc7f9802020-04-30 17:55:40 +08002616 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002617 const Point secondPoint = centerPoint + Point(100, 100);
2618 mDevice->sendSlot(SECOND_SLOT);
2619 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2620 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002621 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002622 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002623 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002624
arthurhungcc7f9802020-04-30 17:55:40 +08002625 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002626 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002627 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002628 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2630
arthurhungcc7f9802020-04-30 17:55:40 +08002631 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2632 // a palm event.
2633 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002634 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002635 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002636 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002637 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002638 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002639
arthurhungcc7f9802020-04-30 17:55:40 +08002640 // Send up to second slot, expect first slot send moving.
2641 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002642 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002643 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002645
arthurhungcc7f9802020-04-30 17:55:40 +08002646 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002647 mDevice->sendSlot(FIRST_SLOT);
2648 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002649 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002650
arthurhungcc7f9802020-04-30 17:55:40 +08002651 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2652 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002653}
2654
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656class InputDeviceTest : public testing::Test {
2657protected:
2658 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002659 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 static const int32_t DEVICE_ID;
2661 static const int32_t DEVICE_GENERATION;
2662 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002663 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002664 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002666 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002668 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002669 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002670 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671
Chris Yea52ade12020-08-27 16:49:20 -07002672 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002673 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002675 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002676 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002677 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678 InputDeviceIdentifier identifier;
2679 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002680 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002681 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002682 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002683 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002684 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002685 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686 }
2687
Chris Yea52ade12020-08-27 16:49:20 -07002688 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002689 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 }
2692};
2693
2694const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002695const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002696const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2698const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002699const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002700 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002701const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702
2703TEST_F(InputDeviceTest, ImmutableProperties) {
2704 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002705 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002706 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707}
2708
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002709TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2710 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002711}
2712
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2714 // Configuration.
2715 InputReaderConfiguration config;
2716 mDevice->configure(ARBITRARY_TIME, &config, 0);
2717
2718 // Reset.
2719 mDevice->reset(ARBITRARY_TIME);
2720
2721 NotifyDeviceResetArgs resetArgs;
2722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2723 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2724 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2725
2726 // Metadata.
2727 ASSERT_TRUE(mDevice->isIgnored());
2728 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2729
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002730 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002732 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002733 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2734 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2735
2736 // State queries.
2737 ASSERT_EQ(0, mDevice->getMetaState());
2738
2739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2740 << "Ignored device should return unknown key code state.";
2741 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2742 << "Ignored device should return unknown scan code state.";
2743 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2744 << "Ignored device should return unknown switch state.";
2745
2746 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2747 uint8_t flags[2] = { 0, 1 };
2748 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2749 << "Ignored device should never mark any key codes.";
2750 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2751 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2752}
2753
2754TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2755 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002756 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002758 FakeInputMapper& mapper1 =
2759 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002760 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2761 mapper1.setMetaState(AMETA_ALT_ON);
2762 mapper1.addSupportedKeyCode(AKEYCODE_A);
2763 mapper1.addSupportedKeyCode(AKEYCODE_B);
2764 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2765 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2766 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2767 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2768 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002770 FakeInputMapper& mapper2 =
2771 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002772 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002773
2774 InputReaderConfiguration config;
2775 mDevice->configure(ARBITRARY_TIME, &config, 0);
2776
2777 String8 propertyValue;
2778 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2779 << "Device should have read configuration during configuration phase.";
2780 ASSERT_STREQ("value", propertyValue.string());
2781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2783 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
2785 // Reset
2786 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2788 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789
2790 NotifyDeviceResetArgs resetArgs;
2791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2792 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2793 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2794
2795 // Metadata.
2796 ASSERT_FALSE(mDevice->isIgnored());
2797 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2798
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002799 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002801 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2803 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2804
2805 // State queries.
2806 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2807 << "Should query mappers and combine meta states.";
2808
2809 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2810 << "Should return unknown key code state when source not supported.";
2811 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2812 << "Should return unknown scan code state when source not supported.";
2813 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2814 << "Should return unknown switch state when source not supported.";
2815
2816 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2817 << "Should query mapper when source is supported.";
2818 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2819 << "Should query mapper when source is supported.";
2820 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2821 << "Should query mapper when source is supported.";
2822
2823 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2824 uint8_t flags[4] = { 0, 0, 0, 1 };
2825 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2826 << "Should do nothing when source is unsupported.";
2827 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2828 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2829 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2830 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2831
2832 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2833 << "Should query mapper when source is supported.";
2834 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2835 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2836 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2837 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2838
2839 // Event handling.
2840 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002841 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842 mDevice->process(&event, 1);
2843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002844 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2845 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846}
2847
Arthur Hung2c9a3342019-07-23 14:18:59 +08002848// A single input device is associated with a specific display. Check that:
2849// 1. Device is disabled if the viewport corresponding to the associated display is not found
2850// 2. Device is disabled when setEnabled API is called
2851TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002852 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002853
2854 // First Configuration.
2855 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2856
2857 // Device should be enabled by default.
2858 ASSERT_TRUE(mDevice->isEnabled());
2859
2860 // Prepare associated info.
2861 constexpr uint8_t hdmi = 1;
2862 const std::string UNIQUE_ID = "local:1";
2863
2864 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2865 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2866 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2867 // Device should be disabled because it is associated with a specific display via
2868 // input port <-> display port association, but the corresponding display is not found
2869 ASSERT_FALSE(mDevice->isEnabled());
2870
2871 // Prepare displays.
2872 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002873 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2874 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002875 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2876 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2877 ASSERT_TRUE(mDevice->isEnabled());
2878
2879 // Device should be disabled after set disable.
2880 mFakePolicy->addDisabledDevice(mDevice->getId());
2881 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2882 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2883 ASSERT_FALSE(mDevice->isEnabled());
2884
2885 // Device should still be disabled even found the associated display.
2886 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2887 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2888 ASSERT_FALSE(mDevice->isEnabled());
2889}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002890
Christine Franks1ba71cc2021-04-07 14:37:42 -07002891TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2892 // Device should be enabled by default.
2893 mFakePolicy->clearViewports();
2894 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2895 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2896 ASSERT_TRUE(mDevice->isEnabled());
2897
2898 // Device should be disabled because it is associated with a specific display, but the
2899 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002900 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002901 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2902 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2903 ASSERT_FALSE(mDevice->isEnabled());
2904
2905 // Device should be enabled when a display is found.
2906 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2907 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2908 NO_PORT, ViewportType::INTERNAL);
2909 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2910 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2911 ASSERT_TRUE(mDevice->isEnabled());
2912
2913 // Device should be disabled after set disable.
2914 mFakePolicy->addDisabledDevice(mDevice->getId());
2915 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2916 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2917 ASSERT_FALSE(mDevice->isEnabled());
2918
2919 // Device should still be disabled even found the associated display.
2920 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2921 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2922 ASSERT_FALSE(mDevice->isEnabled());
2923}
2924
Christine Franks2a2293c2022-01-18 11:51:16 -08002925TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2926 mFakePolicy->clearViewports();
2927 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2928 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2929
Christine Franks2a2293c2022-01-18 11:51:16 -08002930 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2931 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2932 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2933 NO_PORT, ViewportType::INTERNAL);
2934 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2935 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2936 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2937}
2938
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002939/**
2940 * This test reproduces a crash caused by a dangling reference that remains after device is added
2941 * and removed. The reference is accessed in InputDevice::dump(..);
2942 */
2943TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2944 constexpr int32_t TEST_EVENTHUB_ID = 10;
2945 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2946
2947 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
2948 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
2949 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2950 std::string dumpStr, eventHubDevStr;
2951 device.dump(dumpStr, eventHubDevStr);
2952}
2953
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954// --- InputMapperTest ---
2955
2956class InputMapperTest : public testing::Test {
2957protected:
2958 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002959 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 static const int32_t DEVICE_ID;
2961 static const int32_t DEVICE_GENERATION;
2962 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002963 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002964 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002966 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002968 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002969 std::unique_ptr<InstrumentedInputReader> mReader;
2970 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002972 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002973 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002975 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002976 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002977 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002978 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002979 // Consume the device reset notification generated when adding a new device.
2980 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 }
2982
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002983 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002984 SetUp(DEVICE_CLASSES);
2985 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002986
Chris Yea52ade12020-08-27 16:49:20 -07002987 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002988 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990 }
2991
2992 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002993 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 }
2995
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002996 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002997 if (!changes ||
2998 (changes &
2999 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3000 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003001 mReader->requestRefreshConfiguration(changes);
3002 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003003 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003004 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhan36690412022-08-05 22:26:56 +00003005 // Loop the reader to flush the input listener queue.
3006 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003007 }
3008
arthurhungdcef2dc2020-08-11 14:47:50 +08003009 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3010 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003011 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003012 InputDeviceIdentifier identifier;
3013 identifier.name = name;
3014 identifier.location = location;
3015 std::shared_ptr<InputDevice> device =
3016 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3017 identifier);
3018 mReader->pushNextDevice(device);
3019 mFakeEventHub->addDevice(eventHubId, name, classes);
3020 mReader->loopOnce();
3021 return device;
3022 }
3023
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003024 template <class T, typename... Args>
3025 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003026 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003027 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003028 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003029 mapper.reset(ARBITRARY_TIME);
Prabir Pradhan36690412022-08-05 22:26:56 +00003030 // Loop the reader to flush the input listener queue.
3031 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003032 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033 }
3034
3035 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003036 int32_t orientation, const std::string& uniqueId,
3037 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003038 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3039 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003040 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3041 }
3042
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003043 void clearViewports() {
3044 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 }
3046
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003047 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3048 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 RawEvent event;
3050 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003051 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003052 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 event.type = type;
3054 event.code = code;
3055 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003056 mapper.process(&event);
Prabir Pradhan36690412022-08-05 22:26:56 +00003057 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003058 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059 }
3060
3061 static void assertMotionRange(const InputDeviceInfo& info,
3062 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3063 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003064 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3066 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3067 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3068 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3069 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3070 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3071 }
3072
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003073 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3074 float size, float touchMajor, float touchMinor, float toolMajor,
3075 float toolMinor, float orientation, float distance,
3076 float scaledAxisEpsilon = 1.f) {
3077 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3078 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3080 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003081 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3082 scaledAxisEpsilon);
3083 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3084 scaledAxisEpsilon);
3085 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3086 scaledAxisEpsilon);
3087 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3088 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3090 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3091 }
3092
Michael Wright17db18e2020-06-26 20:51:44 +01003093 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003095 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096 ASSERT_NEAR(x, actualX, 1);
3097 ASSERT_NEAR(y, actualY, 1);
3098 }
3099};
3100
3101const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003102const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003103const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3105const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003106const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3107 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003108const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109
3110// --- SwitchInputMapperTest ---
3111
3112class SwitchInputMapperTest : public InputMapperTest {
3113protected:
3114};
3115
3116TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003117 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003119 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120}
3121
3122TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003123 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003125 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003128 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003129 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130}
3131
3132TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003133 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139
3140 NotifySwitchArgs args;
3141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3142 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003143 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3144 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145 args.switchMask);
3146 ASSERT_EQ(uint32_t(0), args.policyFlags);
3147}
3148
Chris Ye87143712020-11-10 05:05:58 +00003149// --- VibratorInputMapperTest ---
3150class VibratorInputMapperTest : public InputMapperTest {
3151protected:
3152 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3153};
3154
3155TEST_F(VibratorInputMapperTest, GetSources) {
3156 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3157
3158 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3159}
3160
3161TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3162 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3163
3164 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3165}
3166
3167TEST_F(VibratorInputMapperTest, Vibrate) {
3168 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003169 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003170 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3171
3172 VibrationElement pattern(2);
3173 VibrationSequence sequence(2);
3174 pattern.duration = std::chrono::milliseconds(200);
3175 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3176 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3177 sequence.addElement(pattern);
3178 pattern.duration = std::chrono::milliseconds(500);
3179 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3180 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3181 sequence.addElement(pattern);
3182
3183 std::vector<int64_t> timings = {0, 1};
3184 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3185
3186 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003187 // Start vibrating
3188 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003189 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003190 // Verify vibrator state listener was notified.
3191 mReader->loopOnce();
3192 NotifyVibratorStateArgs args;
3193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3194 ASSERT_EQ(DEVICE_ID, args.deviceId);
3195 ASSERT_TRUE(args.isOn);
3196 // Stop vibrating
3197 mapper.cancelVibrate(VIBRATION_TOKEN);
3198 ASSERT_FALSE(mapper.isVibrating());
3199 // Verify vibrator state listener was notified.
3200 mReader->loopOnce();
3201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3202 ASSERT_EQ(DEVICE_ID, args.deviceId);
3203 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003204}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205
Chris Yef59a2f42020-10-16 12:55:26 -07003206// --- SensorInputMapperTest ---
3207
3208class SensorInputMapperTest : public InputMapperTest {
3209protected:
3210 static const int32_t ACCEL_RAW_MIN;
3211 static const int32_t ACCEL_RAW_MAX;
3212 static const int32_t ACCEL_RAW_FUZZ;
3213 static const int32_t ACCEL_RAW_FLAT;
3214 static const int32_t ACCEL_RAW_RESOLUTION;
3215
3216 static const int32_t GYRO_RAW_MIN;
3217 static const int32_t GYRO_RAW_MAX;
3218 static const int32_t GYRO_RAW_FUZZ;
3219 static const int32_t GYRO_RAW_FLAT;
3220 static const int32_t GYRO_RAW_RESOLUTION;
3221
3222 static const float GRAVITY_MS2_UNIT;
3223 static const float DEGREE_RADIAN_UNIT;
3224
3225 void prepareAccelAxes();
3226 void prepareGyroAxes();
3227 void setAccelProperties();
3228 void setGyroProperties();
3229 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3230};
3231
3232const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3233const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3234const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3235const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3236const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3237
3238const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3239const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3240const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3241const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3242const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3243
3244const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3245const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3246
3247void SensorInputMapperTest::prepareAccelAxes() {
3248 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3249 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3250 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3251 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3252 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3253 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3254}
3255
3256void SensorInputMapperTest::prepareGyroAxes() {
3257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3258 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3259 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3260 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3261 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3262 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3263}
3264
3265void SensorInputMapperTest::setAccelProperties() {
3266 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3267 /* sensorDataIndex */ 0);
3268 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3269 /* sensorDataIndex */ 1);
3270 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3271 /* sensorDataIndex */ 2);
3272 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3273 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3274 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3275 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3276 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3277}
3278
3279void SensorInputMapperTest::setGyroProperties() {
3280 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3281 /* sensorDataIndex */ 0);
3282 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3283 /* sensorDataIndex */ 1);
3284 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3285 /* sensorDataIndex */ 2);
3286 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3287 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3288 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3289 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3290 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3291}
3292
3293TEST_F(SensorInputMapperTest, GetSources) {
3294 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3295
3296 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3297}
3298
3299TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3300 setAccelProperties();
3301 prepareAccelAxes();
3302 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3303
3304 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3305 std::chrono::microseconds(10000),
3306 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003307 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003313
3314 NotifySensorArgs args;
3315 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3316 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3317 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3318
3319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3320 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3321 ASSERT_EQ(args.deviceId, DEVICE_ID);
3322 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3323 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3324 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3325 ASSERT_EQ(args.values, values);
3326 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3327}
3328
3329TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3330 setGyroProperties();
3331 prepareGyroAxes();
3332 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3333
3334 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3335 std::chrono::microseconds(10000),
3336 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003337 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003343
3344 NotifySensorArgs args;
3345 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3346 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3347 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3348
3349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3350 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3351 ASSERT_EQ(args.deviceId, DEVICE_ID);
3352 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3353 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3354 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3355 ASSERT_EQ(args.values, values);
3356 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3357}
3358
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359// --- KeyboardInputMapperTest ---
3360
3361class KeyboardInputMapperTest : public InputMapperTest {
3362protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003363 const std::string UNIQUE_ID = "local:0";
3364
3365 void prepareDisplay(int32_t orientation);
3366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003368 int32_t originalKeyCode, int32_t rotatedKeyCode,
3369 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370};
3371
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003372/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3373 * orientation.
3374 */
3375void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003376 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3377 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003378}
3379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003380void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003381 int32_t originalScanCode, int32_t originalKeyCode,
3382 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383 NotifyKeyArgs args;
3384
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3387 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3388 ASSERT_EQ(originalScanCode, args.scanCode);
3389 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003390 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3395 ASSERT_EQ(originalScanCode, args.scanCode);
3396 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003397 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398}
3399
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003401 KeyboardInputMapper& mapper =
3402 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3403 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003405 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406}
3407
3408TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3409 const int32_t USAGE_A = 0x070004;
3410 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003411 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3412 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003413 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3414 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3415 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003417 KeyboardInputMapper& mapper =
3418 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3419 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003420 // Initial metastate is AMETA_NONE.
3421 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422
3423 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 NotifyKeyArgs args;
3426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3427 ASSERT_EQ(DEVICE_ID, args.deviceId);
3428 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3429 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3431 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3432 ASSERT_EQ(KEY_HOME, args.scanCode);
3433 ASSERT_EQ(AMETA_NONE, args.metaState);
3434 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3435 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3436 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3437
3438 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003439 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(DEVICE_ID, args.deviceId);
3442 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3443 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3444 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3445 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3446 ASSERT_EQ(KEY_HOME, args.scanCode);
3447 ASSERT_EQ(AMETA_NONE, args.metaState);
3448 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3449 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3450 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3451
3452 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3456 ASSERT_EQ(DEVICE_ID, args.deviceId);
3457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3458 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3459 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3460 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3461 ASSERT_EQ(0, args.scanCode);
3462 ASSERT_EQ(AMETA_NONE, args.metaState);
3463 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3464 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3465 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3466
3467 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3469 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3471 ASSERT_EQ(DEVICE_ID, args.deviceId);
3472 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3473 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3474 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3475 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3476 ASSERT_EQ(0, args.scanCode);
3477 ASSERT_EQ(AMETA_NONE, args.metaState);
3478 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3479 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3480 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3481
3482 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3486 ASSERT_EQ(DEVICE_ID, args.deviceId);
3487 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3488 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3489 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3490 ASSERT_EQ(0, args.keyCode);
3491 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3492 ASSERT_EQ(AMETA_NONE, args.metaState);
3493 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3494 ASSERT_EQ(0U, args.policyFlags);
3495 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3496
3497 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3499 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3501 ASSERT_EQ(DEVICE_ID, args.deviceId);
3502 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3503 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3504 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3505 ASSERT_EQ(0, args.keyCode);
3506 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3507 ASSERT_EQ(AMETA_NONE, args.metaState);
3508 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3509 ASSERT_EQ(0U, args.policyFlags);
3510 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3511}
3512
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513/**
3514 * Ensure that the readTime is set to the time when the EV_KEY is received.
3515 */
3516TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3517 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3518
3519 KeyboardInputMapper& mapper =
3520 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3521 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3522 NotifyKeyArgs args;
3523
3524 // Key down
3525 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3527 ASSERT_EQ(12, args.readTime);
3528
3529 // Key up
3530 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3532 ASSERT_EQ(15, args.readTime);
3533}
3534
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3537 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003538 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3539 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3540 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 KeyboardInputMapper& mapper =
3543 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3544 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545
Arthur Hung95f68612022-04-07 14:08:22 +08003546 // Initial metastate is AMETA_NONE.
3547 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548
3549 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 NotifyKeyArgs args;
3552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3553 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003554 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003555 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556
3557 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003558 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3560 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003561 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562
3563 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003564 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3566 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568
3569 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003570 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3572 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003573 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003574 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003575}
3576
3577TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3580 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3581 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003583 KeyboardInputMapper& mapper =
3584 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3585 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003587 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3589 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3590 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3591 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3593 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3595 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3596}
3597
3598TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003599 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3600 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3601 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3602 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003605 KeyboardInputMapper& mapper =
3606 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3607 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003609 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003610 ASSERT_NO_FATAL_FAILURE(
3611 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3613 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3615 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3616 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3617 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003619 clearViewports();
3620 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003621 ASSERT_NO_FATAL_FAILURE(
3622 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3624 AKEYCODE_DPAD_UP, DISPLAY_ID));
3625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3626 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3627 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3628 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003630 clearViewports();
3631 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003632 ASSERT_NO_FATAL_FAILURE(
3633 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3635 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3636 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3637 AKEYCODE_DPAD_UP, DISPLAY_ID));
3638 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3639 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003641 clearViewports();
3642 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003643 ASSERT_NO_FATAL_FAILURE(
3644 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3646 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3648 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3649 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3650 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003651
3652 // Special case: if orientation changes while key is down, we still emit the same keycode
3653 // in the key up as we did in the key down.
3654 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003655 clearViewports();
3656 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3659 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3660 ASSERT_EQ(KEY_UP, args.scanCode);
3661 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3662
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003663 clearViewports();
3664 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3667 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3668 ASSERT_EQ(KEY_UP, args.scanCode);
3669 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3670}
3671
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003672TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3673 // If the keyboard is not orientation aware,
3674 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003675 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003676
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003677 KeyboardInputMapper& mapper =
3678 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3679 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003680 NotifyKeyArgs args;
3681
3682 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3687 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3688
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003689 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3694 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3695}
3696
3697TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3698 // If the keyboard is orientation aware,
3699 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003700 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003701
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003702 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003703 KeyboardInputMapper& mapper =
3704 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3705 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003706 NotifyKeyArgs args;
3707
3708 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3709 // ^--- already checked by the previous test
3710
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003711 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003712 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3717 ASSERT_EQ(DISPLAY_ID, args.displayId);
3718
3719 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003720 clearViewports();
3721 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003722 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3727 ASSERT_EQ(newDisplayId, args.displayId);
3728}
3729
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003731 KeyboardInputMapper& mapper =
3732 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3733 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003735 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003736 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003738 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003739 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740}
3741
Philip Junker4af3b3d2021-12-14 10:36:55 +01003742TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3743 KeyboardInputMapper& mapper =
3744 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3745 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3746
3747 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3748 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3749 << "If a mapping is available, the result is equal to the mapping";
3750
3751 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3752 << "If no mapping is available, the result is the key location";
3753}
3754
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003756 KeyboardInputMapper& mapper =
3757 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3758 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003760 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003763 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765}
3766
3767TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003768 KeyboardInputMapper& mapper =
3769 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3770 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003772 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
3774 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3775 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777 ASSERT_TRUE(flags[0]);
3778 ASSERT_FALSE(flags[1]);
3779}
3780
3781TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003782 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3783 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3784 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3787 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789 KeyboardInputMapper& mapper =
3790 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3791 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003792 // Initial metastate is AMETA_NONE.
3793 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794
3795 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003796 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3797 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3798 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799
3800 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003803 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3804 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3805 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807
3808 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003811 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3812 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3813 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003814 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003815
3816 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003819 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3820 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3821 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003822 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823
3824 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003827 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3828 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3829 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003830 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003831
3832 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003835 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3836 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3837 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003838 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839
3840 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003843 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3844 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3845 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003846 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847}
3848
Chris Yea52ade12020-08-27 16:49:20 -07003849TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3850 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3851 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3852 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3853 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3854
3855 KeyboardInputMapper& mapper =
3856 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3857 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3858
Chris Yea52ade12020-08-27 16:49:20 -07003859 // Meta state should be AMETA_NONE after reset
3860 mapper.reset(ARBITRARY_TIME);
3861 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3862 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3863 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3864 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3865
3866 NotifyKeyArgs args;
3867 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3870 ASSERT_EQ(AMETA_NONE, args.metaState);
3871 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3872 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3873 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3874
3875 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003876 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3878 ASSERT_EQ(AMETA_NONE, args.metaState);
3879 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3880 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3881 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3882}
3883
Arthur Hung2c9a3342019-07-23 14:18:59 +08003884TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3885 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003886 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3887 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3888 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3889 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003890
3891 // keyboard 2.
3892 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003893 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003894 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003895 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003896 std::shared_ptr<InputDevice> device2 =
3897 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003898 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003899
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003900 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3901 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3902 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3903 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003905 KeyboardInputMapper& mapper =
3906 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3907 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003908
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003909 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003910 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003911 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003912 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3913 device2->reset(ARBITRARY_TIME);
3914
3915 // Prepared displays and associated info.
3916 constexpr uint8_t hdmi1 = 0;
3917 constexpr uint8_t hdmi2 = 1;
3918 const std::string SECONDARY_UNIQUE_ID = "local:1";
3919
3920 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3921 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3922
3923 // No associated display viewport found, should disable the device.
3924 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3925 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3926 ASSERT_FALSE(device2->isEnabled());
3927
3928 // Prepare second display.
3929 constexpr int32_t newDisplayId = 2;
3930 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003931 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003932 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003933 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003934 // Default device will reconfigure above, need additional reconfiguration for another device.
3935 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3936 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3937
3938 // Device should be enabled after the associated display is found.
3939 ASSERT_TRUE(mDevice->isEnabled());
3940 ASSERT_TRUE(device2->isEnabled());
3941
3942 // Test pad key events
3943 ASSERT_NO_FATAL_FAILURE(
3944 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3945 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3946 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3947 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3948 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3949 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3950 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3951
3952 ASSERT_NO_FATAL_FAILURE(
3953 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3954 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3955 AKEYCODE_DPAD_RIGHT, newDisplayId));
3956 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3957 AKEYCODE_DPAD_DOWN, newDisplayId));
3958 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3959 AKEYCODE_DPAD_LEFT, newDisplayId));
3960}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961
arthurhungc903df12020-08-11 15:08:42 +08003962TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3963 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3964 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3965 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3966 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3967 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3968 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3969
3970 KeyboardInputMapper& mapper =
3971 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3972 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003973 // Initial metastate is AMETA_NONE.
3974 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003975
3976 // Initialization should have turned all of the lights off.
3977 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3978 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3979 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3980
3981 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003984 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3985 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3986
3987 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003990 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3991 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3992
3993 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003996 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3997 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3998
3999 mFakeEventHub->removeDevice(EVENTHUB_ID);
4000 mReader->loopOnce();
4001
4002 // keyboard 2 should default toggle keys.
4003 const std::string USB2 = "USB2";
4004 const std::string DEVICE_NAME2 = "KEYBOARD2";
4005 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4006 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4007 std::shared_ptr<InputDevice> device2 =
4008 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004009 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004010 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4011 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4012 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4013 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4014 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4015 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4016
arthurhung6fe95782020-10-05 22:41:16 +08004017 KeyboardInputMapper& mapper2 =
4018 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4019 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004020 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4021 device2->reset(ARBITRARY_TIME);
4022
4023 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4024 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4025 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004026 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4027 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004028}
4029
Arthur Hungcb40a002021-08-03 14:31:01 +00004030TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4032 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4033 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4034
4035 // Suppose we have two mappers. (DPAD + KEYBOARD)
4036 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4037 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4038 KeyboardInputMapper& mapper =
4039 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4040 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004041 // Initial metastate is AMETA_NONE.
4042 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004043
4044 mReader->toggleCapsLockState(DEVICE_ID);
4045 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4046}
4047
Arthur Hungfb3cc112022-04-13 07:39:50 +00004048TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4049 // keyboard 1.
4050 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4051 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4052 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4054 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4055 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4056
4057 KeyboardInputMapper& mapper1 =
4058 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4059 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4060
4061 // keyboard 2.
4062 const std::string USB2 = "USB2";
4063 const std::string DEVICE_NAME2 = "KEYBOARD2";
4064 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4065 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4066 std::shared_ptr<InputDevice> device2 =
4067 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4068 ftl::Flags<InputDeviceClass>(0));
4069 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4070 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4071 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4072 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4073 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4074 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4075
4076 KeyboardInputMapper& mapper2 =
4077 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4078 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4079 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4080 device2->reset(ARBITRARY_TIME);
4081
Arthur Hung95f68612022-04-07 14:08:22 +08004082 // Initial metastate is AMETA_NONE.
4083 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4084 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4085
4086 // Toggle num lock on and off.
4087 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4088 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004089 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4090 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4091 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4092
4093 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4094 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4095 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4096 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4097 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4098
4099 // Toggle caps lock on and off.
4100 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4101 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4102 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4103 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4104 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4105
4106 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4107 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4108 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4109 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4110 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4111
4112 // Toggle scroll lock on and off.
4113 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4114 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4115 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4116 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4117 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4118
4119 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4120 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4121 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4122 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4123 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4124}
4125
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004126// --- KeyboardInputMapperTest_ExternalDevice ---
4127
4128class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4129protected:
Chris Yea52ade12020-08-27 16:49:20 -07004130 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004131};
4132
4133TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004134 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4135 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004136
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4138 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4139 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4140 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004142 KeyboardInputMapper& mapper =
4143 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4144 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004147 NotifyKeyArgs args;
4148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4149 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4150
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004151 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4153 ASSERT_EQ(uint32_t(0), args.policyFlags);
4154
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4157 ASSERT_EQ(uint32_t(0), args.policyFlags);
4158
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004159 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4161 ASSERT_EQ(uint32_t(0), args.policyFlags);
4162
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4165 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4166
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004167 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4169 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4170}
4171
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004172TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004173 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004174
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004175 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4176 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4177 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004178
Powei Fengd041c5d2019-05-03 17:11:33 -07004179 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004180 KeyboardInputMapper& mapper =
4181 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4182 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004183
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004185 NotifyKeyArgs args;
4186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4187 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4188
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004189 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4191 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4192
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4195 ASSERT_EQ(uint32_t(0), args.policyFlags);
4196
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004197 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4199 ASSERT_EQ(uint32_t(0), args.policyFlags);
4200
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4203 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4204
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004205 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4207 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4208}
4209
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210// --- CursorInputMapperTest ---
4211
4212class CursorInputMapperTest : public InputMapperTest {
4213protected:
4214 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4215
Michael Wright17db18e2020-06-26 20:51:44 +01004216 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217
Chris Yea52ade12020-08-27 16:49:20 -07004218 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 InputMapperTest::SetUp();
4220
Michael Wright17db18e2020-06-26 20:51:44 +01004221 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004222 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 }
4224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004225 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4226 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004227
4228 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004229 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4230 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4231 }
4232
4233 void prepareSecondaryDisplay() {
4234 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4235 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4236 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004237 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004238
4239 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4240 float pressure) {
4241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4242 0.0f, 0.0f, 0.0f, EPSILON));
4243 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244};
4245
4246const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004248void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4249 int32_t originalY, int32_t rotatedX,
4250 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 NotifyMotionArgs args;
4252
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004258 ASSERT_NO_FATAL_FAILURE(
4259 assertCursorPointerCoords(args.pointerCoords[0],
4260 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4261 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262}
4263
4264TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004266 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004268 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269}
4270
4271TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004273 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004275 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276}
4277
4278TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004280 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281
4282 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004283 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284
4285 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004286 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4287 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4289 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4290
4291 // When the bounds are set, then there should be a valid motion range.
4292 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4293
4294 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004295 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296
4297 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4298 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4299 1, 800 - 1, 0.0f, 0.0f));
4300 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4301 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4302 2, 480 - 1, 0.0f, 0.0f));
4303 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4304 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4305 0.0f, 1.0f, 0.0f, 0.0f));
4306}
4307
4308TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004310 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311
4312 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004313 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314
4315 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4316 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4317 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4318 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4319 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4320 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4321 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4322 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4323 0.0f, 1.0f, 0.0f, 0.0f));
4324}
4325
4326TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004328 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329
arthurhungdcef2dc2020-08-11 14:47:50 +08004330 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331
4332 NotifyMotionArgs args;
4333
4334 // Button press.
4335 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4339 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4340 ASSERT_EQ(DEVICE_ID, args.deviceId);
4341 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4342 ASSERT_EQ(uint32_t(0), args.policyFlags);
4343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4344 ASSERT_EQ(0, args.flags);
4345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4346 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4347 ASSERT_EQ(0, args.edgeFlags);
4348 ASSERT_EQ(uint32_t(1), args.pointerCount);
4349 ASSERT_EQ(0, args.pointerProperties[0].id);
4350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004351 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4353 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4354 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4355
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4357 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4358 ASSERT_EQ(DEVICE_ID, args.deviceId);
4359 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4360 ASSERT_EQ(uint32_t(0), args.policyFlags);
4361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4362 ASSERT_EQ(0, args.flags);
4363 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4364 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4365 ASSERT_EQ(0, args.edgeFlags);
4366 ASSERT_EQ(uint32_t(1), args.pointerCount);
4367 ASSERT_EQ(0, args.pointerProperties[0].id);
4368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004369 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004370 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4371 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4372 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4373
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004375 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4376 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4378 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4379 ASSERT_EQ(DEVICE_ID, args.deviceId);
4380 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4381 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4383 ASSERT_EQ(0, args.flags);
4384 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4385 ASSERT_EQ(0, args.buttonState);
4386 ASSERT_EQ(0, args.edgeFlags);
4387 ASSERT_EQ(uint32_t(1), args.pointerCount);
4388 ASSERT_EQ(0, args.pointerProperties[0].id);
4389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004390 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4392 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4393 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4394
4395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4396 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4397 ASSERT_EQ(DEVICE_ID, args.deviceId);
4398 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4399 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4401 ASSERT_EQ(0, args.flags);
4402 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4403 ASSERT_EQ(0, args.buttonState);
4404 ASSERT_EQ(0, args.edgeFlags);
4405 ASSERT_EQ(uint32_t(1), args.pointerCount);
4406 ASSERT_EQ(0, args.pointerProperties[0].id);
4407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004408 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4410 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4411 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4412}
4413
4414TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004416 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417
4418 NotifyMotionArgs args;
4419
4420 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004425 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4426 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4427 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428
4429 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004434 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4435 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436}
4437
4438TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004440 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441
4442 NotifyMotionArgs args;
4443
4444 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4448 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004449 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4452 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004453 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004454
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004460 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461
4462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004464 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465}
4466
4467TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004469 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470
4471 NotifyMotionArgs args;
4472
4473 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004480 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4481 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4482 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4485 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004486 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4487 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4488 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004489
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004496 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4497 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4498 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
4500 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004505 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004506
4507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004509 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510}
4511
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004512TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004513 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004515 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4516 // need to be rotated.
4517 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004518 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004520 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4529}
4530
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004531TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004532 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004534 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4535 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004536 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004537
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004538 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004539 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4541 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4542 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4543 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4544 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4545 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4546 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4547 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4548
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004549 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004550 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4553 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4554 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4555 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4556 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4557 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4558 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004560 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004561 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4564 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4565 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4566 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4567 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4568 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4569 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4570
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004571 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004572 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4575 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4576 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4577 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4578 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4579 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4580 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581}
4582
4583TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004585 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586
4587 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4588 mFakePointerController->setPosition(100, 200);
4589 mFakePointerController->setButtonState(0);
4590
4591 NotifyMotionArgs motionArgs;
4592 NotifyKeyArgs keyArgs;
4593
4594 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4598 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4599 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4600 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004601 ASSERT_NO_FATAL_FAILURE(
4602 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4605 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4606 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4607 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004608 ASSERT_NO_FATAL_FAILURE(
4609 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004610
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 ASSERT_EQ(0, motionArgs.buttonState);
4616 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004617 ASSERT_NO_FATAL_FAILURE(
4618 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622 ASSERT_EQ(0, motionArgs.buttonState);
4623 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004624 ASSERT_NO_FATAL_FAILURE(
4625 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004626
4627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004629 ASSERT_EQ(0, motionArgs.buttonState);
4630 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004631 ASSERT_NO_FATAL_FAILURE(
4632 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633
4634 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4640 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4641 motionArgs.buttonState);
4642 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4643 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004644 ASSERT_NO_FATAL_FAILURE(
4645 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4648 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4649 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4650 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4651 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004652 ASSERT_NO_FATAL_FAILURE(
4653 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004654
4655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4657 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4658 motionArgs.buttonState);
4659 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4660 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004661 ASSERT_NO_FATAL_FAILURE(
4662 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004663
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4669 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004670 ASSERT_NO_FATAL_FAILURE(
4671 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004672
4673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004675 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4676 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004677 ASSERT_NO_FATAL_FAILURE(
4678 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004683 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4684 ASSERT_EQ(0, motionArgs.buttonState);
4685 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004686 ASSERT_NO_FATAL_FAILURE(
4687 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004690
4691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_EQ(0, motionArgs.buttonState);
4693 ASSERT_EQ(0, mFakePointerController->getButtonState());
4694 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004695 ASSERT_NO_FATAL_FAILURE(
4696 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4699 ASSERT_EQ(0, motionArgs.buttonState);
4700 ASSERT_EQ(0, mFakePointerController->getButtonState());
4701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004702 ASSERT_NO_FATAL_FAILURE(
4703 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704
4705 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4709 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4710 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004711
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4715 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004716 ASSERT_NO_FATAL_FAILURE(
4717 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004718
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4720 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4721 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4722 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004723 ASSERT_NO_FATAL_FAILURE(
4724 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004729 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730 ASSERT_EQ(0, motionArgs.buttonState);
4731 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004732 ASSERT_NO_FATAL_FAILURE(
4733 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004734
4735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737 ASSERT_EQ(0, motionArgs.buttonState);
4738 ASSERT_EQ(0, mFakePointerController->getButtonState());
4739
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004740 ASSERT_NO_FATAL_FAILURE(
4741 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4743 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4744 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4745
4746 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4750 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4751 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004752
Michael Wrightd02c5b62014-02-10 15:10:22 -08004753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004754 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4756 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004757 ASSERT_NO_FATAL_FAILURE(
4758 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004759
4760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4761 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4762 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4763 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004764 ASSERT_NO_FATAL_FAILURE(
4765 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771 ASSERT_EQ(0, motionArgs.buttonState);
4772 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004773 ASSERT_NO_FATAL_FAILURE(
4774 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004775
4776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4777 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4778 ASSERT_EQ(0, motionArgs.buttonState);
4779 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004780 ASSERT_NO_FATAL_FAILURE(
4781 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004782
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4784 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4785 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4786
4787 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4791 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4792 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004793
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004795 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4797 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004798 ASSERT_NO_FATAL_FAILURE(
4799 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004800
4801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4802 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4803 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4804 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004805 ASSERT_NO_FATAL_FAILURE(
4806 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812 ASSERT_EQ(0, motionArgs.buttonState);
4813 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004814 ASSERT_NO_FATAL_FAILURE(
4815 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004816
4817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4818 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4819 ASSERT_EQ(0, motionArgs.buttonState);
4820 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004821 ASSERT_NO_FATAL_FAILURE(
4822 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004823
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4825 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4826 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4827
4828 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4832 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4833 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004834
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4838 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004839 ASSERT_NO_FATAL_FAILURE(
4840 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004841
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4844 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4845 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004846 ASSERT_NO_FATAL_FAILURE(
4847 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 ASSERT_EQ(0, motionArgs.buttonState);
4854 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004855 ASSERT_NO_FATAL_FAILURE(
4856 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004857
4858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4859 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4860 ASSERT_EQ(0, motionArgs.buttonState);
4861 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004862 ASSERT_NO_FATAL_FAILURE(
4863 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004864
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4866 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4867 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4868}
4869
4870TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004872 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873
4874 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4875 mFakePointerController->setPosition(100, 200);
4876 mFakePointerController->setButtonState(0);
4877
4878 NotifyMotionArgs args;
4879
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004884 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4885 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4887 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 +01004888 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004889}
4890
4891TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004892 addConfigurationProperty("cursor.mode", "pointer");
4893 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004894 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004895
4896 NotifyDeviceResetArgs resetArgs;
4897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4898 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4899 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4900
4901 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4902 mFakePointerController->setPosition(100, 200);
4903 mFakePointerController->setButtonState(0);
4904
4905 NotifyMotionArgs args;
4906
4907 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4909 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4910 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4912 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4915 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 +01004916 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004917
4918 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4922 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4925 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4927 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4928 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4930 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4931
4932 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004933 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4934 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4936 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4937 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4939 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4941 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4942 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4944 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4945
4946 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4951 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4954 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 +01004955 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004956
4957 // Disable pointer capture and check that the device generation got bumped
4958 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004959 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004960 mFakePolicy->setPointerCapture(false);
4961 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004962 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004963
4964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004965 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4966
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4971 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4974 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 +01004975 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976}
4977
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004978/**
4979 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4980 * pointer acceleration or speed processing should not be applied.
4981 */
4982TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4983 addConfigurationProperty("cursor.mode", "pointer");
4984 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4985 100.f /*high threshold*/, 10.f /*acceleration*/);
4986 mFakePolicy->setVelocityControlParams(testParams);
4987 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4988
4989 NotifyDeviceResetArgs resetArgs;
4990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4991 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4992 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4993
4994 NotifyMotionArgs args;
4995
4996 // Move and verify scale is applied.
4997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5001 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5002 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5003 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5004 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5005 ASSERT_GT(relX, 10);
5006 ASSERT_GT(relY, 20);
5007
5008 // Enable Pointer Capture
5009 mFakePolicy->setPointerCapture(true);
5010 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5011 NotifyPointerCaptureChangedArgs captureArgs;
5012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5013 ASSERT_TRUE(captureArgs.request.enable);
5014
5015 // Move and verify scale is not applied.
5016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5020 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5022 ASSERT_EQ(10, args.pointerCoords[0].getX());
5023 ASSERT_EQ(20, args.pointerCoords[0].getY());
5024}
5025
Prabir Pradhan258e2b92022-06-24 18:37:04 +00005026TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5027 addConfigurationProperty("cursor.mode", "pointer");
5028 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5029
5030 NotifyDeviceResetArgs resetArgs;
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5032 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5033 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5034
5035 // Ensure the display is rotated.
5036 prepareDisplay(DISPLAY_ORIENTATION_90);
5037
5038 NotifyMotionArgs args;
5039
5040 // Verify that the coordinates are rotated.
5041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5045 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5046 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5047 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5048 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5049
5050 // Enable Pointer Capture.
5051 mFakePolicy->setPointerCapture(true);
5052 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5053 NotifyPointerCaptureChangedArgs captureArgs;
5054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5055 ASSERT_TRUE(captureArgs.request.enable);
5056
5057 // Move and verify rotation is not applied.
5058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5062 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5064 ASSERT_EQ(10, args.pointerCoords[0].getX());
5065 ASSERT_EQ(20, args.pointerCoords[0].getY());
5066}
5067
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005068TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005069 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005070
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005071 // Set up the default display.
5072 prepareDisplay(DISPLAY_ORIENTATION_90);
5073
5074 // Set up the secondary display as the display on which the pointer should be shown.
5075 // The InputDevice is not associated with any display.
5076 prepareSecondaryDisplay();
5077 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005078 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5079
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005080 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005081 mFakePointerController->setPosition(100, 200);
5082 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005083
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005084 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5089 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5090 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005091 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005092}
5093
5094TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5095 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5096
5097 // Set up the default display.
5098 prepareDisplay(DISPLAY_ORIENTATION_90);
5099
5100 // Set up the secondary display as the display on which the pointer should be shown,
5101 // and associate the InputDevice with the secondary display.
5102 prepareSecondaryDisplay();
5103 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5104 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5105 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5106
5107 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5108 mFakePointerController->setPosition(100, 200);
5109 mFakePointerController->setButtonState(0);
5110
5111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5115 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5116 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
5117 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5118}
5119
5120TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5121 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5122
5123 // Set up the default display as the display on which the pointer should be shown.
5124 prepareDisplay(DISPLAY_ORIENTATION_90);
5125 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5126
5127 // Associate the InputDevice with the secondary display.
5128 prepareSecondaryDisplay();
5129 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5130 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5131
5132 // The mapper should not generate any events because it is associated with a display that is
5133 // different from the pointer display.
5134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005138}
5139
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140// --- TouchInputMapperTest ---
5141
5142class TouchInputMapperTest : public InputMapperTest {
5143protected:
5144 static const int32_t RAW_X_MIN;
5145 static const int32_t RAW_X_MAX;
5146 static const int32_t RAW_Y_MIN;
5147 static const int32_t RAW_Y_MAX;
5148 static const int32_t RAW_TOUCH_MIN;
5149 static const int32_t RAW_TOUCH_MAX;
5150 static const int32_t RAW_TOOL_MIN;
5151 static const int32_t RAW_TOOL_MAX;
5152 static const int32_t RAW_PRESSURE_MIN;
5153 static const int32_t RAW_PRESSURE_MAX;
5154 static const int32_t RAW_ORIENTATION_MIN;
5155 static const int32_t RAW_ORIENTATION_MAX;
5156 static const int32_t RAW_DISTANCE_MIN;
5157 static const int32_t RAW_DISTANCE_MAX;
5158 static const int32_t RAW_TILT_MIN;
5159 static const int32_t RAW_TILT_MAX;
5160 static const int32_t RAW_ID_MIN;
5161 static const int32_t RAW_ID_MAX;
5162 static const int32_t RAW_SLOT_MIN;
5163 static const int32_t RAW_SLOT_MAX;
5164 static const float X_PRECISION;
5165 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005166 static const float X_PRECISION_VIRTUAL;
5167 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168
5169 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005170 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171
5172 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5173
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005174 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005175 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005176
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177 enum Axes {
5178 POSITION = 1 << 0,
5179 TOUCH = 1 << 1,
5180 TOOL = 1 << 2,
5181 PRESSURE = 1 << 3,
5182 ORIENTATION = 1 << 4,
5183 MINOR = 1 << 5,
5184 ID = 1 << 6,
5185 DISTANCE = 1 << 7,
5186 TILT = 1 << 8,
5187 SLOT = 1 << 9,
5188 TOOL_TYPE = 1 << 10,
5189 };
5190
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005191 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5192 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005193 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005195 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196 int32_t toRawX(float displayX);
5197 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005198 int32_t toRotatedRawX(float displayX);
5199 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005200 float toCookedX(float rawX, float rawY);
5201 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005202 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005203 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005205 float toDisplayY(int32_t rawY, int32_t displayHeight);
5206
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207};
5208
5209const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5210const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5211const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5212const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5213const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5214const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5215const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5216const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005217const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5218const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005219const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5220const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5221const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5222const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5223const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5224const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5225const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5226const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5227const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5228const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5229const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5230const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005231const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5232 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5233const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5234 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005235const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5236 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005237
5238const float TouchInputMapperTest::GEOMETRIC_SCALE =
5239 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5240 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5241
5242const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5243 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5244 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5245};
5246
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005247void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005248 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5249 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005250}
5251
5252void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5253 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5254 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255}
5256
Santos Cordonfa5cf462017-04-05 10:37:00 -07005257void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005258 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5259 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5260 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005261}
5262
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005264 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5265 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5266 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5267 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268}
5269
Jason Gerecke489fda82012-09-07 17:19:40 -07005270void TouchInputMapperTest::prepareLocationCalibration() {
5271 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5272}
5273
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274int32_t TouchInputMapperTest::toRawX(float displayX) {
5275 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5276}
5277
5278int32_t TouchInputMapperTest::toRawY(float displayY) {
5279 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5280}
5281
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005282int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5283 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5284}
5285
5286int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5287 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5288}
5289
Jason Gerecke489fda82012-09-07 17:19:40 -07005290float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5291 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5292 return rawX;
5293}
5294
5295float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5296 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5297 return rawY;
5298}
5299
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005301 return toDisplayX(rawX, DISPLAY_WIDTH);
5302}
5303
5304float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5305 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306}
5307
5308float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005309 return toDisplayY(rawY, DISPLAY_HEIGHT);
5310}
5311
5312float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5313 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314}
5315
5316
5317// --- SingleTouchInputMapperTest ---
5318
5319class SingleTouchInputMapperTest : public TouchInputMapperTest {
5320protected:
5321 void prepareButtons();
5322 void prepareAxes(int axes);
5323
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005324 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5325 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5326 void processUp(SingleTouchInputMapper& mappery);
5327 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5328 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5329 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5330 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5331 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5332 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333};
5334
5335void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005336 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337}
5338
5339void SingleTouchInputMapperTest::prepareAxes(int axes) {
5340 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005341 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5342 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343 }
5344 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005345 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5346 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347 }
5348 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005349 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5350 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351 }
5352 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005353 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5354 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355 }
5356 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005357 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5358 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359 }
5360}
5361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005362void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366}
5367
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005368void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005371}
5372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005373void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375}
5376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005377void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379}
5380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5382 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384}
5385
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005386void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388}
5389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005390void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5391 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5393 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394}
5395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005396void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5397 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399}
5400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005401void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403}
5404
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 prepareButtons();
5407 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005408 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005410 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411}
5412
5413TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005414 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5415 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 prepareButtons();
5417 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005418 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005420 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421}
5422
5423TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424 prepareButtons();
5425 prepareAxes(POSITION);
5426 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005429 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430}
5431
5432TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433 prepareButtons();
5434 prepareAxes(POSITION);
5435 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005436 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005438 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439}
5440
5441TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442 addConfigurationProperty("touch.deviceType", "touchScreen");
5443 prepareDisplay(DISPLAY_ORIENTATION_0);
5444 prepareButtons();
5445 prepareAxes(POSITION);
5446 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005447 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448
5449 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005450 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451
5452 // Virtual key is down.
5453 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5454 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5455 processDown(mapper, x, y);
5456 processSync(mapper);
5457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5458
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005459 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460
5461 // Virtual key is up.
5462 processUp(mapper);
5463 processSync(mapper);
5464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005466 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467}
5468
5469TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470 addConfigurationProperty("touch.deviceType", "touchScreen");
5471 prepareDisplay(DISPLAY_ORIENTATION_0);
5472 prepareButtons();
5473 prepareAxes(POSITION);
5474 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005475 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476
5477 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005478 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005479
5480 // Virtual key is down.
5481 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5482 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5483 processDown(mapper, x, y);
5484 processSync(mapper);
5485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5486
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005487 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005488
5489 // Virtual key is up.
5490 processUp(mapper);
5491 processSync(mapper);
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5493
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005494 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495}
5496
5497TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 addConfigurationProperty("touch.deviceType", "touchScreen");
5499 prepareDisplay(DISPLAY_ORIENTATION_0);
5500 prepareButtons();
5501 prepareAxes(POSITION);
5502 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005503 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504
5505 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5506 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005507 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 ASSERT_TRUE(flags[0]);
5509 ASSERT_FALSE(flags[1]);
5510}
5511
5512TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513 addConfigurationProperty("touch.deviceType", "touchScreen");
5514 prepareDisplay(DISPLAY_ORIENTATION_0);
5515 prepareButtons();
5516 prepareAxes(POSITION);
5517 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005518 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519
arthurhungdcef2dc2020-08-11 14:47:50 +08005520 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005521
5522 NotifyKeyArgs args;
5523
5524 // Press virtual key.
5525 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5526 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5527 processDown(mapper, x, y);
5528 processSync(mapper);
5529
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5531 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5532 ASSERT_EQ(DEVICE_ID, args.deviceId);
5533 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5534 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5535 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5536 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5537 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5538 ASSERT_EQ(KEY_HOME, args.scanCode);
5539 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5540 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5541
5542 // Release virtual key.
5543 processUp(mapper);
5544 processSync(mapper);
5545
5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5547 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5548 ASSERT_EQ(DEVICE_ID, args.deviceId);
5549 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5550 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5551 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5552 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5553 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5554 ASSERT_EQ(KEY_HOME, args.scanCode);
5555 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5556 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5557
5558 // Should not have sent any motions.
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5560}
5561
5562TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 addConfigurationProperty("touch.deviceType", "touchScreen");
5564 prepareDisplay(DISPLAY_ORIENTATION_0);
5565 prepareButtons();
5566 prepareAxes(POSITION);
5567 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005568 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569
arthurhungdcef2dc2020-08-11 14:47:50 +08005570 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571
5572 NotifyKeyArgs keyArgs;
5573
5574 // Press virtual key.
5575 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5576 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5577 processDown(mapper, x, y);
5578 processSync(mapper);
5579
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5581 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5582 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5583 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5584 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5585 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5586 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5587 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5588 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5590 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5591
5592 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5593 // into the display area.
5594 y -= 100;
5595 processMove(mapper, x, y);
5596 processSync(mapper);
5597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5599 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5600 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5601 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5602 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5603 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5604 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5605 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5606 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5607 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5608 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5609 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5610
5611 NotifyMotionArgs motionArgs;
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5613 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5614 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5615 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5616 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5618 ASSERT_EQ(0, motionArgs.flags);
5619 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5620 ASSERT_EQ(0, motionArgs.buttonState);
5621 ASSERT_EQ(0, motionArgs.edgeFlags);
5622 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5626 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5627 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5628 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5629 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5630
5631 // Keep moving out of bounds. Should generate a pointer move.
5632 y -= 50;
5633 processMove(mapper, x, y);
5634 processSync(mapper);
5635
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5638 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5639 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5640 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5642 ASSERT_EQ(0, motionArgs.flags);
5643 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5644 ASSERT_EQ(0, motionArgs.buttonState);
5645 ASSERT_EQ(0, motionArgs.edgeFlags);
5646 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5650 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5651 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5652 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5653 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5654
5655 // Release out of bounds. Should generate a pointer up.
5656 processUp(mapper);
5657 processSync(mapper);
5658
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5660 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5661 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5662 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5663 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5664 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5665 ASSERT_EQ(0, motionArgs.flags);
5666 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5667 ASSERT_EQ(0, motionArgs.buttonState);
5668 ASSERT_EQ(0, motionArgs.edgeFlags);
5669 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5673 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5674 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5675 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5676 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5677
5678 // Should not have sent any more keys or motions.
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5681}
5682
5683TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684 addConfigurationProperty("touch.deviceType", "touchScreen");
5685 prepareDisplay(DISPLAY_ORIENTATION_0);
5686 prepareButtons();
5687 prepareAxes(POSITION);
5688 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005689 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690
arthurhungdcef2dc2020-08-11 14:47:50 +08005691 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692
5693 NotifyMotionArgs motionArgs;
5694
5695 // Initially go down out of bounds.
5696 int32_t x = -10;
5697 int32_t y = -10;
5698 processDown(mapper, x, y);
5699 processSync(mapper);
5700
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5702
5703 // Move into the display area. Should generate a pointer down.
5704 x = 50;
5705 y = 75;
5706 processMove(mapper, x, y);
5707 processSync(mapper);
5708
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5710 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5711 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5712 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5713 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5714 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5715 ASSERT_EQ(0, motionArgs.flags);
5716 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5717 ASSERT_EQ(0, motionArgs.buttonState);
5718 ASSERT_EQ(0, motionArgs.edgeFlags);
5719 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5720 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5723 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5724 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5725 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5726 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5727
5728 // Release. Should generate a pointer up.
5729 processUp(mapper);
5730 processSync(mapper);
5731
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5733 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5734 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5735 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5736 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5737 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5738 ASSERT_EQ(0, motionArgs.flags);
5739 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5740 ASSERT_EQ(0, motionArgs.buttonState);
5741 ASSERT_EQ(0, motionArgs.edgeFlags);
5742 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5743 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5746 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5747 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5748 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5749 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5750
5751 // Should not have sent any more keys or motions.
5752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5754}
5755
Santos Cordonfa5cf462017-04-05 10:37:00 -07005756TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005757 addConfigurationProperty("touch.deviceType", "touchScreen");
5758 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5759
5760 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5761 prepareButtons();
5762 prepareAxes(POSITION);
5763 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005764 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005765
arthurhungdcef2dc2020-08-11 14:47:50 +08005766 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005767
5768 NotifyMotionArgs motionArgs;
5769
5770 // Down.
5771 int32_t x = 100;
5772 int32_t y = 125;
5773 processDown(mapper, x, y);
5774 processSync(mapper);
5775
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5777 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5778 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5779 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5780 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5781 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5782 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5783 ASSERT_EQ(0, motionArgs.flags);
5784 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5785 ASSERT_EQ(0, motionArgs.buttonState);
5786 ASSERT_EQ(0, motionArgs.edgeFlags);
5787 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5788 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5791 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5792 1, 0, 0, 0, 0, 0, 0, 0));
5793 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5794 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5795 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5796
5797 // Move.
5798 x += 50;
5799 y += 75;
5800 processMove(mapper, x, y);
5801 processSync(mapper);
5802
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5805 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5806 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5807 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5808 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5810 ASSERT_EQ(0, motionArgs.flags);
5811 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5812 ASSERT_EQ(0, motionArgs.buttonState);
5813 ASSERT_EQ(0, motionArgs.edgeFlags);
5814 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5815 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5818 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5819 1, 0, 0, 0, 0, 0, 0, 0));
5820 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5821 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5822 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5823
5824 // Up.
5825 processUp(mapper);
5826 processSync(mapper);
5827
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5830 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5831 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5832 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5833 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5835 ASSERT_EQ(0, motionArgs.flags);
5836 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5837 ASSERT_EQ(0, motionArgs.buttonState);
5838 ASSERT_EQ(0, motionArgs.edgeFlags);
5839 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5843 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5844 1, 0, 0, 0, 0, 0, 0, 0));
5845 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5846 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5847 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5848
5849 // Should not have sent any more keys or motions.
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5852}
5853
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005855 addConfigurationProperty("touch.deviceType", "touchScreen");
5856 prepareDisplay(DISPLAY_ORIENTATION_0);
5857 prepareButtons();
5858 prepareAxes(POSITION);
5859 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005860 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861
arthurhungdcef2dc2020-08-11 14:47:50 +08005862 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005863
5864 NotifyMotionArgs motionArgs;
5865
5866 // Down.
5867 int32_t x = 100;
5868 int32_t y = 125;
5869 processDown(mapper, x, y);
5870 processSync(mapper);
5871
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5874 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5875 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5876 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5877 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5878 ASSERT_EQ(0, motionArgs.flags);
5879 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5880 ASSERT_EQ(0, motionArgs.buttonState);
5881 ASSERT_EQ(0, motionArgs.edgeFlags);
5882 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5883 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5886 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5887 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5888 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5889 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5890
5891 // Move.
5892 x += 50;
5893 y += 75;
5894 processMove(mapper, x, y);
5895 processSync(mapper);
5896
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5898 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5899 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5900 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5901 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5903 ASSERT_EQ(0, motionArgs.flags);
5904 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5905 ASSERT_EQ(0, motionArgs.buttonState);
5906 ASSERT_EQ(0, motionArgs.edgeFlags);
5907 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5908 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5911 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5912 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5913 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5914 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5915
5916 // Up.
5917 processUp(mapper);
5918 processSync(mapper);
5919
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5922 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5923 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5924 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5925 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5926 ASSERT_EQ(0, motionArgs.flags);
5927 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5928 ASSERT_EQ(0, motionArgs.buttonState);
5929 ASSERT_EQ(0, motionArgs.edgeFlags);
5930 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5931 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5934 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5935 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5936 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5937 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5938
5939 // Should not have sent any more keys or motions.
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5942}
5943
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005944TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005945 addConfigurationProperty("touch.deviceType", "touchScreen");
5946 prepareButtons();
5947 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005948 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5949 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005950 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951
5952 NotifyMotionArgs args;
5953
5954 // Rotation 90.
5955 prepareDisplay(DISPLAY_ORIENTATION_90);
5956 processDown(mapper, toRawX(50), toRawY(75));
5957 processSync(mapper);
5958
5959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5960 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5961 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5962
5963 processUp(mapper);
5964 processSync(mapper);
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5966}
5967
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005968TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969 addConfigurationProperty("touch.deviceType", "touchScreen");
5970 prepareButtons();
5971 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005972 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5973 // orientation-aware are affected by display rotation.
5974 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005975 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976
5977 NotifyMotionArgs args;
5978
5979 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005980 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981 prepareDisplay(DISPLAY_ORIENTATION_0);
5982 processDown(mapper, toRawX(50), toRawY(75));
5983 processSync(mapper);
5984
5985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5986 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5987 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5988
5989 processUp(mapper);
5990 processSync(mapper);
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5992
5993 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005994 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005996 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997 processSync(mapper);
5998
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6000 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6001 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6002
6003 processUp(mapper);
6004 processSync(mapper);
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6006
6007 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006008 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009 prepareDisplay(DISPLAY_ORIENTATION_180);
6010 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6011 processSync(mapper);
6012
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6014 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6015 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6016
6017 processUp(mapper);
6018 processSync(mapper);
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6020
6021 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006022 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006024 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006025 processSync(mapper);
6026
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6028 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6029 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6030
6031 processUp(mapper);
6032 processSync(mapper);
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6034}
6035
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006036TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6037 addConfigurationProperty("touch.deviceType", "touchScreen");
6038 prepareButtons();
6039 prepareAxes(POSITION);
6040 addConfigurationProperty("touch.orientationAware", "1");
6041 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6042 clearViewports();
6043 prepareDisplay(DISPLAY_ORIENTATION_0);
6044 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6045 NotifyMotionArgs args;
6046
6047 // Orientation 0.
6048 processDown(mapper, toRawX(50), toRawY(75));
6049 processSync(mapper);
6050
6051 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6052 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6053 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6054
6055 processUp(mapper);
6056 processSync(mapper);
6057 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6058}
6059
6060TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6061 addConfigurationProperty("touch.deviceType", "touchScreen");
6062 prepareButtons();
6063 prepareAxes(POSITION);
6064 addConfigurationProperty("touch.orientationAware", "1");
6065 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6066 clearViewports();
6067 prepareDisplay(DISPLAY_ORIENTATION_0);
6068 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6069 NotifyMotionArgs args;
6070
6071 // Orientation 90.
6072 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6073 processSync(mapper);
6074
6075 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6076 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6077 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6078
6079 processUp(mapper);
6080 processSync(mapper);
6081 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6082}
6083
6084TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6085 addConfigurationProperty("touch.deviceType", "touchScreen");
6086 prepareButtons();
6087 prepareAxes(POSITION);
6088 addConfigurationProperty("touch.orientationAware", "1");
6089 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6090 clearViewports();
6091 prepareDisplay(DISPLAY_ORIENTATION_0);
6092 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6093 NotifyMotionArgs args;
6094
6095 // Orientation 180.
6096 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6097 processSync(mapper);
6098
6099 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6100 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6101 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6102
6103 processUp(mapper);
6104 processSync(mapper);
6105 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6106}
6107
6108TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6109 addConfigurationProperty("touch.deviceType", "touchScreen");
6110 prepareButtons();
6111 prepareAxes(POSITION);
6112 addConfigurationProperty("touch.orientationAware", "1");
6113 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6114 clearViewports();
6115 prepareDisplay(DISPLAY_ORIENTATION_0);
6116 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6117 NotifyMotionArgs args;
6118
6119 // Orientation 270.
6120 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6121 processSync(mapper);
6122
6123 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6124 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6125 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6126
6127 processUp(mapper);
6128 processSync(mapper);
6129 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6130}
6131
6132TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6133 addConfigurationProperty("touch.deviceType", "touchScreen");
6134 prepareButtons();
6135 prepareAxes(POSITION);
6136 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6137 // orientation-aware are affected by display rotation.
6138 addConfigurationProperty("touch.orientationAware", "0");
6139 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6140 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6141
6142 NotifyMotionArgs args;
6143
6144 // Orientation 90, Rotation 0.
6145 clearViewports();
6146 prepareDisplay(DISPLAY_ORIENTATION_0);
6147 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6148 processSync(mapper);
6149
6150 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6151 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6152 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6153
6154 processUp(mapper);
6155 processSync(mapper);
6156 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6157
6158 // Orientation 90, Rotation 90.
6159 clearViewports();
6160 prepareDisplay(DISPLAY_ORIENTATION_90);
6161 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6162 processSync(mapper);
6163
6164 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6165 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6166 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6167
6168 processUp(mapper);
6169 processSync(mapper);
6170 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6171
6172 // Orientation 90, Rotation 180.
6173 clearViewports();
6174 prepareDisplay(DISPLAY_ORIENTATION_180);
6175 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6176 processSync(mapper);
6177
6178 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6179 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6180 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6181
6182 processUp(mapper);
6183 processSync(mapper);
6184 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6185
6186 // Orientation 90, Rotation 270.
6187 clearViewports();
6188 prepareDisplay(DISPLAY_ORIENTATION_270);
6189 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6190 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6191 processSync(mapper);
6192
6193 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6194 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6195 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6196
6197 processUp(mapper);
6198 processSync(mapper);
6199 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6200}
6201
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203 addConfigurationProperty("touch.deviceType", "touchScreen");
6204 prepareDisplay(DISPLAY_ORIENTATION_0);
6205 prepareButtons();
6206 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006207 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208
6209 // These calculations are based on the input device calibration documentation.
6210 int32_t rawX = 100;
6211 int32_t rawY = 200;
6212 int32_t rawPressure = 10;
6213 int32_t rawToolMajor = 12;
6214 int32_t rawDistance = 2;
6215 int32_t rawTiltX = 30;
6216 int32_t rawTiltY = 110;
6217
6218 float x = toDisplayX(rawX);
6219 float y = toDisplayY(rawY);
6220 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6221 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6222 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6223 float distance = float(rawDistance);
6224
6225 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6226 float tiltScale = M_PI / 180;
6227 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6228 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6229 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6230 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6231
6232 processDown(mapper, rawX, rawY);
6233 processPressure(mapper, rawPressure);
6234 processToolMajor(mapper, rawToolMajor);
6235 processDistance(mapper, rawDistance);
6236 processTilt(mapper, rawTiltX, rawTiltY);
6237 processSync(mapper);
6238
6239 NotifyMotionArgs args;
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6242 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6243 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6244}
6245
Jason Gerecke489fda82012-09-07 17:19:40 -07006246TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006247 addConfigurationProperty("touch.deviceType", "touchScreen");
6248 prepareDisplay(DISPLAY_ORIENTATION_0);
6249 prepareLocationCalibration();
6250 prepareButtons();
6251 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006252 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006253
6254 int32_t rawX = 100;
6255 int32_t rawY = 200;
6256
6257 float x = toDisplayX(toCookedX(rawX, rawY));
6258 float y = toDisplayY(toCookedY(rawX, rawY));
6259
6260 processDown(mapper, rawX, rawY);
6261 processSync(mapper);
6262
6263 NotifyMotionArgs args;
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6266 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6267}
6268
Michael Wrightd02c5b62014-02-10 15:10:22 -08006269TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006270 addConfigurationProperty("touch.deviceType", "touchScreen");
6271 prepareDisplay(DISPLAY_ORIENTATION_0);
6272 prepareButtons();
6273 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006274 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275
6276 NotifyMotionArgs motionArgs;
6277 NotifyKeyArgs keyArgs;
6278
6279 processDown(mapper, 100, 200);
6280 processSync(mapper);
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6282 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6283 ASSERT_EQ(0, motionArgs.buttonState);
6284
6285 // press BTN_LEFT, release BTN_LEFT
6286 processKey(mapper, BTN_LEFT, 1);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6290 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6291
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6294 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6295
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296 processKey(mapper, BTN_LEFT, 0);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305
6306 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6307 processKey(mapper, BTN_RIGHT, 1);
6308 processKey(mapper, BTN_MIDDLE, 1);
6309 processSync(mapper);
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6312 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6313 motionArgs.buttonState);
6314
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6317 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6318
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6320 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6321 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6322 motionArgs.buttonState);
6323
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324 processKey(mapper, BTN_RIGHT, 0);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006332 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333
6334 processKey(mapper, BTN_MIDDLE, 0);
6335 processSync(mapper);
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343
6344 // press BTN_BACK, release BTN_BACK
6345 processKey(mapper, BTN_BACK, 1);
6346 processSync(mapper);
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6348 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6349 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006350
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006353 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6354
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6356 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6357 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358
6359 processKey(mapper, BTN_BACK, 0);
6360 processSync(mapper);
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006363 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006364
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006367 ASSERT_EQ(0, motionArgs.buttonState);
6368
Michael Wrightd02c5b62014-02-10 15:10:22 -08006369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6370 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6371 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6372
6373 // press BTN_SIDE, release BTN_SIDE
6374 processKey(mapper, BTN_SIDE, 1);
6375 processSync(mapper);
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6377 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6378 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006379
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006382 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6383
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6386 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387
6388 processKey(mapper, BTN_SIDE, 0);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006393
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006396 ASSERT_EQ(0, motionArgs.buttonState);
6397
Michael Wrightd02c5b62014-02-10 15:10:22 -08006398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6399 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6400 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6401
6402 // press BTN_FORWARD, release BTN_FORWARD
6403 processKey(mapper, BTN_FORWARD, 1);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6406 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6407 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006408
Michael Wrightd02c5b62014-02-10 15:10:22 -08006409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006411 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6412
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6415 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416
6417 processKey(mapper, BTN_FORWARD, 0);
6418 processSync(mapper);
6419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006422
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006425 ASSERT_EQ(0, motionArgs.buttonState);
6426
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6428 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6429 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6430
6431 // press BTN_EXTRA, release BTN_EXTRA
6432 processKey(mapper, BTN_EXTRA, 1);
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6435 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6436 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006437
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006440 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6441
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6444 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006445
6446 processKey(mapper, BTN_EXTRA, 0);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006451
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006454 ASSERT_EQ(0, motionArgs.buttonState);
6455
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6457 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6458 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6459
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6461
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462 // press BTN_STYLUS, release BTN_STYLUS
6463 processKey(mapper, BTN_STYLUS, 1);
6464 processSync(mapper);
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006467 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6468
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6471 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472
6473 processKey(mapper, BTN_STYLUS, 0);
6474 processSync(mapper);
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006477 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006478
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006481 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482
6483 // press BTN_STYLUS2, release BTN_STYLUS2
6484 processKey(mapper, BTN_STYLUS2, 1);
6485 processSync(mapper);
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006488 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6489
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6492 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493
6494 processKey(mapper, BTN_STYLUS2, 0);
6495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006499
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006502 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006503
6504 // release touch
6505 processUp(mapper);
6506 processSync(mapper);
6507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6508 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6509 ASSERT_EQ(0, motionArgs.buttonState);
6510}
6511
6512TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006513 addConfigurationProperty("touch.deviceType", "touchScreen");
6514 prepareDisplay(DISPLAY_ORIENTATION_0);
6515 prepareButtons();
6516 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006517 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006518
6519 NotifyMotionArgs motionArgs;
6520
6521 // default tool type is finger
6522 processDown(mapper, 100, 200);
6523 processSync(mapper);
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6527
6528 // eraser
6529 processKey(mapper, BTN_TOOL_RUBBER, 1);
6530 processSync(mapper);
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6534
6535 // stylus
6536 processKey(mapper, BTN_TOOL_RUBBER, 0);
6537 processKey(mapper, BTN_TOOL_PEN, 1);
6538 processSync(mapper);
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6542
6543 // brush
6544 processKey(mapper, BTN_TOOL_PEN, 0);
6545 processKey(mapper, BTN_TOOL_BRUSH, 1);
6546 processSync(mapper);
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6550
6551 // pencil
6552 processKey(mapper, BTN_TOOL_BRUSH, 0);
6553 processKey(mapper, BTN_TOOL_PENCIL, 1);
6554 processSync(mapper);
6555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6558
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006559 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006560 processKey(mapper, BTN_TOOL_PENCIL, 0);
6561 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6562 processSync(mapper);
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6566
6567 // mouse
6568 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6569 processKey(mapper, BTN_TOOL_MOUSE, 1);
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6574
6575 // lens
6576 processKey(mapper, BTN_TOOL_MOUSE, 0);
6577 processKey(mapper, BTN_TOOL_LENS, 1);
6578 processSync(mapper);
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6582
6583 // double-tap
6584 processKey(mapper, BTN_TOOL_LENS, 0);
6585 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6586 processSync(mapper);
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6590
6591 // triple-tap
6592 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6593 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6594 processSync(mapper);
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6598
6599 // quad-tap
6600 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6601 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6606
6607 // finger
6608 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6609 processKey(mapper, BTN_TOOL_FINGER, 1);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6614
6615 // stylus trumps finger
6616 processKey(mapper, BTN_TOOL_PEN, 1);
6617 processSync(mapper);
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6621
6622 // eraser trumps stylus
6623 processKey(mapper, BTN_TOOL_RUBBER, 1);
6624 processSync(mapper);
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6628
6629 // mouse trumps eraser
6630 processKey(mapper, BTN_TOOL_MOUSE, 1);
6631 processSync(mapper);
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6635
6636 // back to default tool type
6637 processKey(mapper, BTN_TOOL_MOUSE, 0);
6638 processKey(mapper, BTN_TOOL_RUBBER, 0);
6639 processKey(mapper, BTN_TOOL_PEN, 0);
6640 processKey(mapper, BTN_TOOL_FINGER, 0);
6641 processSync(mapper);
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6645}
6646
6647TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006648 addConfigurationProperty("touch.deviceType", "touchScreen");
6649 prepareDisplay(DISPLAY_ORIENTATION_0);
6650 prepareButtons();
6651 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006652 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006653 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006654
6655 NotifyMotionArgs motionArgs;
6656
6657 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6658 processKey(mapper, BTN_TOOL_FINGER, 1);
6659 processMove(mapper, 100, 200);
6660 processSync(mapper);
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6662 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6664 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6665
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6669 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6670
6671 // move a little
6672 processMove(mapper, 150, 250);
6673 processSync(mapper);
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6678
6679 // down when BTN_TOUCH is pressed, pressure defaults to 1
6680 processKey(mapper, BTN_TOUCH, 1);
6681 processSync(mapper);
6682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6683 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6685 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6686
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6688 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6690 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6691
6692 // up when BTN_TOUCH is released, hover restored
6693 processKey(mapper, BTN_TOUCH, 0);
6694 processSync(mapper);
6695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6696 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6698 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6699
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6703 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6704
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6706 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6708 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6709
6710 // exit hover when pointer goes away
6711 processKey(mapper, BTN_TOOL_FINGER, 0);
6712 processSync(mapper);
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6716 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6717}
6718
6719TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006720 addConfigurationProperty("touch.deviceType", "touchScreen");
6721 prepareDisplay(DISPLAY_ORIENTATION_0);
6722 prepareButtons();
6723 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006724 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006725
6726 NotifyMotionArgs motionArgs;
6727
6728 // initially hovering because pressure is 0
6729 processDown(mapper, 100, 200);
6730 processPressure(mapper, 0);
6731 processSync(mapper);
6732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6735 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6736
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6738 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6740 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6741
6742 // move a little
6743 processMove(mapper, 150, 250);
6744 processSync(mapper);
6745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6746 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6748 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6749
6750 // down when pressure is non-zero
6751 processPressure(mapper, RAW_PRESSURE_MAX);
6752 processSync(mapper);
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6754 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6756 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6757
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6759 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6761 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6762
6763 // up when pressure becomes 0, hover restored
6764 processPressure(mapper, 0);
6765 processSync(mapper);
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6769 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6770
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6772 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6774 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6775
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6777 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6779 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6780
6781 // exit hover when pointer goes away
6782 processUp(mapper);
6783 processSync(mapper);
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6785 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6787 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6788}
6789
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006790TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6791 addConfigurationProperty("touch.deviceType", "touchScreen");
6792 prepareDisplay(DISPLAY_ORIENTATION_0);
6793 prepareButtons();
6794 prepareAxes(POSITION | PRESSURE);
6795 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6796 NotifyMotionArgs motionArgs;
6797
6798 // Set the initial state for the touch pointer.
6799 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6800 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6801 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6802 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6803
6804 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
6805 // state by reading the current axis values.
6806 mapper.reset(ARBITRARY_TIME);
6807
6808 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6809 // the recreated touch state to generate a down event.
6810 processSync(mapper);
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6812 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6813
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6815}
6816
lilinnan687e58f2022-07-19 16:00:50 +08006817TEST_F(SingleTouchInputMapperTest,
6818 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6819 addConfigurationProperty("touch.deviceType", "touchScreen");
6820 prepareDisplay(DISPLAY_ORIENTATION_0);
6821 prepareButtons();
6822 prepareAxes(POSITION);
6823 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6824 NotifyMotionArgs motionArgs;
6825
6826 // Down.
6827 int32_t x = 100;
6828 int32_t y = 200;
6829 processDown(mapper, x, y);
6830 processSync(mapper);
6831
6832 // We should receive a down event
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6835
6836 // Change display id
6837 clearViewports();
6838 prepareSecondaryDisplay(ViewportType::INTERNAL);
6839
6840 // We should receive a cancel event
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6842 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6843 // Then receive reset called
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6845}
6846
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006847TEST_F(SingleTouchInputMapperTest,
6848 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6849 addConfigurationProperty("touch.deviceType", "touchScreen");
6850 prepareDisplay(DISPLAY_ORIENTATION_0);
6851 prepareButtons();
6852 prepareAxes(POSITION);
6853 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6855 NotifyMotionArgs motionArgs;
6856
6857 // Start a new gesture.
6858 processDown(mapper, 100, 200);
6859 processSync(mapper);
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6862
6863 // Make the viewport inactive. This will put the device in disabled mode.
6864 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6865 viewport->isActive = false;
6866 mFakePolicy->updateViewport(*viewport);
6867 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6868
6869 // We should receive a cancel event for the ongoing gesture.
6870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6871 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6872 // Then we should be notified that the device was reset.
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6874
6875 // No events are generated while the viewport is inactive.
6876 processMove(mapper, 101, 201);
6877 processSync(mapper);
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006878 processUp(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006879 processSync(mapper);
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6881
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006882 // Start a new gesture while the viewport is still inactive.
6883 processDown(mapper, 300, 400);
6884 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6885 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6886 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6887 processSync(mapper);
6888
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006889 // Make the viewport active again. The device should resume processing events.
6890 viewport->isActive = true;
6891 mFakePolicy->updateViewport(*viewport);
6892 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6893
6894 // The device is reset because it changes back to direct mode, without generating any events.
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6897
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006898 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006899 processSync(mapper);
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6901 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6902
6903 // No more events.
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6906}
6907
Prabir Pradhan5632d622021-09-06 07:57:20 -07006908// --- TouchDisplayProjectionTest ---
6909
6910class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6911public:
6912 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6913 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6914 // rotated equivalent of the given un-rotated physical display bounds.
6915 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6916 uint32_t inverseRotationFlags;
6917 auto width = DISPLAY_WIDTH;
6918 auto height = DISPLAY_HEIGHT;
6919 switch (orientation) {
6920 case DISPLAY_ORIENTATION_90:
6921 inverseRotationFlags = ui::Transform::ROT_270;
6922 std::swap(width, height);
6923 break;
6924 case DISPLAY_ORIENTATION_180:
6925 inverseRotationFlags = ui::Transform::ROT_180;
6926 break;
6927 case DISPLAY_ORIENTATION_270:
6928 inverseRotationFlags = ui::Transform::ROT_90;
6929 std::swap(width, height);
6930 break;
6931 case DISPLAY_ORIENTATION_0:
6932 inverseRotationFlags = ui::Transform::ROT_0;
6933 break;
6934 default:
6935 FAIL() << "Invalid orientation: " << orientation;
6936 }
6937
6938 const ui::Transform rotation(inverseRotationFlags, width, height);
6939 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6940
6941 std::optional<DisplayViewport> internalViewport =
6942 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6943 DisplayViewport& v = *internalViewport;
6944 v.displayId = DISPLAY_ID;
6945 v.orientation = orientation;
6946
6947 v.logicalLeft = 0;
6948 v.logicalTop = 0;
6949 v.logicalRight = 100;
6950 v.logicalBottom = 100;
6951
6952 v.physicalLeft = rotatedPhysicalDisplay.left;
6953 v.physicalTop = rotatedPhysicalDisplay.top;
6954 v.physicalRight = rotatedPhysicalDisplay.right;
6955 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6956
6957 v.deviceWidth = width;
6958 v.deviceHeight = height;
6959
6960 v.isActive = true;
6961 v.uniqueId = UNIQUE_ID;
6962 v.type = ViewportType::INTERNAL;
6963 mFakePolicy->updateViewport(v);
6964 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6965 }
6966
6967 void assertReceivedMove(const Point& point) {
6968 NotifyMotionArgs motionArgs;
6969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6971 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6973 1, 0, 0, 0, 0, 0, 0, 0));
6974 }
6975};
6976
6977TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6978 addConfigurationProperty("touch.deviceType", "touchScreen");
6979 prepareDisplay(DISPLAY_ORIENTATION_0);
6980
6981 prepareButtons();
6982 prepareAxes(POSITION);
6983 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6984
6985 NotifyMotionArgs motionArgs;
6986
6987 // Configure the DisplayViewport such that the logical display maps to a subsection of
6988 // the display panel called the physical display. Here, the physical display is bounded by the
6989 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6990 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6991 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6992 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6993
6994 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6995 DISPLAY_ORIENTATION_270}) {
6996 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6997
6998 // Touches outside the physical display should be ignored, and should not generate any
6999 // events. Ensure touches at the following points that lie outside of the physical display
7000 // area do not generate any events.
7001 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7002 processDown(mapper, toRawX(point.x), toRawY(point.y));
7003 processSync(mapper);
7004 processUp(mapper);
7005 processSync(mapper);
7006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7007 << "Unexpected event generated for touch outside physical display at point: "
7008 << point.x << ", " << point.y;
7009 }
7010 }
7011}
7012
7013TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7014 addConfigurationProperty("touch.deviceType", "touchScreen");
7015 prepareDisplay(DISPLAY_ORIENTATION_0);
7016
7017 prepareButtons();
7018 prepareAxes(POSITION);
7019 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7020
7021 NotifyMotionArgs motionArgs;
7022
7023 // Configure the DisplayViewport such that the logical display maps to a subsection of
7024 // the display panel called the physical display. Here, the physical display is bounded by the
7025 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7026 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7027
7028 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7029 DISPLAY_ORIENTATION_270}) {
7030 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7031
7032 // Touches that start outside the physical display should be ignored until it enters the
7033 // physical display bounds, at which point it should generate a down event. Start a touch at
7034 // the point (5, 100), which is outside the physical display bounds.
7035 static const Point kOutsidePoint{5, 100};
7036 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7037 processSync(mapper);
7038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7039
7040 // Move the touch into the physical display area. This should generate a pointer down.
7041 processMove(mapper, toRawX(11), toRawY(21));
7042 processSync(mapper);
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7045 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7046 ASSERT_NO_FATAL_FAILURE(
7047 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7048
7049 // Move the touch inside the physical display area. This should generate a pointer move.
7050 processMove(mapper, toRawX(69), toRawY(159));
7051 processSync(mapper);
7052 assertReceivedMove({69, 159});
7053
7054 // Move outside the physical display area. Since the pointer is already down, this should
7055 // now continue generating events.
7056 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7057 processSync(mapper);
7058 assertReceivedMove(kOutsidePoint);
7059
7060 // Release. This should generate a pointer up.
7061 processUp(mapper);
7062 processSync(mapper);
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7064 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7066 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7067
7068 // Ensure no more events were generated.
7069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7071 }
7072}
7073
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074// --- MultiTouchInputMapperTest ---
7075
7076class MultiTouchInputMapperTest : public TouchInputMapperTest {
7077protected:
7078 void prepareAxes(int axes);
7079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007080 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7081 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7082 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7083 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7084 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7085 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7086 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7087 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7088 void processId(MultiTouchInputMapper& mapper, int32_t id);
7089 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7090 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7091 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7092 void processMTSync(MultiTouchInputMapper& mapper);
7093 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094};
7095
7096void MultiTouchInputMapperTest::prepareAxes(int axes) {
7097 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7099 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100 }
7101 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007102 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7103 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007104 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007105 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7106 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007107 }
7108 }
7109 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007110 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7111 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007113 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007114 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007115 }
7116 }
7117 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007118 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7119 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120 }
7121 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007122 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7123 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124 }
7125 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007126 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7127 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 }
7129 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7131 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132 }
7133 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007134 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7135 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007136 }
7137 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007138 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139 }
7140}
7141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007142void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7143 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146}
7147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007148void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7149 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151}
7152
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007153void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7154 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156}
7157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007158void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160}
7161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007162void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007164}
7165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007166void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7167 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169}
7170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007171void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173}
7174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007175void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177}
7178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007179void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181}
7182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007183void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007185}
7186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007187void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007189}
7190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007191void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7192 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194}
7195
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007196void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198}
7199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007200void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202}
7203
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007205 addConfigurationProperty("touch.deviceType", "touchScreen");
7206 prepareDisplay(DISPLAY_ORIENTATION_0);
7207 prepareAxes(POSITION);
7208 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007209 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210
arthurhungdcef2dc2020-08-11 14:47:50 +08007211 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007212
7213 NotifyMotionArgs motionArgs;
7214
7215 // Two fingers down at once.
7216 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7217 processPosition(mapper, x1, y1);
7218 processMTSync(mapper);
7219 processPosition(mapper, x2, y2);
7220 processMTSync(mapper);
7221 processSync(mapper);
7222
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7225 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7226 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7227 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7228 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7229 ASSERT_EQ(0, motionArgs.flags);
7230 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7231 ASSERT_EQ(0, motionArgs.buttonState);
7232 ASSERT_EQ(0, motionArgs.edgeFlags);
7233 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7234 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7237 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7238 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7239 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7241
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7243 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7244 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7245 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7246 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007247 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007248 ASSERT_EQ(0, motionArgs.flags);
7249 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7250 ASSERT_EQ(0, motionArgs.buttonState);
7251 ASSERT_EQ(0, motionArgs.edgeFlags);
7252 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7253 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7255 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7258 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7260 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7261 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7262 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7264
7265 // Move.
7266 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7267 processPosition(mapper, x1, y1);
7268 processMTSync(mapper);
7269 processPosition(mapper, x2, y2);
7270 processMTSync(mapper);
7271 processSync(mapper);
7272
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7274 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7275 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7276 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7277 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7279 ASSERT_EQ(0, motionArgs.flags);
7280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7281 ASSERT_EQ(0, motionArgs.buttonState);
7282 ASSERT_EQ(0, motionArgs.edgeFlags);
7283 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7286 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7289 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7291 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7292 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7293 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7295
7296 // First finger up.
7297 x2 += 15; y2 -= 20;
7298 processPosition(mapper, x2, y2);
7299 processMTSync(mapper);
7300 processSync(mapper);
7301
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7303 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7304 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7305 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7306 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007307 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007308 ASSERT_EQ(0, motionArgs.flags);
7309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7310 ASSERT_EQ(0, motionArgs.buttonState);
7311 ASSERT_EQ(0, motionArgs.edgeFlags);
7312 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7313 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7315 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7318 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7320 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7321 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7322 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7324
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7327 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7328 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7329 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7331 ASSERT_EQ(0, motionArgs.flags);
7332 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7333 ASSERT_EQ(0, motionArgs.buttonState);
7334 ASSERT_EQ(0, motionArgs.edgeFlags);
7335 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7336 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7339 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7340 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7341 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7343
7344 // Move.
7345 x2 += 20; y2 -= 25;
7346 processPosition(mapper, x2, y2);
7347 processMTSync(mapper);
7348 processSync(mapper);
7349
7350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7351 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7352 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7353 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7354 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7356 ASSERT_EQ(0, motionArgs.flags);
7357 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7358 ASSERT_EQ(0, motionArgs.buttonState);
7359 ASSERT_EQ(0, motionArgs.edgeFlags);
7360 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7361 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7364 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7365 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7366 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7367 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7368
7369 // New finger down.
7370 int32_t x3 = 700, y3 = 300;
7371 processPosition(mapper, x2, y2);
7372 processMTSync(mapper);
7373 processPosition(mapper, x3, y3);
7374 processMTSync(mapper);
7375 processSync(mapper);
7376
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7379 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7380 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7381 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007382 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007383 ASSERT_EQ(0, motionArgs.flags);
7384 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7385 ASSERT_EQ(0, motionArgs.buttonState);
7386 ASSERT_EQ(0, motionArgs.edgeFlags);
7387 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7388 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7390 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7393 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7395 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7396 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7397 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7399
7400 // Second finger up.
7401 x3 += 30; y3 -= 20;
7402 processPosition(mapper, x3, y3);
7403 processMTSync(mapper);
7404 processSync(mapper);
7405
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7407 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7408 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7409 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7410 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007411 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007412 ASSERT_EQ(0, motionArgs.flags);
7413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7414 ASSERT_EQ(0, motionArgs.buttonState);
7415 ASSERT_EQ(0, motionArgs.edgeFlags);
7416 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7419 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7422 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7424 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7425 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7426 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7427 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7428
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7430 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7431 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7432 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7433 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7435 ASSERT_EQ(0, motionArgs.flags);
7436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7437 ASSERT_EQ(0, motionArgs.buttonState);
7438 ASSERT_EQ(0, motionArgs.edgeFlags);
7439 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7443 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7444 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7445 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7447
7448 // Last finger up.
7449 processMTSync(mapper);
7450 processSync(mapper);
7451
7452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7453 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7454 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7455 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7456 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7457 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7458 ASSERT_EQ(0, motionArgs.flags);
7459 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7460 ASSERT_EQ(0, motionArgs.buttonState);
7461 ASSERT_EQ(0, motionArgs.edgeFlags);
7462 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7463 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7466 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7467 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7468 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7469 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7470
7471 // Should not have sent any more keys or motions.
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7474}
7475
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007476TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7477 addConfigurationProperty("touch.deviceType", "touchScreen");
7478 prepareDisplay(DISPLAY_ORIENTATION_0);
7479
7480 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7481 /*fuzz*/ 0, /*resolution*/ 10);
7482 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7483 /*fuzz*/ 0, /*resolution*/ 11);
7484 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7485 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7486 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7487 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7488 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7489 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7490 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7491 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7492
7493 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7494
7495 // X and Y axes
7496 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7497 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7498 // Touch major and minor
7499 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7500 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7501 // Tool major and minor
7502 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7503 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7504}
7505
7506TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7507 addConfigurationProperty("touch.deviceType", "touchScreen");
7508 prepareDisplay(DISPLAY_ORIENTATION_0);
7509
7510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7511 /*fuzz*/ 0, /*resolution*/ 10);
7512 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7513 /*fuzz*/ 0, /*resolution*/ 11);
7514
7515 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7516
7517 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7518
7519 // Touch major and minor
7520 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7521 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7522 // Tool major and minor
7523 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7524 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7525}
7526
Michael Wrightd02c5b62014-02-10 15:10:22 -08007527TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007528 addConfigurationProperty("touch.deviceType", "touchScreen");
7529 prepareDisplay(DISPLAY_ORIENTATION_0);
7530 prepareAxes(POSITION | ID);
7531 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007532 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533
arthurhungdcef2dc2020-08-11 14:47:50 +08007534 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007535
7536 NotifyMotionArgs motionArgs;
7537
7538 // Two fingers down at once.
7539 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7540 processPosition(mapper, x1, y1);
7541 processId(mapper, 1);
7542 processMTSync(mapper);
7543 processPosition(mapper, x2, y2);
7544 processId(mapper, 2);
7545 processMTSync(mapper);
7546 processSync(mapper);
7547
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7549 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7550 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7551 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7554 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7555
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007557 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007558 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7559 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7561 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7564 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7566 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7567
7568 // Move.
7569 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7570 processPosition(mapper, x1, y1);
7571 processId(mapper, 1);
7572 processMTSync(mapper);
7573 processPosition(mapper, x2, y2);
7574 processId(mapper, 2);
7575 processMTSync(mapper);
7576 processSync(mapper);
7577
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7580 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7583 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7586 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7588 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7589
7590 // First finger up.
7591 x2 += 15; y2 -= 20;
7592 processPosition(mapper, x2, y2);
7593 processId(mapper, 2);
7594 processMTSync(mapper);
7595 processSync(mapper);
7596
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007598 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007599 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7600 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7602 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7605 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7607 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7608
7609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7611 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7612 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7615 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7616
7617 // Move.
7618 x2 += 20; y2 -= 25;
7619 processPosition(mapper, x2, y2);
7620 processId(mapper, 2);
7621 processMTSync(mapper);
7622 processSync(mapper);
7623
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7626 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7627 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7630 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7631
7632 // New finger down.
7633 int32_t x3 = 700, y3 = 300;
7634 processPosition(mapper, x2, y2);
7635 processId(mapper, 2);
7636 processMTSync(mapper);
7637 processPosition(mapper, x3, y3);
7638 processId(mapper, 3);
7639 processMTSync(mapper);
7640 processSync(mapper);
7641
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007643 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007644 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7645 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7647 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7650 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7652 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7653
7654 // Second finger up.
7655 x3 += 30; y3 -= 20;
7656 processPosition(mapper, x3, y3);
7657 processId(mapper, 3);
7658 processMTSync(mapper);
7659 processSync(mapper);
7660
7661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007662 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007663 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7664 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7666 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7669 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7671 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7672
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7675 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7679 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7680
7681 // Last finger up.
7682 processMTSync(mapper);
7683 processSync(mapper);
7684
7685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7686 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7687 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7688 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7691 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7692
7693 // Should not have sent any more keys or motions.
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7696}
7697
7698TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007699 addConfigurationProperty("touch.deviceType", "touchScreen");
7700 prepareDisplay(DISPLAY_ORIENTATION_0);
7701 prepareAxes(POSITION | ID | SLOT);
7702 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007703 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007704
arthurhungdcef2dc2020-08-11 14:47:50 +08007705 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007706
7707 NotifyMotionArgs motionArgs;
7708
7709 // Two fingers down at once.
7710 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7711 processPosition(mapper, x1, y1);
7712 processId(mapper, 1);
7713 processSlot(mapper, 1);
7714 processPosition(mapper, x2, y2);
7715 processId(mapper, 2);
7716 processSync(mapper);
7717
7718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7719 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7720 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7724 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7725
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007727 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7729 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7731 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7734 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7736 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7737
7738 // Move.
7739 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7740 processSlot(mapper, 0);
7741 processPosition(mapper, x1, y1);
7742 processSlot(mapper, 1);
7743 processPosition(mapper, x2, y2);
7744 processSync(mapper);
7745
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7748 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7749 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7751 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7754 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7756 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7757
7758 // First finger up.
7759 x2 += 15; y2 -= 20;
7760 processSlot(mapper, 0);
7761 processId(mapper, -1);
7762 processSlot(mapper, 1);
7763 processPosition(mapper, x2, y2);
7764 processSync(mapper);
7765
7766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007767 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7769 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7771 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7774 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7776 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7777
7778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7780 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7781 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7784 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7785
7786 // Move.
7787 x2 += 20; y2 -= 25;
7788 processPosition(mapper, x2, y2);
7789 processSync(mapper);
7790
7791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7793 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7794 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7797 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7798
7799 // New finger down.
7800 int32_t x3 = 700, y3 = 300;
7801 processPosition(mapper, x2, y2);
7802 processSlot(mapper, 0);
7803 processId(mapper, 3);
7804 processPosition(mapper, x3, y3);
7805 processSync(mapper);
7806
7807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007808 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007809 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7810 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7812 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7815 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7817 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7818
7819 // Second finger up.
7820 x3 += 30; y3 -= 20;
7821 processSlot(mapper, 1);
7822 processId(mapper, -1);
7823 processSlot(mapper, 0);
7824 processPosition(mapper, x3, y3);
7825 processSync(mapper);
7826
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007828 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7830 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7832 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7833 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7835 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7837 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7838
7839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7841 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7842 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7845 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7846
7847 // Last finger up.
7848 processId(mapper, -1);
7849 processSync(mapper);
7850
7851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7852 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7853 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7854 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7857 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7858
7859 // Should not have sent any more keys or motions.
7860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7862}
7863
7864TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007865 addConfigurationProperty("touch.deviceType", "touchScreen");
7866 prepareDisplay(DISPLAY_ORIENTATION_0);
7867 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007868 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007869
7870 // These calculations are based on the input device calibration documentation.
7871 int32_t rawX = 100;
7872 int32_t rawY = 200;
7873 int32_t rawTouchMajor = 7;
7874 int32_t rawTouchMinor = 6;
7875 int32_t rawToolMajor = 9;
7876 int32_t rawToolMinor = 8;
7877 int32_t rawPressure = 11;
7878 int32_t rawDistance = 0;
7879 int32_t rawOrientation = 3;
7880 int32_t id = 5;
7881
7882 float x = toDisplayX(rawX);
7883 float y = toDisplayY(rawY);
7884 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7885 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7886 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7887 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7888 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7889 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7890 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7891 float distance = float(rawDistance);
7892
7893 processPosition(mapper, rawX, rawY);
7894 processTouchMajor(mapper, rawTouchMajor);
7895 processTouchMinor(mapper, rawTouchMinor);
7896 processToolMajor(mapper, rawToolMajor);
7897 processToolMinor(mapper, rawToolMinor);
7898 processPressure(mapper, rawPressure);
7899 processOrientation(mapper, rawOrientation);
7900 processDistance(mapper, rawDistance);
7901 processId(mapper, id);
7902 processMTSync(mapper);
7903 processSync(mapper);
7904
7905 NotifyMotionArgs args;
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7907 ASSERT_EQ(0, args.pointerProperties[0].id);
7908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7909 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7910 orientation, distance));
7911}
7912
7913TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007914 addConfigurationProperty("touch.deviceType", "touchScreen");
7915 prepareDisplay(DISPLAY_ORIENTATION_0);
7916 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7917 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007918 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007919
7920 // These calculations are based on the input device calibration documentation.
7921 int32_t rawX = 100;
7922 int32_t rawY = 200;
7923 int32_t rawTouchMajor = 140;
7924 int32_t rawTouchMinor = 120;
7925 int32_t rawToolMajor = 180;
7926 int32_t rawToolMinor = 160;
7927
7928 float x = toDisplayX(rawX);
7929 float y = toDisplayY(rawY);
7930 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7931 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7932 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7933 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7934 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7935
7936 processPosition(mapper, rawX, rawY);
7937 processTouchMajor(mapper, rawTouchMajor);
7938 processTouchMinor(mapper, rawTouchMinor);
7939 processToolMajor(mapper, rawToolMajor);
7940 processToolMinor(mapper, rawToolMinor);
7941 processMTSync(mapper);
7942 processSync(mapper);
7943
7944 NotifyMotionArgs args;
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7947 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7948}
7949
7950TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951 addConfigurationProperty("touch.deviceType", "touchScreen");
7952 prepareDisplay(DISPLAY_ORIENTATION_0);
7953 prepareAxes(POSITION | TOUCH | TOOL);
7954 addConfigurationProperty("touch.size.calibration", "diameter");
7955 addConfigurationProperty("touch.size.scale", "10");
7956 addConfigurationProperty("touch.size.bias", "160");
7957 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007958 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959
7960 // These calculations are based on the input device calibration documentation.
7961 // Note: We only provide a single common touch/tool value because the device is assumed
7962 // not to emit separate values for each pointer (isSummed = 1).
7963 int32_t rawX = 100;
7964 int32_t rawY = 200;
7965 int32_t rawX2 = 150;
7966 int32_t rawY2 = 250;
7967 int32_t rawTouchMajor = 5;
7968 int32_t rawToolMajor = 8;
7969
7970 float x = toDisplayX(rawX);
7971 float y = toDisplayY(rawY);
7972 float x2 = toDisplayX(rawX2);
7973 float y2 = toDisplayY(rawY2);
7974 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7975 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7976 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7977
7978 processPosition(mapper, rawX, rawY);
7979 processTouchMajor(mapper, rawTouchMajor);
7980 processToolMajor(mapper, rawToolMajor);
7981 processMTSync(mapper);
7982 processPosition(mapper, rawX2, rawY2);
7983 processTouchMajor(mapper, rawTouchMajor);
7984 processToolMajor(mapper, rawToolMajor);
7985 processMTSync(mapper);
7986 processSync(mapper);
7987
7988 NotifyMotionArgs args;
7989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7990 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7991
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007993 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007994 ASSERT_EQ(size_t(2), args.pointerCount);
7995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7996 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7998 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7999}
8000
8001TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008002 addConfigurationProperty("touch.deviceType", "touchScreen");
8003 prepareDisplay(DISPLAY_ORIENTATION_0);
8004 prepareAxes(POSITION | TOUCH | TOOL);
8005 addConfigurationProperty("touch.size.calibration", "area");
8006 addConfigurationProperty("touch.size.scale", "43");
8007 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008008 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008009
8010 // These calculations are based on the input device calibration documentation.
8011 int32_t rawX = 100;
8012 int32_t rawY = 200;
8013 int32_t rawTouchMajor = 5;
8014 int32_t rawToolMajor = 8;
8015
8016 float x = toDisplayX(rawX);
8017 float y = toDisplayY(rawY);
8018 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8019 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8020 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8021
8022 processPosition(mapper, rawX, rawY);
8023 processTouchMajor(mapper, rawTouchMajor);
8024 processToolMajor(mapper, rawToolMajor);
8025 processMTSync(mapper);
8026 processSync(mapper);
8027
8028 NotifyMotionArgs args;
8029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8031 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8032}
8033
8034TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008035 addConfigurationProperty("touch.deviceType", "touchScreen");
8036 prepareDisplay(DISPLAY_ORIENTATION_0);
8037 prepareAxes(POSITION | PRESSURE);
8038 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8039 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008040 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041
Michael Wrightaa449c92017-12-13 21:21:43 +00008042 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008043 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008044 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8045 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8046 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8047
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 // These calculations are based on the input device calibration documentation.
8049 int32_t rawX = 100;
8050 int32_t rawY = 200;
8051 int32_t rawPressure = 60;
8052
8053 float x = toDisplayX(rawX);
8054 float y = toDisplayY(rawY);
8055 float pressure = float(rawPressure) * 0.01f;
8056
8057 processPosition(mapper, rawX, rawY);
8058 processPressure(mapper, rawPressure);
8059 processMTSync(mapper);
8060 processSync(mapper);
8061
8062 NotifyMotionArgs args;
8063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8065 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8066}
8067
8068TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 addConfigurationProperty("touch.deviceType", "touchScreen");
8070 prepareDisplay(DISPLAY_ORIENTATION_0);
8071 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008072 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073
8074 NotifyMotionArgs motionArgs;
8075 NotifyKeyArgs keyArgs;
8076
8077 processId(mapper, 1);
8078 processPosition(mapper, 100, 200);
8079 processSync(mapper);
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8081 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8082 ASSERT_EQ(0, motionArgs.buttonState);
8083
8084 // press BTN_LEFT, release BTN_LEFT
8085 processKey(mapper, BTN_LEFT, 1);
8086 processSync(mapper);
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8089 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8090
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8093 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8094
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 processKey(mapper, BTN_LEFT, 0);
8096 processSync(mapper);
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008100
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008103 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104
8105 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8106 processKey(mapper, BTN_RIGHT, 1);
8107 processKey(mapper, BTN_MIDDLE, 1);
8108 processSync(mapper);
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8111 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8112 motionArgs.buttonState);
8113
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8116 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8117
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8120 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8121 motionArgs.buttonState);
8122
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123 processKey(mapper, BTN_RIGHT, 0);
8124 processSync(mapper);
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008127 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008128
8129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008131 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132
8133 processKey(mapper, BTN_MIDDLE, 0);
8134 processSync(mapper);
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008136 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008138
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008141 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142
8143 // press BTN_BACK, release BTN_BACK
8144 processKey(mapper, BTN_BACK, 1);
8145 processSync(mapper);
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8147 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8148 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008149
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008152 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8153
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8156 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157
8158 processKey(mapper, BTN_BACK, 0);
8159 processSync(mapper);
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008162 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008163
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008166 ASSERT_EQ(0, motionArgs.buttonState);
8167
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8169 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8170 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8171
8172 // press BTN_SIDE, release BTN_SIDE
8173 processKey(mapper, BTN_SIDE, 1);
8174 processSync(mapper);
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8176 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8177 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008178
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008181 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8182
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8185 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186
8187 processKey(mapper, BTN_SIDE, 0);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008191 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008192
8193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008195 ASSERT_EQ(0, motionArgs.buttonState);
8196
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8198 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8199 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8200
8201 // press BTN_FORWARD, release BTN_FORWARD
8202 processKey(mapper, BTN_FORWARD, 1);
8203 processSync(mapper);
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8205 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8206 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008207
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008210 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8211
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8214 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008215
8216 processKey(mapper, BTN_FORWARD, 0);
8217 processSync(mapper);
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008220 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008221
8222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008224 ASSERT_EQ(0, motionArgs.buttonState);
8225
Michael Wrightd02c5b62014-02-10 15:10:22 -08008226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8227 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8228 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8229
8230 // press BTN_EXTRA, release BTN_EXTRA
8231 processKey(mapper, BTN_EXTRA, 1);
8232 processSync(mapper);
8233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8234 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8235 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008236
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008239 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8240
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8243 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008244
8245 processKey(mapper, BTN_EXTRA, 0);
8246 processSync(mapper);
8247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008250
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008253 ASSERT_EQ(0, motionArgs.buttonState);
8254
Michael Wrightd02c5b62014-02-10 15:10:22 -08008255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8256 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8257 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8258
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8260
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 // press BTN_STYLUS, release BTN_STYLUS
8262 processKey(mapper, BTN_STYLUS, 1);
8263 processSync(mapper);
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008266 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8267
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8269 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8270 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008271
8272 processKey(mapper, BTN_STYLUS, 0);
8273 processSync(mapper);
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008276 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008277
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008280 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281
8282 // press BTN_STYLUS2, release BTN_STYLUS2
8283 processKey(mapper, BTN_STYLUS2, 1);
8284 processSync(mapper);
8285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008287 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8288
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8290 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8291 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292
8293 processKey(mapper, BTN_STYLUS2, 0);
8294 processSync(mapper);
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008298
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008301 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008302
8303 // release touch
8304 processId(mapper, -1);
8305 processSync(mapper);
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8307 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8308 ASSERT_EQ(0, motionArgs.buttonState);
8309}
8310
8311TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312 addConfigurationProperty("touch.deviceType", "touchScreen");
8313 prepareDisplay(DISPLAY_ORIENTATION_0);
8314 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008315 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316
8317 NotifyMotionArgs motionArgs;
8318
8319 // default tool type is finger
8320 processId(mapper, 1);
8321 processPosition(mapper, 100, 200);
8322 processSync(mapper);
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8324 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8326
8327 // eraser
8328 processKey(mapper, BTN_TOOL_RUBBER, 1);
8329 processSync(mapper);
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8333
8334 // stylus
8335 processKey(mapper, BTN_TOOL_RUBBER, 0);
8336 processKey(mapper, BTN_TOOL_PEN, 1);
8337 processSync(mapper);
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8341
8342 // brush
8343 processKey(mapper, BTN_TOOL_PEN, 0);
8344 processKey(mapper, BTN_TOOL_BRUSH, 1);
8345 processSync(mapper);
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8349
8350 // pencil
8351 processKey(mapper, BTN_TOOL_BRUSH, 0);
8352 processKey(mapper, BTN_TOOL_PENCIL, 1);
8353 processSync(mapper);
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8357
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008358 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008359 processKey(mapper, BTN_TOOL_PENCIL, 0);
8360 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8361 processSync(mapper);
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8365
8366 // mouse
8367 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8368 processKey(mapper, BTN_TOOL_MOUSE, 1);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8373
8374 // lens
8375 processKey(mapper, BTN_TOOL_MOUSE, 0);
8376 processKey(mapper, BTN_TOOL_LENS, 1);
8377 processSync(mapper);
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8381
8382 // double-tap
8383 processKey(mapper, BTN_TOOL_LENS, 0);
8384 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8385 processSync(mapper);
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8389
8390 // triple-tap
8391 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8392 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8393 processSync(mapper);
8394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8397
8398 // quad-tap
8399 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8400 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8401 processSync(mapper);
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8405
8406 // finger
8407 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8408 processKey(mapper, BTN_TOOL_FINGER, 1);
8409 processSync(mapper);
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8413
8414 // stylus trumps finger
8415 processKey(mapper, BTN_TOOL_PEN, 1);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8420
8421 // eraser trumps stylus
8422 processKey(mapper, BTN_TOOL_RUBBER, 1);
8423 processSync(mapper);
8424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8427
8428 // mouse trumps eraser
8429 processKey(mapper, BTN_TOOL_MOUSE, 1);
8430 processSync(mapper);
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8434
8435 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8436 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8441
8442 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8443 processToolType(mapper, MT_TOOL_PEN);
8444 processSync(mapper);
8445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8448
8449 // back to default tool type
8450 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8451 processKey(mapper, BTN_TOOL_MOUSE, 0);
8452 processKey(mapper, BTN_TOOL_RUBBER, 0);
8453 processKey(mapper, BTN_TOOL_PEN, 0);
8454 processKey(mapper, BTN_TOOL_FINGER, 0);
8455 processSync(mapper);
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8459}
8460
8461TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462 addConfigurationProperty("touch.deviceType", "touchScreen");
8463 prepareDisplay(DISPLAY_ORIENTATION_0);
8464 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008465 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008466 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008467
8468 NotifyMotionArgs motionArgs;
8469
8470 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8471 processId(mapper, 1);
8472 processPosition(mapper, 100, 200);
8473 processSync(mapper);
8474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8475 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8477 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8478
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8480 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8482 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8483
8484 // move a little
8485 processPosition(mapper, 150, 250);
8486 processSync(mapper);
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8490 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8491
8492 // down when BTN_TOUCH is pressed, pressure defaults to 1
8493 processKey(mapper, BTN_TOUCH, 1);
8494 processSync(mapper);
8495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8496 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8498 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8499
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8503 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8504
8505 // up when BTN_TOUCH is released, hover restored
8506 processKey(mapper, BTN_TOUCH, 0);
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8511 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8512
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8516 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8517
8518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8521 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8522
8523 // exit hover when pointer goes away
8524 processId(mapper, -1);
8525 processSync(mapper);
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8529 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8530}
8531
8532TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008533 addConfigurationProperty("touch.deviceType", "touchScreen");
8534 prepareDisplay(DISPLAY_ORIENTATION_0);
8535 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008536 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008537
8538 NotifyMotionArgs motionArgs;
8539
8540 // initially hovering because pressure is 0
8541 processId(mapper, 1);
8542 processPosition(mapper, 100, 200);
8543 processPressure(mapper, 0);
8544 processSync(mapper);
8545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8546 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8548 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8549
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8553 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8554
8555 // move a little
8556 processPosition(mapper, 150, 250);
8557 processSync(mapper);
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8561 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8562
8563 // down when pressure becomes non-zero
8564 processPressure(mapper, RAW_PRESSURE_MAX);
8565 processSync(mapper);
8566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8569 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8570
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8575
8576 // up when pressure becomes 0, hover restored
8577 processPressure(mapper, 0);
8578 processSync(mapper);
8579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8580 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8582 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8583
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8585 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8587 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8588
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8593
8594 // exit hover when pointer goes away
8595 processId(mapper, -1);
8596 processSync(mapper);
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8600 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8601}
8602
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008603/**
8604 * Set the input device port <--> display port associations, and check that the
8605 * events are routed to the display that matches the display port.
8606 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8607 */
8608TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008609 const std::string usb2 = "USB2";
8610 const uint8_t hdmi1 = 0;
8611 const uint8_t hdmi2 = 1;
8612 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008613 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008614
8615 addConfigurationProperty("touch.deviceType", "touchScreen");
8616 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008617 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008618
8619 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8620 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8621
8622 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8623 // for this input device is specified, and the matching viewport is not present,
8624 // the input device should be disabled (at the mapper level).
8625
8626 // Add viewport for display 2 on hdmi2
8627 prepareSecondaryDisplay(type, hdmi2);
8628 // Send a touch event
8629 processPosition(mapper, 100, 100);
8630 processSync(mapper);
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8632
8633 // Add viewport for display 1 on hdmi1
8634 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8635 // Send a touch event again
8636 processPosition(mapper, 100, 100);
8637 processSync(mapper);
8638
8639 NotifyMotionArgs args;
8640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8641 ASSERT_EQ(DISPLAY_ID, args.displayId);
8642}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008643
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008644TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8645 addConfigurationProperty("touch.deviceType", "touchScreen");
8646 prepareAxes(POSITION);
8647 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8648
8649 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8650
8651 prepareDisplay(DISPLAY_ORIENTATION_0);
8652 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8653
8654 // Send a touch event
8655 processPosition(mapper, 100, 100);
8656 processSync(mapper);
8657
8658 NotifyMotionArgs args;
8659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8660 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8661}
8662
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008663TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008664 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008665 std::shared_ptr<FakePointerController> fakePointerController =
8666 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008667 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008668 fakePointerController->setPosition(100, 200);
8669 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008670 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008671
Garfield Tan888a6a42020-01-09 11:39:16 -08008672 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008673 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008674
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008675 prepareDisplay(DISPLAY_ORIENTATION_0);
8676 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008677 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008678
8679 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008680 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008681
8682 NotifyMotionArgs motionArgs;
8683 processPosition(mapper, 100, 100);
8684 processSync(mapper);
8685
8686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8687 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8688 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8689}
8690
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008691/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008692 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8693 */
8694TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8695 addConfigurationProperty("touch.deviceType", "touchScreen");
8696 prepareAxes(POSITION);
8697 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8698
8699 prepareDisplay(DISPLAY_ORIENTATION_0);
8700 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8701 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8702 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8703 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8704
8705 NotifyMotionArgs args;
8706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8707 ASSERT_EQ(26, args.readTime);
8708
8709 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8710 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8711 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8712
8713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8714 ASSERT_EQ(33, args.readTime);
8715}
8716
8717/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008718 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8719 * events should not be delivered to the listener.
8720 */
8721TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8722 addConfigurationProperty("touch.deviceType", "touchScreen");
8723 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8724 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8725 ViewportType::INTERNAL);
8726 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8727 prepareAxes(POSITION);
8728 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8729
8730 NotifyMotionArgs motionArgs;
8731 processPosition(mapper, 100, 100);
8732 processSync(mapper);
8733
8734 mFakeListener->assertNotifyMotionWasNotCalled();
8735}
8736
Garfield Tanc734e4f2021-01-15 20:01:39 -08008737TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8738 addConfigurationProperty("touch.deviceType", "touchScreen");
8739 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8740 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8741 ViewportType::INTERNAL);
8742 std::optional<DisplayViewport> optionalDisplayViewport =
8743 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8744 ASSERT_TRUE(optionalDisplayViewport.has_value());
8745 DisplayViewport displayViewport = *optionalDisplayViewport;
8746
8747 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8748 prepareAxes(POSITION);
8749 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8750
8751 // Finger down
8752 int32_t x = 100, y = 100;
8753 processPosition(mapper, x, y);
8754 processSync(mapper);
8755
8756 NotifyMotionArgs motionArgs;
8757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8758 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8759
8760 // Deactivate display viewport
8761 displayViewport.isActive = false;
8762 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8763 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8764
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008765 // The ongoing touch should be canceled immediately
8766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8767 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8768
8769 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008770 x += 10, y += 10;
8771 processPosition(mapper, x, y);
8772 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008774
8775 // Reactivate display viewport
8776 displayViewport.isActive = true;
8777 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8778 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8779
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008780 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008781 x += 10, y += 10;
8782 processPosition(mapper, x, y);
8783 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8785 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008786}
8787
Arthur Hung7c645402019-01-25 17:45:42 +08008788TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8789 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008790 prepareAxes(POSITION | ID | SLOT);
8791 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008792 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008793
8794 // Create the second touch screen device, and enable multi fingers.
8795 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008796 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008797 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008798 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008799 std::shared_ptr<InputDevice> device2 =
8800 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008801 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008802
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008803 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8804 0 /*flat*/, 0 /*fuzz*/);
8805 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8806 0 /*flat*/, 0 /*fuzz*/);
8807 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8808 0 /*flat*/, 0 /*fuzz*/);
8809 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8810 0 /*flat*/, 0 /*fuzz*/);
8811 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8812 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8813 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008814
8815 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008816 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008817 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8818 device2->reset(ARBITRARY_TIME);
8819
8820 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008821 std::shared_ptr<FakePointerController> fakePointerController =
8822 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008823 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008824
8825 // Setup policy for associated displays and show touches.
8826 const uint8_t hdmi1 = 0;
8827 const uint8_t hdmi2 = 1;
8828 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8829 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8830 mFakePolicy->setShowTouches(true);
8831
8832 // Create displays.
8833 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008834 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008835
8836 // Default device will reconfigure above, need additional reconfiguration for another device.
8837 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008838 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8839 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008840
8841 // Two fingers down at default display.
8842 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8843 processPosition(mapper, x1, y1);
8844 processId(mapper, 1);
8845 processSlot(mapper, 1);
8846 processPosition(mapper, x2, y2);
8847 processId(mapper, 2);
8848 processSync(mapper);
8849
8850 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8851 fakePointerController->getSpots().find(DISPLAY_ID);
8852 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8853 ASSERT_EQ(size_t(2), iter->second.size());
8854
8855 // Two fingers down at second display.
8856 processPosition(mapper2, x1, y1);
8857 processId(mapper2, 1);
8858 processSlot(mapper2, 1);
8859 processPosition(mapper2, x2, y2);
8860 processId(mapper2, 2);
8861 processSync(mapper2);
8862
8863 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8864 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8865 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008866
8867 // Disable the show touches configuration and ensure the spots are cleared.
8868 mFakePolicy->setShowTouches(false);
8869 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8870 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8871
8872 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008873}
8874
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008875TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008876 prepareAxes(POSITION);
8877 addConfigurationProperty("touch.deviceType", "touchScreen");
8878 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008879 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008880
8881 NotifyMotionArgs motionArgs;
8882 // Unrotated video frame
8883 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8884 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008885 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008886 processPosition(mapper, 100, 200);
8887 processSync(mapper);
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8889 ASSERT_EQ(frames, motionArgs.videoFrames);
8890
8891 // Subsequent touch events should not have any videoframes
8892 // This is implemented separately in FakeEventHub,
8893 // but that should match the behaviour of TouchVideoDevice.
8894 processPosition(mapper, 200, 200);
8895 processSync(mapper);
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8897 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8898}
8899
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008900TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008901 prepareAxes(POSITION);
8902 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008903 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008904 // Unrotated video frame
8905 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8906 NotifyMotionArgs motionArgs;
8907
8908 // Test all 4 orientations
8909 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008910 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8911 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8912 clearViewports();
8913 prepareDisplay(orientation);
8914 std::vector<TouchVideoFrame> frames{frame};
8915 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8916 processPosition(mapper, 100, 200);
8917 processSync(mapper);
8918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8919 ASSERT_EQ(frames, motionArgs.videoFrames);
8920 }
8921}
8922
8923TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8924 prepareAxes(POSITION);
8925 addConfigurationProperty("touch.deviceType", "touchScreen");
8926 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8927 // orientation-aware are affected by display rotation.
8928 addConfigurationProperty("touch.orientationAware", "0");
8929 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8930 // Unrotated video frame
8931 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8932 NotifyMotionArgs motionArgs;
8933
8934 // Test all 4 orientations
8935 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008936 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8937 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8938 clearViewports();
8939 prepareDisplay(orientation);
8940 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008941 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008942 processPosition(mapper, 100, 200);
8943 processSync(mapper);
8944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008945 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8946 // compared to the display. This is so that when the window transform (which contains the
8947 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8948 // window's coordinate space.
8949 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008950 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008951
8952 // Release finger.
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008955 }
8956}
8957
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008958TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008959 prepareAxes(POSITION);
8960 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008961 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008962 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8963 // so mix these.
8964 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8965 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8966 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8967 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8968 NotifyMotionArgs motionArgs;
8969
8970 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008971 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008972 processPosition(mapper, 100, 200);
8973 processSync(mapper);
8974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008975 ASSERT_EQ(frames, motionArgs.videoFrames);
8976}
8977
8978TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8979 prepareAxes(POSITION);
8980 addConfigurationProperty("touch.deviceType", "touchScreen");
8981 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8982 // orientation-aware are affected by display rotation.
8983 addConfigurationProperty("touch.orientationAware", "0");
8984 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8985 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8986 // so mix these.
8987 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8988 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8989 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8990 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8991 NotifyMotionArgs motionArgs;
8992
8993 prepareDisplay(DISPLAY_ORIENTATION_90);
8994 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8995 processPosition(mapper, 100, 200);
8996 processSync(mapper);
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8998 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8999 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9000 // compared to the display. This is so that when the window transform (which contains the
9001 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9002 // window's coordinate space.
9003 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9004 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009005 ASSERT_EQ(frames, motionArgs.videoFrames);
9006}
9007
Arthur Hung9da14732019-09-02 16:16:58 +08009008/**
9009 * If we had defined port associations, but the viewport is not ready, the touch device would be
9010 * expected to be disabled, and it should be enabled after the viewport has found.
9011 */
9012TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009013 constexpr uint8_t hdmi2 = 1;
9014 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009015 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009016
9017 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9018
9019 addConfigurationProperty("touch.deviceType", "touchScreen");
9020 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009021 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009022
9023 ASSERT_EQ(mDevice->isEnabled(), false);
9024
9025 // Add display on hdmi2, the device should be enabled and can receive touch event.
9026 prepareSecondaryDisplay(type, hdmi2);
9027 ASSERT_EQ(mDevice->isEnabled(), true);
9028
9029 // Send a touch event.
9030 processPosition(mapper, 100, 100);
9031 processSync(mapper);
9032
9033 NotifyMotionArgs args;
9034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9035 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9036}
9037
Arthur Hung421eb1c2020-01-16 00:09:42 +08009038TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009039 addConfigurationProperty("touch.deviceType", "touchScreen");
9040 prepareDisplay(DISPLAY_ORIENTATION_0);
9041 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009042 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009043
9044 NotifyMotionArgs motionArgs;
9045
9046 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9047 // finger down
9048 processId(mapper, 1);
9049 processPosition(mapper, x1, y1);
9050 processSync(mapper);
9051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9052 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9054
9055 // finger move
9056 processId(mapper, 1);
9057 processPosition(mapper, x2, y2);
9058 processSync(mapper);
9059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9062
9063 // finger up.
9064 processId(mapper, -1);
9065 processSync(mapper);
9066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9067 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9069
9070 // new finger down
9071 processId(mapper, 1);
9072 processPosition(mapper, x3, y3);
9073 processSync(mapper);
9074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9075 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9077}
9078
9079/**
arthurhungcc7f9802020-04-30 17:55:40 +08009080 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9081 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009082 */
arthurhungcc7f9802020-04-30 17:55:40 +08009083TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009084 addConfigurationProperty("touch.deviceType", "touchScreen");
9085 prepareDisplay(DISPLAY_ORIENTATION_0);
9086 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009087 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009088
9089 NotifyMotionArgs motionArgs;
9090
9091 // default tool type is finger
9092 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009093 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009094 processPosition(mapper, x1, y1);
9095 processSync(mapper);
9096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9097 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9099
9100 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9101 processToolType(mapper, MT_TOOL_PALM);
9102 processSync(mapper);
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9104 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9105
9106 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009107 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009108 processPosition(mapper, x2, y2);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9111
9112 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009113 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009114 processSync(mapper);
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9116
9117 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009118 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009119 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009120 processPosition(mapper, x3, y3);
9121 processSync(mapper);
9122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9125}
9126
arthurhungbf89a482020-04-17 17:37:55 +08009127/**
arthurhungcc7f9802020-04-30 17:55:40 +08009128 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9129 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009130 */
arthurhungcc7f9802020-04-30 17:55:40 +08009131TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009132 addConfigurationProperty("touch.deviceType", "touchScreen");
9133 prepareDisplay(DISPLAY_ORIENTATION_0);
9134 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9135 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9136
9137 NotifyMotionArgs motionArgs;
9138
9139 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009140 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9141 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009142 processPosition(mapper, x1, y1);
9143 processSync(mapper);
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9145 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9147
9148 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009149 processSlot(mapper, SECOND_SLOT);
9150 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009151 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009152 processSync(mapper);
9153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009154 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9156
9157 // If the tool type of the first finger changes to MT_TOOL_PALM,
9158 // we expect to receive ACTION_POINTER_UP with cancel flag.
9159 processSlot(mapper, FIRST_SLOT);
9160 processId(mapper, FIRST_TRACKING_ID);
9161 processToolType(mapper, MT_TOOL_PALM);
9162 processSync(mapper);
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009164 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009165 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9166
9167 // The following MOVE events of second finger should be processed.
9168 processSlot(mapper, SECOND_SLOT);
9169 processId(mapper, SECOND_TRACKING_ID);
9170 processPosition(mapper, x2 + 1, y2 + 1);
9171 processSync(mapper);
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9174 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9175
9176 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9177 // it. Second finger receive move.
9178 processSlot(mapper, FIRST_SLOT);
9179 processId(mapper, INVALID_TRACKING_ID);
9180 processSync(mapper);
9181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9183 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9184
9185 // Second finger keeps moving.
9186 processSlot(mapper, SECOND_SLOT);
9187 processId(mapper, SECOND_TRACKING_ID);
9188 processPosition(mapper, x2 + 2, y2 + 2);
9189 processSync(mapper);
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9192 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9193
9194 // Second finger up.
9195 processId(mapper, INVALID_TRACKING_ID);
9196 processSync(mapper);
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9198 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9199 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9200}
9201
9202/**
9203 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9204 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9205 */
9206TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9207 addConfigurationProperty("touch.deviceType", "touchScreen");
9208 prepareDisplay(DISPLAY_ORIENTATION_0);
9209 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9210 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9211
9212 NotifyMotionArgs motionArgs;
9213
9214 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9215 // First finger down.
9216 processId(mapper, FIRST_TRACKING_ID);
9217 processPosition(mapper, x1, y1);
9218 processSync(mapper);
9219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9220 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9222
9223 // Second finger down.
9224 processSlot(mapper, SECOND_SLOT);
9225 processId(mapper, SECOND_TRACKING_ID);
9226 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009227 processSync(mapper);
9228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009229 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9231
arthurhungcc7f9802020-04-30 17:55:40 +08009232 // If the tool type of the first finger changes to MT_TOOL_PALM,
9233 // we expect to receive ACTION_POINTER_UP with cancel flag.
9234 processSlot(mapper, FIRST_SLOT);
9235 processId(mapper, FIRST_TRACKING_ID);
9236 processToolType(mapper, MT_TOOL_PALM);
9237 processSync(mapper);
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009239 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009240 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9241
9242 // Second finger keeps moving.
9243 processSlot(mapper, SECOND_SLOT);
9244 processId(mapper, SECOND_TRACKING_ID);
9245 processPosition(mapper, x2 + 1, y2 + 1);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9249
9250 // second finger becomes palm, receive cancel due to only 1 finger is active.
9251 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009252 processToolType(mapper, MT_TOOL_PALM);
9253 processSync(mapper);
9254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9255 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9256
arthurhungcc7f9802020-04-30 17:55:40 +08009257 // third finger down.
9258 processSlot(mapper, THIRD_SLOT);
9259 processId(mapper, THIRD_TRACKING_ID);
9260 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009261 processPosition(mapper, x3, y3);
9262 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9264 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9265 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009266 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9267
9268 // third finger move
9269 processId(mapper, THIRD_TRACKING_ID);
9270 processPosition(mapper, x3 + 1, y3 + 1);
9271 processSync(mapper);
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9274
9275 // first finger up, third finger receive move.
9276 processSlot(mapper, FIRST_SLOT);
9277 processId(mapper, INVALID_TRACKING_ID);
9278 processSync(mapper);
9279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9281 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9282
9283 // second finger up, third finger receive move.
9284 processSlot(mapper, SECOND_SLOT);
9285 processId(mapper, INVALID_TRACKING_ID);
9286 processSync(mapper);
9287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9289 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9290
9291 // third finger up.
9292 processSlot(mapper, THIRD_SLOT);
9293 processId(mapper, INVALID_TRACKING_ID);
9294 processSync(mapper);
9295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9296 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9297 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9298}
9299
9300/**
9301 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9302 * and the active finger could still be allowed to receive the events
9303 */
9304TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9305 addConfigurationProperty("touch.deviceType", "touchScreen");
9306 prepareDisplay(DISPLAY_ORIENTATION_0);
9307 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9308 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9309
9310 NotifyMotionArgs motionArgs;
9311
9312 // default tool type is finger
9313 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9314 processId(mapper, FIRST_TRACKING_ID);
9315 processPosition(mapper, x1, y1);
9316 processSync(mapper);
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9318 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9320
9321 // Second finger down.
9322 processSlot(mapper, SECOND_SLOT);
9323 processId(mapper, SECOND_TRACKING_ID);
9324 processPosition(mapper, x2, y2);
9325 processSync(mapper);
9326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009327 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9329
9330 // If the tool type of the second finger changes to MT_TOOL_PALM,
9331 // we expect to receive ACTION_POINTER_UP with cancel flag.
9332 processId(mapper, SECOND_TRACKING_ID);
9333 processToolType(mapper, MT_TOOL_PALM);
9334 processSync(mapper);
9335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009336 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009337 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9338
9339 // The following MOVE event should be processed.
9340 processSlot(mapper, FIRST_SLOT);
9341 processId(mapper, FIRST_TRACKING_ID);
9342 processPosition(mapper, x1 + 1, y1 + 1);
9343 processSync(mapper);
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9346 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9347
9348 // second finger up.
9349 processSlot(mapper, SECOND_SLOT);
9350 processId(mapper, INVALID_TRACKING_ID);
9351 processSync(mapper);
9352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9354
9355 // first finger keep moving
9356 processSlot(mapper, FIRST_SLOT);
9357 processId(mapper, FIRST_TRACKING_ID);
9358 processPosition(mapper, x1 + 2, y1 + 2);
9359 processSync(mapper);
9360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9362
9363 // first finger up.
9364 processId(mapper, INVALID_TRACKING_ID);
9365 processSync(mapper);
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9367 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9368 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009369}
9370
Arthur Hung9ad18942021-06-19 02:04:46 +00009371/**
9372 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9373 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9374 * cause slot be valid again.
9375 */
9376TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9377 addConfigurationProperty("touch.deviceType", "touchScreen");
9378 prepareDisplay(DISPLAY_ORIENTATION_0);
9379 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9380 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9381
9382 NotifyMotionArgs motionArgs;
9383
9384 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9385 // First finger down.
9386 processId(mapper, FIRST_TRACKING_ID);
9387 processPosition(mapper, x1, y1);
9388 processPressure(mapper, RAW_PRESSURE_MAX);
9389 processSync(mapper);
9390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9391 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9392 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9393
9394 // First finger move.
9395 processId(mapper, FIRST_TRACKING_ID);
9396 processPosition(mapper, x1 + 1, y1 + 1);
9397 processPressure(mapper, RAW_PRESSURE_MAX);
9398 processSync(mapper);
9399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9401 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9402
9403 // Second finger down.
9404 processSlot(mapper, SECOND_SLOT);
9405 processId(mapper, SECOND_TRACKING_ID);
9406 processPosition(mapper, x2, y2);
9407 processPressure(mapper, RAW_PRESSURE_MAX);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009410 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009411 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9412
9413 // second finger up with some unexpected data.
9414 processSlot(mapper, SECOND_SLOT);
9415 processId(mapper, INVALID_TRACKING_ID);
9416 processPosition(mapper, x2, y2);
9417 processSync(mapper);
9418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009419 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009420 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9421
9422 // first finger up with some unexpected data.
9423 processSlot(mapper, FIRST_SLOT);
9424 processId(mapper, INVALID_TRACKING_ID);
9425 processPosition(mapper, x2, y2);
9426 processPressure(mapper, RAW_PRESSURE_MAX);
9427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9429 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9430 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9431}
9432
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00009433TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9434 addConfigurationProperty("touch.deviceType", "touchScreen");
9435 prepareDisplay(DISPLAY_ORIENTATION_0);
9436 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9437 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9438
9439 NotifyMotionArgs motionArgs;
9440
9441 // First finger down.
9442 processId(mapper, FIRST_TRACKING_ID);
9443 processPosition(mapper, 100, 200);
9444 processPressure(mapper, RAW_PRESSURE_MAX);
9445 processSync(mapper);
9446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9447 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9448
9449 // Second finger down.
9450 processSlot(mapper, SECOND_SLOT);
9451 processId(mapper, SECOND_TRACKING_ID);
9452 processPosition(mapper, 300, 400);
9453 processPressure(mapper, RAW_PRESSURE_MAX);
9454 processSync(mapper);
9455 ASSERT_NO_FATAL_FAILURE(
9456 mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9457 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
9458
9459 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
9460 // preserved. Resetting should not generate any events.
9461 mapper.reset(ARBITRARY_TIME);
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9463
9464 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9465 // the existing touch state to generate a down event.
9466 processPosition(mapper, 301, 302);
9467 processSync(mapper);
9468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9469 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9471 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
9472
9473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9474}
9475
9476TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9477 addConfigurationProperty("touch.deviceType", "touchScreen");
9478 prepareDisplay(DISPLAY_ORIENTATION_0);
9479 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9480 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9481
9482 NotifyMotionArgs motionArgs;
9483
9484 // First finger touches down and releases.
9485 processId(mapper, FIRST_TRACKING_ID);
9486 processPosition(mapper, 100, 200);
9487 processPressure(mapper, RAW_PRESSURE_MAX);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9490 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9491 processId(mapper, INVALID_TRACKING_ID);
9492 processSync(mapper);
9493 ASSERT_NO_FATAL_FAILURE(
9494 mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9495 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9496
9497 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9498 // raw state where no pointers are down.
9499 mapper.reset(ARBITRARY_TIME);
9500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9501
9502 // Send an empty sync frame. Since there are no pointers, no events are generated.
9503 processSync(mapper);
9504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9505}
9506
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009507// --- MultiTouchInputMapperTest_ExternalDevice ---
9508
9509class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9510protected:
Chris Yea52ade12020-08-27 16:49:20 -07009511 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009512};
9513
9514/**
9515 * Expect fallback to internal viewport if device is external and external viewport is not present.
9516 */
9517TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9518 prepareAxes(POSITION);
9519 addConfigurationProperty("touch.deviceType", "touchScreen");
9520 prepareDisplay(DISPLAY_ORIENTATION_0);
9521 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9522
9523 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9524
9525 NotifyMotionArgs motionArgs;
9526
9527 // Expect the event to be sent to the internal viewport,
9528 // because an external viewport is not present.
9529 processPosition(mapper, 100, 100);
9530 processSync(mapper);
9531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9532 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9533
9534 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009535 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009536 processPosition(mapper, 100, 100);
9537 processSync(mapper);
9538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9539 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9540}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009541
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009542TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9543 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9544 std::shared_ptr<FakePointerController> fakePointerController =
9545 std::make_shared<FakePointerController>();
9546 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9547 fakePointerController->setPosition(0, 0);
9548 fakePointerController->setButtonState(0);
9549
9550 // prepare device and capture
9551 prepareDisplay(DISPLAY_ORIENTATION_0);
9552 prepareAxes(POSITION | ID | SLOT);
9553 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9554 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9555 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009556 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009557 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9558
9559 // captured touchpad should be a touchpad source
9560 NotifyDeviceResetArgs resetArgs;
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9562 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9563
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009564 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009565
9566 const InputDeviceInfo::MotionRange* relRangeX =
9567 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9568 ASSERT_NE(relRangeX, nullptr);
9569 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9570 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9571 const InputDeviceInfo::MotionRange* relRangeY =
9572 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9573 ASSERT_NE(relRangeY, nullptr);
9574 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9575 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9576
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009577 // run captured pointer tests - note that this is unscaled, so input listener events should be
9578 // identical to what the hardware sends (accounting for any
9579 // calibration).
9580 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009581 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009582 processId(mapper, 1);
9583 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9584 processKey(mapper, BTN_TOUCH, 1);
9585 processSync(mapper);
9586
9587 // expect coord[0] to contain initial location of touch 0
9588 NotifyMotionArgs args;
9589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9591 ASSERT_EQ(1U, args.pointerCount);
9592 ASSERT_EQ(0, args.pointerProperties[0].id);
9593 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9594 ASSERT_NO_FATAL_FAILURE(
9595 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9596
9597 // FINGER 1 DOWN
9598 processSlot(mapper, 1);
9599 processId(mapper, 2);
9600 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9601 processSync(mapper);
9602
9603 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009605 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009606 ASSERT_EQ(2U, args.pointerCount);
9607 ASSERT_EQ(0, args.pointerProperties[0].id);
9608 ASSERT_EQ(1, args.pointerProperties[1].id);
9609 ASSERT_NO_FATAL_FAILURE(
9610 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9611 ASSERT_NO_FATAL_FAILURE(
9612 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9613
9614 // FINGER 1 MOVE
9615 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9616 processSync(mapper);
9617
9618 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9619 // from move
9620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9622 ASSERT_NO_FATAL_FAILURE(
9623 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9624 ASSERT_NO_FATAL_FAILURE(
9625 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9626
9627 // FINGER 0 MOVE
9628 processSlot(mapper, 0);
9629 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9630 processSync(mapper);
9631
9632 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9635 ASSERT_NO_FATAL_FAILURE(
9636 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9637 ASSERT_NO_FATAL_FAILURE(
9638 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9639
9640 // BUTTON DOWN
9641 processKey(mapper, BTN_LEFT, 1);
9642 processSync(mapper);
9643
9644 // touchinputmapper design sends a move before button press
9645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9648 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9649
9650 // BUTTON UP
9651 processKey(mapper, BTN_LEFT, 0);
9652 processSync(mapper);
9653
9654 // touchinputmapper design sends a move after button release
9655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9659
9660 // FINGER 0 UP
9661 processId(mapper, -1);
9662 processSync(mapper);
9663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9664 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9665
9666 // FINGER 1 MOVE
9667 processSlot(mapper, 1);
9668 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9669 processSync(mapper);
9670
9671 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9674 ASSERT_EQ(1U, args.pointerCount);
9675 ASSERT_EQ(1, args.pointerProperties[0].id);
9676 ASSERT_NO_FATAL_FAILURE(
9677 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9678
9679 // FINGER 1 UP
9680 processId(mapper, -1);
9681 processKey(mapper, BTN_TOUCH, 0);
9682 processSync(mapper);
9683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9684 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9685
9686 // non captured touchpad should be a mouse source
9687 mFakePolicy->setPointerCapture(false);
9688 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9690 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9691}
9692
9693TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9694 std::shared_ptr<FakePointerController> fakePointerController =
9695 std::make_shared<FakePointerController>();
9696 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9697 fakePointerController->setPosition(0, 0);
9698 fakePointerController->setButtonState(0);
9699
9700 // prepare device and capture
9701 prepareDisplay(DISPLAY_ORIENTATION_0);
9702 prepareAxes(POSITION | ID | SLOT);
9703 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9704 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009705 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009706 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9707 // run uncaptured pointer tests - pushes out generic events
9708 // FINGER 0 DOWN
9709 processId(mapper, 3);
9710 processPosition(mapper, 100, 100);
9711 processKey(mapper, BTN_TOUCH, 1);
9712 processSync(mapper);
9713
9714 // start at (100,100), cursor should be at (0,0) * scale
9715 NotifyMotionArgs args;
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9717 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9718 ASSERT_NO_FATAL_FAILURE(
9719 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9720
9721 // FINGER 0 MOVE
9722 processPosition(mapper, 200, 200);
9723 processSync(mapper);
9724
9725 // compute scaling to help with touch position checking
9726 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9727 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9728 float scale =
9729 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9730
9731 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9735 0, 0, 0, 0, 0, 0, 0));
9736}
9737
9738TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9739 std::shared_ptr<FakePointerController> fakePointerController =
9740 std::make_shared<FakePointerController>();
9741
9742 prepareDisplay(DISPLAY_ORIENTATION_0);
9743 prepareAxes(POSITION | ID | SLOT);
9744 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009745 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009746 mFakePolicy->setPointerCapture(false);
9747 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9748
9749 // uncaptured touchpad should be a pointer device
9750 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9751
9752 // captured touchpad should be a touchpad device
9753 mFakePolicy->setPointerCapture(true);
9754 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9755 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9756}
9757
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009758// --- JoystickInputMapperTest ---
9759
9760class JoystickInputMapperTest : public InputMapperTest {
9761protected:
9762 static const int32_t RAW_X_MIN;
9763 static const int32_t RAW_X_MAX;
9764 static const int32_t RAW_Y_MIN;
9765 static const int32_t RAW_Y_MAX;
9766
9767 void SetUp() override {
9768 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9769 }
9770 void prepareAxes() {
9771 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9772 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9773 }
9774
9775 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9777 }
9778
9779 void processSync(JoystickInputMapper& mapper) {
9780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9781 }
9782
9783 void prepareVirtualDisplay(int32_t orientation) {
9784 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9785 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9786 NO_PORT, ViewportType::VIRTUAL);
9787 }
9788};
9789
9790const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9791const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9792const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9793const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9794
9795TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9796 prepareAxes();
9797 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9798
9799 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9800
9801 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9802
9803 // Send an axis event
9804 processAxis(mapper, ABS_X, 100);
9805 processSync(mapper);
9806
9807 NotifyMotionArgs args;
9808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9809 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9810
9811 // Send another axis event
9812 processAxis(mapper, ABS_Y, 100);
9813 processSync(mapper);
9814
9815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9816 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9817}
9818
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009819// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009820
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009821class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009822protected:
9823 static const char* DEVICE_NAME;
9824 static const char* DEVICE_LOCATION;
9825 static const int32_t DEVICE_ID;
9826 static const int32_t DEVICE_GENERATION;
9827 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009828 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009829 static const int32_t EVENTHUB_ID;
9830
9831 std::shared_ptr<FakeEventHub> mFakeEventHub;
9832 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009833 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009834 std::unique_ptr<InstrumentedInputReader> mReader;
9835 std::shared_ptr<InputDevice> mDevice;
9836
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009837 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009838 mFakeEventHub = std::make_unique<FakeEventHub>();
9839 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009840 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009841 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009842 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009843 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9844 }
9845
9846 void SetUp() override { SetUp(DEVICE_CLASSES); }
9847
9848 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009849 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009850 mFakePolicy.clear();
9851 }
9852
9853 void configureDevice(uint32_t changes) {
9854 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9855 mReader->requestRefreshConfiguration(changes);
9856 mReader->loopOnce();
9857 }
9858 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9859 }
9860
9861 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9862 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009863 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009864 InputDeviceIdentifier identifier;
9865 identifier.name = name;
9866 identifier.location = location;
9867 std::shared_ptr<InputDevice> device =
9868 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9869 identifier);
9870 mReader->pushNextDevice(device);
9871 mFakeEventHub->addDevice(eventHubId, name, classes);
9872 mReader->loopOnce();
9873 return device;
9874 }
9875
9876 template <class T, typename... Args>
9877 T& addControllerAndConfigure(Args... args) {
9878 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9879
9880 return controller;
9881 }
9882};
9883
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009884const char* PeripheralControllerTest::DEVICE_NAME = "device";
9885const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9886const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9887const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9888const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009889const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9890 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009891const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009892
9893// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009894class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009895protected:
9896 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009897 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009898 }
9899};
9900
9901TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009902 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009903
9904 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9905 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9906}
9907
9908TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009909 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009910
9911 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9912 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9913}
9914
9915// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009916class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009917protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009918 void SetUp() override {
9919 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9920 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009921};
9922
Chris Ye85758332021-05-16 23:05:17 -07009923TEST_F(LightControllerTest, MonoLight) {
9924 RawLightInfo infoMono = {.id = 1,
9925 .name = "Mono",
9926 .maxBrightness = 255,
9927 .flags = InputLightClass::BRIGHTNESS,
9928 .path = ""};
9929 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009930
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009931 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009932 InputDeviceInfo info;
9933 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009934 std::vector<InputDeviceLightInfo> lights = info.getLights();
9935 ASSERT_EQ(1U, lights.size());
9936 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009937
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009938 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9939 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009940}
9941
9942TEST_F(LightControllerTest, RGBLight) {
9943 RawLightInfo infoRed = {.id = 1,
9944 .name = "red",
9945 .maxBrightness = 255,
9946 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9947 .path = ""};
9948 RawLightInfo infoGreen = {.id = 2,
9949 .name = "green",
9950 .maxBrightness = 255,
9951 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9952 .path = ""};
9953 RawLightInfo infoBlue = {.id = 3,
9954 .name = "blue",
9955 .maxBrightness = 255,
9956 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9957 .path = ""};
9958 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9959 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9960 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9961
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009962 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009963 InputDeviceInfo info;
9964 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009965 std::vector<InputDeviceLightInfo> lights = info.getLights();
9966 ASSERT_EQ(1U, lights.size());
9967 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009968
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009969 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9970 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009971}
9972
9973TEST_F(LightControllerTest, MultiColorRGBLight) {
9974 RawLightInfo infoColor = {.id = 1,
9975 .name = "red",
9976 .maxBrightness = 255,
9977 .flags = InputLightClass::BRIGHTNESS |
9978 InputLightClass::MULTI_INTENSITY |
9979 InputLightClass::MULTI_INDEX,
9980 .path = ""};
9981
9982 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9983
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009984 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009985 InputDeviceInfo info;
9986 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009987 std::vector<InputDeviceLightInfo> lights = info.getLights();
9988 ASSERT_EQ(1U, lights.size());
9989 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009990
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009991 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9992 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009993}
9994
9995TEST_F(LightControllerTest, PlayerIdLight) {
9996 RawLightInfo info1 = {.id = 1,
9997 .name = "player1",
9998 .maxBrightness = 255,
9999 .flags = InputLightClass::BRIGHTNESS,
10000 .path = ""};
10001 RawLightInfo info2 = {.id = 2,
10002 .name = "player2",
10003 .maxBrightness = 255,
10004 .flags = InputLightClass::BRIGHTNESS,
10005 .path = ""};
10006 RawLightInfo info3 = {.id = 3,
10007 .name = "player3",
10008 .maxBrightness = 255,
10009 .flags = InputLightClass::BRIGHTNESS,
10010 .path = ""};
10011 RawLightInfo info4 = {.id = 4,
10012 .name = "player4",
10013 .maxBrightness = 255,
10014 .flags = InputLightClass::BRIGHTNESS,
10015 .path = ""};
10016 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10017 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10018 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10019 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10020
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010021 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010022 InputDeviceInfo info;
10023 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010024 std::vector<InputDeviceLightInfo> lights = info.getLights();
10025 ASSERT_EQ(1U, lights.size());
10026 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010027
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010028 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10029 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10030 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010031}
10032
Michael Wrightd02c5b62014-02-10 15:10:22 -080010033} // namespace android