blob: 3afa52cf2ce0c567a69683c8ddd189ee56e9bb20 [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
lilinnan687e58f2022-07-19 16:00:50 +08006790TEST_F(SingleTouchInputMapperTest,
6791 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6792 addConfigurationProperty("touch.deviceType", "touchScreen");
6793 prepareDisplay(DISPLAY_ORIENTATION_0);
6794 prepareButtons();
6795 prepareAxes(POSITION);
6796 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6797 NotifyMotionArgs motionArgs;
6798
6799 // Down.
6800 int32_t x = 100;
6801 int32_t y = 200;
6802 processDown(mapper, x, y);
6803 processSync(mapper);
6804
6805 // We should receive a down event
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6807 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6808
6809 // Change display id
6810 clearViewports();
6811 prepareSecondaryDisplay(ViewportType::INTERNAL);
6812
6813 // We should receive a cancel event
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6815 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6816 // Then receive reset called
6817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6818}
6819
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006820TEST_F(SingleTouchInputMapperTest,
6821 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6822 addConfigurationProperty("touch.deviceType", "touchScreen");
6823 prepareDisplay(DISPLAY_ORIENTATION_0);
6824 prepareButtons();
6825 prepareAxes(POSITION);
6826 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6828 NotifyMotionArgs motionArgs;
6829
6830 // Start a new gesture.
6831 processDown(mapper, 100, 200);
6832 processSync(mapper);
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6835
6836 // Make the viewport inactive. This will put the device in disabled mode.
6837 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6838 viewport->isActive = false;
6839 mFakePolicy->updateViewport(*viewport);
6840 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6841
6842 // We should receive a cancel event for the ongoing gesture.
6843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6844 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6845 // Then we should be notified that the device was reset.
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6847
6848 // No events are generated while the viewport is inactive.
6849 processMove(mapper, 101, 201);
6850 processSync(mapper);
6851 processDown(mapper, 102, 202);
6852 processSync(mapper);
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6854
6855 // Make the viewport active again. The device should resume processing events.
6856 viewport->isActive = true;
6857 mFakePolicy->updateViewport(*viewport);
6858 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6859
6860 // The device is reset because it changes back to direct mode, without generating any events.
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6863
6864 // Start a new gesture.
6865 processDown(mapper, 100, 200);
6866 processSync(mapper);
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6868 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6869
6870 // No more events.
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6873}
6874
Prabir Pradhan5632d622021-09-06 07:57:20 -07006875// --- TouchDisplayProjectionTest ---
6876
6877class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6878public:
6879 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6880 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6881 // rotated equivalent of the given un-rotated physical display bounds.
6882 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6883 uint32_t inverseRotationFlags;
6884 auto width = DISPLAY_WIDTH;
6885 auto height = DISPLAY_HEIGHT;
6886 switch (orientation) {
6887 case DISPLAY_ORIENTATION_90:
6888 inverseRotationFlags = ui::Transform::ROT_270;
6889 std::swap(width, height);
6890 break;
6891 case DISPLAY_ORIENTATION_180:
6892 inverseRotationFlags = ui::Transform::ROT_180;
6893 break;
6894 case DISPLAY_ORIENTATION_270:
6895 inverseRotationFlags = ui::Transform::ROT_90;
6896 std::swap(width, height);
6897 break;
6898 case DISPLAY_ORIENTATION_0:
6899 inverseRotationFlags = ui::Transform::ROT_0;
6900 break;
6901 default:
6902 FAIL() << "Invalid orientation: " << orientation;
6903 }
6904
6905 const ui::Transform rotation(inverseRotationFlags, width, height);
6906 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6907
6908 std::optional<DisplayViewport> internalViewport =
6909 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6910 DisplayViewport& v = *internalViewport;
6911 v.displayId = DISPLAY_ID;
6912 v.orientation = orientation;
6913
6914 v.logicalLeft = 0;
6915 v.logicalTop = 0;
6916 v.logicalRight = 100;
6917 v.logicalBottom = 100;
6918
6919 v.physicalLeft = rotatedPhysicalDisplay.left;
6920 v.physicalTop = rotatedPhysicalDisplay.top;
6921 v.physicalRight = rotatedPhysicalDisplay.right;
6922 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6923
6924 v.deviceWidth = width;
6925 v.deviceHeight = height;
6926
6927 v.isActive = true;
6928 v.uniqueId = UNIQUE_ID;
6929 v.type = ViewportType::INTERNAL;
6930 mFakePolicy->updateViewport(v);
6931 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6932 }
6933
6934 void assertReceivedMove(const Point& point) {
6935 NotifyMotionArgs motionArgs;
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6938 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6940 1, 0, 0, 0, 0, 0, 0, 0));
6941 }
6942};
6943
6944TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6945 addConfigurationProperty("touch.deviceType", "touchScreen");
6946 prepareDisplay(DISPLAY_ORIENTATION_0);
6947
6948 prepareButtons();
6949 prepareAxes(POSITION);
6950 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6951
6952 NotifyMotionArgs motionArgs;
6953
6954 // Configure the DisplayViewport such that the logical display maps to a subsection of
6955 // the display panel called the physical display. Here, the physical display is bounded by the
6956 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6957 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6958 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6959 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6960
6961 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6962 DISPLAY_ORIENTATION_270}) {
6963 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6964
6965 // Touches outside the physical display should be ignored, and should not generate any
6966 // events. Ensure touches at the following points that lie outside of the physical display
6967 // area do not generate any events.
6968 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6969 processDown(mapper, toRawX(point.x), toRawY(point.y));
6970 processSync(mapper);
6971 processUp(mapper);
6972 processSync(mapper);
6973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6974 << "Unexpected event generated for touch outside physical display at point: "
6975 << point.x << ", " << point.y;
6976 }
6977 }
6978}
6979
6980TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6981 addConfigurationProperty("touch.deviceType", "touchScreen");
6982 prepareDisplay(DISPLAY_ORIENTATION_0);
6983
6984 prepareButtons();
6985 prepareAxes(POSITION);
6986 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6987
6988 NotifyMotionArgs motionArgs;
6989
6990 // Configure the DisplayViewport such that the logical display maps to a subsection of
6991 // the display panel called the physical display. Here, the physical display is bounded by the
6992 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6993 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6994
6995 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6996 DISPLAY_ORIENTATION_270}) {
6997 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6998
6999 // Touches that start outside the physical display should be ignored until it enters the
7000 // physical display bounds, at which point it should generate a down event. Start a touch at
7001 // the point (5, 100), which is outside the physical display bounds.
7002 static const Point kOutsidePoint{5, 100};
7003 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7004 processSync(mapper);
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7006
7007 // Move the touch into the physical display area. This should generate a pointer down.
7008 processMove(mapper, toRawX(11), toRawY(21));
7009 processSync(mapper);
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7011 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7012 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7013 ASSERT_NO_FATAL_FAILURE(
7014 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7015
7016 // Move the touch inside the physical display area. This should generate a pointer move.
7017 processMove(mapper, toRawX(69), toRawY(159));
7018 processSync(mapper);
7019 assertReceivedMove({69, 159});
7020
7021 // Move outside the physical display area. Since the pointer is already down, this should
7022 // now continue generating events.
7023 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7024 processSync(mapper);
7025 assertReceivedMove(kOutsidePoint);
7026
7027 // Release. This should generate a pointer up.
7028 processUp(mapper);
7029 processSync(mapper);
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7031 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7033 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7034
7035 // Ensure no more events were generated.
7036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7038 }
7039}
7040
Michael Wrightd02c5b62014-02-10 15:10:22 -08007041// --- MultiTouchInputMapperTest ---
7042
7043class MultiTouchInputMapperTest : public TouchInputMapperTest {
7044protected:
7045 void prepareAxes(int axes);
7046
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007047 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7048 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7049 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7050 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7051 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7052 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7053 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7054 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7055 void processId(MultiTouchInputMapper& mapper, int32_t id);
7056 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7057 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7058 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7059 void processMTSync(MultiTouchInputMapper& mapper);
7060 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007061};
7062
7063void MultiTouchInputMapperTest::prepareAxes(int axes) {
7064 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007065 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7066 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 }
7068 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007069 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7070 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007072 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7073 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074 }
7075 }
7076 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007077 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7078 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007079 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007080 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007081 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082 }
7083 }
7084 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007085 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7086 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007087 }
7088 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007089 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7090 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091 }
7092 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007093 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7094 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007095 }
7096 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7098 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 }
7100 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007101 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7102 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103 }
7104 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007105 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106 }
7107}
7108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007109void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7110 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113}
7114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007115void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7116 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007118}
7119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007120void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7121 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123}
7124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007125void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127}
7128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007129void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131}
7132
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007133void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7134 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007136}
7137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007138void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140}
7141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007142void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144}
7145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007146void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007148}
7149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007150void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152}
7153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007154void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156}
7157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007158void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7159 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161}
7162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007163void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165}
7166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007167void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169}
7170
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172 addConfigurationProperty("touch.deviceType", "touchScreen");
7173 prepareDisplay(DISPLAY_ORIENTATION_0);
7174 prepareAxes(POSITION);
7175 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007176 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177
arthurhungdcef2dc2020-08-11 14:47:50 +08007178 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179
7180 NotifyMotionArgs motionArgs;
7181
7182 // Two fingers down at once.
7183 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7184 processPosition(mapper, x1, y1);
7185 processMTSync(mapper);
7186 processPosition(mapper, x2, y2);
7187 processMTSync(mapper);
7188 processSync(mapper);
7189
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7191 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7192 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7193 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7194 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7195 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7196 ASSERT_EQ(0, motionArgs.flags);
7197 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7198 ASSERT_EQ(0, motionArgs.buttonState);
7199 ASSERT_EQ(0, motionArgs.edgeFlags);
7200 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7201 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7202 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7204 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7205 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7206 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7207 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7208
7209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7210 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7211 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7212 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7213 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007214 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215 ASSERT_EQ(0, motionArgs.flags);
7216 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7217 ASSERT_EQ(0, motionArgs.buttonState);
7218 ASSERT_EQ(0, motionArgs.edgeFlags);
7219 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7220 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7222 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7225 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7227 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7228 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7229 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7230 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7231
7232 // Move.
7233 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7234 processPosition(mapper, x1, y1);
7235 processMTSync(mapper);
7236 processPosition(mapper, x2, y2);
7237 processMTSync(mapper);
7238 processSync(mapper);
7239
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7241 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7242 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7243 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7244 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7246 ASSERT_EQ(0, motionArgs.flags);
7247 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7248 ASSERT_EQ(0, motionArgs.buttonState);
7249 ASSERT_EQ(0, motionArgs.edgeFlags);
7250 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7251 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7253 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7256 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7258 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7259 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7260 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7261 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7262
7263 // First finger up.
7264 x2 += 15; y2 -= 20;
7265 processPosition(mapper, x2, y2);
7266 processMTSync(mapper);
7267 processSync(mapper);
7268
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007274 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007275 ASSERT_EQ(0, motionArgs.flags);
7276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7277 ASSERT_EQ(0, motionArgs.buttonState);
7278 ASSERT_EQ(0, motionArgs.edgeFlags);
7279 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7282 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7285 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7287 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7288 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7289 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7290 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7291
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7298 ASSERT_EQ(0, motionArgs.flags);
7299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7300 ASSERT_EQ(0, motionArgs.buttonState);
7301 ASSERT_EQ(0, motionArgs.edgeFlags);
7302 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7303 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7306 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7307 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7308 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7310
7311 // Move.
7312 x2 += 20; y2 -= 25;
7313 processPosition(mapper, x2, y2);
7314 processMTSync(mapper);
7315 processSync(mapper);
7316
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7318 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7319 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7320 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7321 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7323 ASSERT_EQ(0, motionArgs.flags);
7324 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7325 ASSERT_EQ(0, motionArgs.buttonState);
7326 ASSERT_EQ(0, motionArgs.edgeFlags);
7327 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7328 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7331 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7332 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7333 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7334 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7335
7336 // New finger down.
7337 int32_t x3 = 700, y3 = 300;
7338 processPosition(mapper, x2, y2);
7339 processMTSync(mapper);
7340 processPosition(mapper, x3, y3);
7341 processMTSync(mapper);
7342 processSync(mapper);
7343
7344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7345 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7346 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7347 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7348 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007349 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007350 ASSERT_EQ(0, motionArgs.flags);
7351 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7352 ASSERT_EQ(0, motionArgs.buttonState);
7353 ASSERT_EQ(0, motionArgs.edgeFlags);
7354 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7355 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7357 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7360 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7362 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7363 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7364 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7366
7367 // Second finger up.
7368 x3 += 30; y3 -= 20;
7369 processPosition(mapper, x3, y3);
7370 processMTSync(mapper);
7371 processSync(mapper);
7372
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7375 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7376 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7377 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007378 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007379 ASSERT_EQ(0, motionArgs.flags);
7380 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7381 ASSERT_EQ(0, motionArgs.buttonState);
7382 ASSERT_EQ(0, motionArgs.edgeFlags);
7383 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7384 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7386 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7389 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7391 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7392 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7393 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7395
7396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7397 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7398 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7399 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7400 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7402 ASSERT_EQ(0, motionArgs.flags);
7403 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7404 ASSERT_EQ(0, motionArgs.buttonState);
7405 ASSERT_EQ(0, motionArgs.edgeFlags);
7406 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7407 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7410 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7411 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7412 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7413 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7414
7415 // Last finger up.
7416 processMTSync(mapper);
7417 processSync(mapper);
7418
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7421 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7422 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7423 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7424 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7425 ASSERT_EQ(0, motionArgs.flags);
7426 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7427 ASSERT_EQ(0, motionArgs.buttonState);
7428 ASSERT_EQ(0, motionArgs.edgeFlags);
7429 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7430 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7433 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7434 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7435 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7436 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7437
7438 // Should not have sent any more keys or motions.
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7441}
7442
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007443TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7444 addConfigurationProperty("touch.deviceType", "touchScreen");
7445 prepareDisplay(DISPLAY_ORIENTATION_0);
7446
7447 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7448 /*fuzz*/ 0, /*resolution*/ 10);
7449 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7450 /*fuzz*/ 0, /*resolution*/ 11);
7451 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7452 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7453 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7454 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7456 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7457 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7458 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7459
7460 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7461
7462 // X and Y axes
7463 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7464 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7465 // Touch major and minor
7466 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7467 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7468 // Tool major and minor
7469 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7470 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7471}
7472
7473TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7474 addConfigurationProperty("touch.deviceType", "touchScreen");
7475 prepareDisplay(DISPLAY_ORIENTATION_0);
7476
7477 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7478 /*fuzz*/ 0, /*resolution*/ 10);
7479 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7480 /*fuzz*/ 0, /*resolution*/ 11);
7481
7482 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7483
7484 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7485
7486 // Touch major and minor
7487 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7488 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7489 // Tool major and minor
7490 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7491 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7492}
7493
Michael Wrightd02c5b62014-02-10 15:10:22 -08007494TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007495 addConfigurationProperty("touch.deviceType", "touchScreen");
7496 prepareDisplay(DISPLAY_ORIENTATION_0);
7497 prepareAxes(POSITION | ID);
7498 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007499 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007500
arthurhungdcef2dc2020-08-11 14:47:50 +08007501 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007502
7503 NotifyMotionArgs motionArgs;
7504
7505 // Two fingers down at once.
7506 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7507 processPosition(mapper, x1, y1);
7508 processId(mapper, 1);
7509 processMTSync(mapper);
7510 processPosition(mapper, x2, y2);
7511 processId(mapper, 2);
7512 processMTSync(mapper);
7513 processSync(mapper);
7514
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7516 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7517 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7518 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7521 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7522
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007524 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7526 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7528 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7531 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7533 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7534
7535 // Move.
7536 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7537 processPosition(mapper, x1, y1);
7538 processId(mapper, 1);
7539 processMTSync(mapper);
7540 processPosition(mapper, x2, y2);
7541 processId(mapper, 2);
7542 processMTSync(mapper);
7543 processSync(mapper);
7544
7545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7547 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7548 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7550 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7553 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7555 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7556
7557 // First finger up.
7558 x2 += 15; y2 -= 20;
7559 processPosition(mapper, x2, y2);
7560 processId(mapper, 2);
7561 processMTSync(mapper);
7562 processSync(mapper);
7563
7564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007565 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7567 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7569 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7572 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7574 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7575
7576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7578 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7579 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7582 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7583
7584 // Move.
7585 x2 += 20; y2 -= 25;
7586 processPosition(mapper, x2, y2);
7587 processId(mapper, 2);
7588 processMTSync(mapper);
7589 processSync(mapper);
7590
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7593 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7594 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7597 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7598
7599 // New finger down.
7600 int32_t x3 = 700, y3 = 300;
7601 processPosition(mapper, x2, y2);
7602 processId(mapper, 2);
7603 processMTSync(mapper);
7604 processPosition(mapper, x3, y3);
7605 processId(mapper, 3);
7606 processMTSync(mapper);
7607 processSync(mapper);
7608
7609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007610 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7614 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7617 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7619 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7620
7621 // Second finger up.
7622 x3 += 30; y3 -= 20;
7623 processPosition(mapper, x3, y3);
7624 processId(mapper, 3);
7625 processMTSync(mapper);
7626 processSync(mapper);
7627
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007629 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007630 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7631 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7633 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7636 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7638 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7639
7640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7642 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7643 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7646 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7647
7648 // Last finger up.
7649 processMTSync(mapper);
7650 processSync(mapper);
7651
7652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7653 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7654 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7655 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7658 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7659
7660 // Should not have sent any more keys or motions.
7661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7663}
7664
7665TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007666 addConfigurationProperty("touch.deviceType", "touchScreen");
7667 prepareDisplay(DISPLAY_ORIENTATION_0);
7668 prepareAxes(POSITION | ID | SLOT);
7669 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007670 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007671
arthurhungdcef2dc2020-08-11 14:47:50 +08007672 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007673
7674 NotifyMotionArgs motionArgs;
7675
7676 // Two fingers down at once.
7677 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7678 processPosition(mapper, x1, y1);
7679 processId(mapper, 1);
7680 processSlot(mapper, 1);
7681 processPosition(mapper, x2, y2);
7682 processId(mapper, 2);
7683 processSync(mapper);
7684
7685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7686 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7692
7693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007694 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007695 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7696 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7698 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7701 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7703 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7704
7705 // Move.
7706 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7707 processSlot(mapper, 0);
7708 processPosition(mapper, x1, y1);
7709 processSlot(mapper, 1);
7710 processPosition(mapper, x2, y2);
7711 processSync(mapper);
7712
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7715 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7716 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7718 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7721 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7723 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7724
7725 // First finger up.
7726 x2 += 15; y2 -= 20;
7727 processSlot(mapper, 0);
7728 processId(mapper, -1);
7729 processSlot(mapper, 1);
7730 processPosition(mapper, x2, y2);
7731 processSync(mapper);
7732
7733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007734 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007735 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7736 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7738 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7741 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7743 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7744
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7747 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7748 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7751 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7752
7753 // Move.
7754 x2 += 20; y2 -= 25;
7755 processPosition(mapper, x2, y2);
7756 processSync(mapper);
7757
7758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7760 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7761 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7764 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7765
7766 // New finger down.
7767 int32_t x3 = 700, y3 = 300;
7768 processPosition(mapper, x2, y2);
7769 processSlot(mapper, 0);
7770 processId(mapper, 3);
7771 processPosition(mapper, x3, y3);
7772 processSync(mapper);
7773
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007775 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007776 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7779 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7782 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7784 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7785
7786 // Second finger up.
7787 x3 += 30; y3 -= 20;
7788 processSlot(mapper, 1);
7789 processId(mapper, -1);
7790 processSlot(mapper, 0);
7791 processPosition(mapper, x3, y3);
7792 processSync(mapper);
7793
7794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007795 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7797 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7799 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7802 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7804 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7805
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7808 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7809 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7812 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7813
7814 // Last finger up.
7815 processId(mapper, -1);
7816 processSync(mapper);
7817
7818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7819 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7820 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7821 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7824 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7825
7826 // Should not have sent any more keys or motions.
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7829}
7830
7831TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007832 addConfigurationProperty("touch.deviceType", "touchScreen");
7833 prepareDisplay(DISPLAY_ORIENTATION_0);
7834 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007835 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836
7837 // These calculations are based on the input device calibration documentation.
7838 int32_t rawX = 100;
7839 int32_t rawY = 200;
7840 int32_t rawTouchMajor = 7;
7841 int32_t rawTouchMinor = 6;
7842 int32_t rawToolMajor = 9;
7843 int32_t rawToolMinor = 8;
7844 int32_t rawPressure = 11;
7845 int32_t rawDistance = 0;
7846 int32_t rawOrientation = 3;
7847 int32_t id = 5;
7848
7849 float x = toDisplayX(rawX);
7850 float y = toDisplayY(rawY);
7851 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7852 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7853 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7854 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7855 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7856 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7857 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7858 float distance = float(rawDistance);
7859
7860 processPosition(mapper, rawX, rawY);
7861 processTouchMajor(mapper, rawTouchMajor);
7862 processTouchMinor(mapper, rawTouchMinor);
7863 processToolMajor(mapper, rawToolMajor);
7864 processToolMinor(mapper, rawToolMinor);
7865 processPressure(mapper, rawPressure);
7866 processOrientation(mapper, rawOrientation);
7867 processDistance(mapper, rawDistance);
7868 processId(mapper, id);
7869 processMTSync(mapper);
7870 processSync(mapper);
7871
7872 NotifyMotionArgs args;
7873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7874 ASSERT_EQ(0, args.pointerProperties[0].id);
7875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7876 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7877 orientation, distance));
7878}
7879
7880TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007881 addConfigurationProperty("touch.deviceType", "touchScreen");
7882 prepareDisplay(DISPLAY_ORIENTATION_0);
7883 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7884 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007885 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886
7887 // These calculations are based on the input device calibration documentation.
7888 int32_t rawX = 100;
7889 int32_t rawY = 200;
7890 int32_t rawTouchMajor = 140;
7891 int32_t rawTouchMinor = 120;
7892 int32_t rawToolMajor = 180;
7893 int32_t rawToolMinor = 160;
7894
7895 float x = toDisplayX(rawX);
7896 float y = toDisplayY(rawY);
7897 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7898 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7899 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7900 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7901 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7902
7903 processPosition(mapper, rawX, rawY);
7904 processTouchMajor(mapper, rawTouchMajor);
7905 processTouchMinor(mapper, rawTouchMinor);
7906 processToolMajor(mapper, rawToolMajor);
7907 processToolMinor(mapper, rawToolMinor);
7908 processMTSync(mapper);
7909 processSync(mapper);
7910
7911 NotifyMotionArgs args;
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7914 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7915}
7916
7917TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918 addConfigurationProperty("touch.deviceType", "touchScreen");
7919 prepareDisplay(DISPLAY_ORIENTATION_0);
7920 prepareAxes(POSITION | TOUCH | TOOL);
7921 addConfigurationProperty("touch.size.calibration", "diameter");
7922 addConfigurationProperty("touch.size.scale", "10");
7923 addConfigurationProperty("touch.size.bias", "160");
7924 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007925 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007926
7927 // These calculations are based on the input device calibration documentation.
7928 // Note: We only provide a single common touch/tool value because the device is assumed
7929 // not to emit separate values for each pointer (isSummed = 1).
7930 int32_t rawX = 100;
7931 int32_t rawY = 200;
7932 int32_t rawX2 = 150;
7933 int32_t rawY2 = 250;
7934 int32_t rawTouchMajor = 5;
7935 int32_t rawToolMajor = 8;
7936
7937 float x = toDisplayX(rawX);
7938 float y = toDisplayY(rawY);
7939 float x2 = toDisplayX(rawX2);
7940 float y2 = toDisplayY(rawY2);
7941 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7942 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7943 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7944
7945 processPosition(mapper, rawX, rawY);
7946 processTouchMajor(mapper, rawTouchMajor);
7947 processToolMajor(mapper, rawToolMajor);
7948 processMTSync(mapper);
7949 processPosition(mapper, rawX2, rawY2);
7950 processTouchMajor(mapper, rawTouchMajor);
7951 processToolMajor(mapper, rawToolMajor);
7952 processMTSync(mapper);
7953 processSync(mapper);
7954
7955 NotifyMotionArgs args;
7956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7957 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7958
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007960 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961 ASSERT_EQ(size_t(2), args.pointerCount);
7962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7963 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7965 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7966}
7967
7968TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969 addConfigurationProperty("touch.deviceType", "touchScreen");
7970 prepareDisplay(DISPLAY_ORIENTATION_0);
7971 prepareAxes(POSITION | TOUCH | TOOL);
7972 addConfigurationProperty("touch.size.calibration", "area");
7973 addConfigurationProperty("touch.size.scale", "43");
7974 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007975 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007976
7977 // These calculations are based on the input device calibration documentation.
7978 int32_t rawX = 100;
7979 int32_t rawY = 200;
7980 int32_t rawTouchMajor = 5;
7981 int32_t rawToolMajor = 8;
7982
7983 float x = toDisplayX(rawX);
7984 float y = toDisplayY(rawY);
7985 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7986 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7987 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7988
7989 processPosition(mapper, rawX, rawY);
7990 processTouchMajor(mapper, rawTouchMajor);
7991 processToolMajor(mapper, rawToolMajor);
7992 processMTSync(mapper);
7993 processSync(mapper);
7994
7995 NotifyMotionArgs args;
7996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7998 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7999}
8000
8001TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008002 addConfigurationProperty("touch.deviceType", "touchScreen");
8003 prepareDisplay(DISPLAY_ORIENTATION_0);
8004 prepareAxes(POSITION | PRESSURE);
8005 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8006 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008007 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008
Michael Wrightaa449c92017-12-13 21:21:43 +00008009 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008010 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008011 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8012 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8013 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8014
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015 // These calculations are based on the input device calibration documentation.
8016 int32_t rawX = 100;
8017 int32_t rawY = 200;
8018 int32_t rawPressure = 60;
8019
8020 float x = toDisplayX(rawX);
8021 float y = toDisplayY(rawY);
8022 float pressure = float(rawPressure) * 0.01f;
8023
8024 processPosition(mapper, rawX, rawY);
8025 processPressure(mapper, rawPressure);
8026 processMTSync(mapper);
8027 processSync(mapper);
8028
8029 NotifyMotionArgs args;
8030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8032 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8033}
8034
8035TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 addConfigurationProperty("touch.deviceType", "touchScreen");
8037 prepareDisplay(DISPLAY_ORIENTATION_0);
8038 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008039 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040
8041 NotifyMotionArgs motionArgs;
8042 NotifyKeyArgs keyArgs;
8043
8044 processId(mapper, 1);
8045 processPosition(mapper, 100, 200);
8046 processSync(mapper);
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8048 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8049 ASSERT_EQ(0, motionArgs.buttonState);
8050
8051 // press BTN_LEFT, release BTN_LEFT
8052 processKey(mapper, BTN_LEFT, 1);
8053 processSync(mapper);
8054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8056 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8057
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8059 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8060 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8061
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062 processKey(mapper, BTN_LEFT, 0);
8063 processSync(mapper);
8064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008065 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008067
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008070 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071
8072 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8073 processKey(mapper, BTN_RIGHT, 1);
8074 processKey(mapper, BTN_MIDDLE, 1);
8075 processSync(mapper);
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8078 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8079 motionArgs.buttonState);
8080
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8083 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8084
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8087 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8088 motionArgs.buttonState);
8089
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 processKey(mapper, BTN_RIGHT, 0);
8091 processSync(mapper);
8092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008095
8096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008098 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099
8100 processKey(mapper, BTN_MIDDLE, 0);
8101 processSync(mapper);
8102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008105
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008108 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008109
8110 // press BTN_BACK, release BTN_BACK
8111 processKey(mapper, BTN_BACK, 1);
8112 processSync(mapper);
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8114 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8115 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008116
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008119 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8120
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8123 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124
8125 processKey(mapper, BTN_BACK, 0);
8126 processSync(mapper);
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008128 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008130
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008133 ASSERT_EQ(0, motionArgs.buttonState);
8134
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8136 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8137 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8138
8139 // press BTN_SIDE, release BTN_SIDE
8140 processKey(mapper, BTN_SIDE, 1);
8141 processSync(mapper);
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8143 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8144 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008145
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008148 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8149
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8151 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8152 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153
8154 processKey(mapper, BTN_SIDE, 0);
8155 processSync(mapper);
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162 ASSERT_EQ(0, motionArgs.buttonState);
8163
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8165 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8166 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8167
8168 // press BTN_FORWARD, release BTN_FORWARD
8169 processKey(mapper, BTN_FORWARD, 1);
8170 processSync(mapper);
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8172 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8173 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008174
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008177 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8178
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8180 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8181 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182
8183 processKey(mapper, BTN_FORWARD, 0);
8184 processSync(mapper);
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008188
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191 ASSERT_EQ(0, motionArgs.buttonState);
8192
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8194 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8195 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8196
8197 // press BTN_EXTRA, release BTN_EXTRA
8198 processKey(mapper, BTN_EXTRA, 1);
8199 processSync(mapper);
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8201 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8202 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008203
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008206 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8207
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8209 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8210 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211
8212 processKey(mapper, BTN_EXTRA, 0);
8213 processSync(mapper);
8214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008216 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008217
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220 ASSERT_EQ(0, motionArgs.buttonState);
8221
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8223 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8224 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8225
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8227
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228 // press BTN_STYLUS, release BTN_STYLUS
8229 processKey(mapper, BTN_STYLUS, 1);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008233 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8234
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8237 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238
8239 processKey(mapper, BTN_STYLUS, 0);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008244
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008248
8249 // press BTN_STYLUS2, release BTN_STYLUS2
8250 processKey(mapper, BTN_STYLUS2, 1);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008254 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8255
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8258 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259
8260 processKey(mapper, BTN_STYLUS2, 0);
8261 processSync(mapper);
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008263 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008265
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008268 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269
8270 // release touch
8271 processId(mapper, -1);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8274 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8275 ASSERT_EQ(0, motionArgs.buttonState);
8276}
8277
8278TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 addConfigurationProperty("touch.deviceType", "touchScreen");
8280 prepareDisplay(DISPLAY_ORIENTATION_0);
8281 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008282 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283
8284 NotifyMotionArgs motionArgs;
8285
8286 // default tool type is finger
8287 processId(mapper, 1);
8288 processPosition(mapper, 100, 200);
8289 processSync(mapper);
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8291 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8293
8294 // eraser
8295 processKey(mapper, BTN_TOOL_RUBBER, 1);
8296 processSync(mapper);
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8300
8301 // stylus
8302 processKey(mapper, BTN_TOOL_RUBBER, 0);
8303 processKey(mapper, BTN_TOOL_PEN, 1);
8304 processSync(mapper);
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8308
8309 // brush
8310 processKey(mapper, BTN_TOOL_PEN, 0);
8311 processKey(mapper, BTN_TOOL_BRUSH, 1);
8312 processSync(mapper);
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8316
8317 // pencil
8318 processKey(mapper, BTN_TOOL_BRUSH, 0);
8319 processKey(mapper, BTN_TOOL_PENCIL, 1);
8320 processSync(mapper);
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8324
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008325 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326 processKey(mapper, BTN_TOOL_PENCIL, 0);
8327 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8332
8333 // mouse
8334 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8335 processKey(mapper, BTN_TOOL_MOUSE, 1);
8336 processSync(mapper);
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8340
8341 // lens
8342 processKey(mapper, BTN_TOOL_MOUSE, 0);
8343 processKey(mapper, BTN_TOOL_LENS, 1);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8348
8349 // double-tap
8350 processKey(mapper, BTN_TOOL_LENS, 0);
8351 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8356
8357 // triple-tap
8358 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8359 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8360 processSync(mapper);
8361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8364
8365 // quad-tap
8366 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8367 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8368 processSync(mapper);
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8372
8373 // finger
8374 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8375 processKey(mapper, BTN_TOOL_FINGER, 1);
8376 processSync(mapper);
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8380
8381 // stylus trumps finger
8382 processKey(mapper, BTN_TOOL_PEN, 1);
8383 processSync(mapper);
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8387
8388 // eraser trumps stylus
8389 processKey(mapper, BTN_TOOL_RUBBER, 1);
8390 processSync(mapper);
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8394
8395 // mouse trumps eraser
8396 processKey(mapper, BTN_TOOL_MOUSE, 1);
8397 processSync(mapper);
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8401
8402 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8403 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8404 processSync(mapper);
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8408
8409 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8410 processToolType(mapper, MT_TOOL_PEN);
8411 processSync(mapper);
8412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8415
8416 // back to default tool type
8417 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8418 processKey(mapper, BTN_TOOL_MOUSE, 0);
8419 processKey(mapper, BTN_TOOL_RUBBER, 0);
8420 processKey(mapper, BTN_TOOL_PEN, 0);
8421 processKey(mapper, BTN_TOOL_FINGER, 0);
8422 processSync(mapper);
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8426}
8427
8428TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429 addConfigurationProperty("touch.deviceType", "touchScreen");
8430 prepareDisplay(DISPLAY_ORIENTATION_0);
8431 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008432 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008433 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434
8435 NotifyMotionArgs motionArgs;
8436
8437 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8438 processId(mapper, 1);
8439 processPosition(mapper, 100, 200);
8440 processSync(mapper);
8441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8442 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8444 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8445
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8447 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8449 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8450
8451 // move a little
8452 processPosition(mapper, 150, 250);
8453 processSync(mapper);
8454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8455 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8457 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8458
8459 // down when BTN_TOUCH is pressed, pressure defaults to 1
8460 processKey(mapper, BTN_TOUCH, 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8465 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8466
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8468 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8470 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8471
8472 // up when BTN_TOUCH is released, hover restored
8473 processKey(mapper, BTN_TOUCH, 0);
8474 processSync(mapper);
8475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8476 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8478 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8479
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8483 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8484
8485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8488 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8489
8490 // exit hover when pointer goes away
8491 processId(mapper, -1);
8492 processSync(mapper);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8496 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8497}
8498
8499TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008500 addConfigurationProperty("touch.deviceType", "touchScreen");
8501 prepareDisplay(DISPLAY_ORIENTATION_0);
8502 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008503 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504
8505 NotifyMotionArgs motionArgs;
8506
8507 // initially hovering because pressure is 0
8508 processId(mapper, 1);
8509 processPosition(mapper, 100, 200);
8510 processPressure(mapper, 0);
8511 processSync(mapper);
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8515 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8516
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8518 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8520 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8521
8522 // move a little
8523 processPosition(mapper, 150, 250);
8524 processSync(mapper);
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8526 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8528 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8529
8530 // down when pressure becomes non-zero
8531 processPressure(mapper, RAW_PRESSURE_MAX);
8532 processSync(mapper);
8533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8534 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8536 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8537
8538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8539 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8541 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8542
8543 // up when pressure becomes 0, hover restored
8544 processPressure(mapper, 0);
8545 processSync(mapper);
8546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8547 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8549 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8550
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8554 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8555
8556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8559 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8560
8561 // exit hover when pointer goes away
8562 processId(mapper, -1);
8563 processSync(mapper);
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8567 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8568}
8569
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008570/**
8571 * Set the input device port <--> display port associations, and check that the
8572 * events are routed to the display that matches the display port.
8573 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8574 */
8575TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008576 const std::string usb2 = "USB2";
8577 const uint8_t hdmi1 = 0;
8578 const uint8_t hdmi2 = 1;
8579 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008580 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008581
8582 addConfigurationProperty("touch.deviceType", "touchScreen");
8583 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008584 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008585
8586 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8587 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8588
8589 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8590 // for this input device is specified, and the matching viewport is not present,
8591 // the input device should be disabled (at the mapper level).
8592
8593 // Add viewport for display 2 on hdmi2
8594 prepareSecondaryDisplay(type, hdmi2);
8595 // Send a touch event
8596 processPosition(mapper, 100, 100);
8597 processSync(mapper);
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8599
8600 // Add viewport for display 1 on hdmi1
8601 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8602 // Send a touch event again
8603 processPosition(mapper, 100, 100);
8604 processSync(mapper);
8605
8606 NotifyMotionArgs args;
8607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8608 ASSERT_EQ(DISPLAY_ID, args.displayId);
8609}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008610
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008611TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8612 addConfigurationProperty("touch.deviceType", "touchScreen");
8613 prepareAxes(POSITION);
8614 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8615
8616 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8617
8618 prepareDisplay(DISPLAY_ORIENTATION_0);
8619 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8620
8621 // Send a touch event
8622 processPosition(mapper, 100, 100);
8623 processSync(mapper);
8624
8625 NotifyMotionArgs args;
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8627 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8628}
8629
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008630TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008631 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008632 std::shared_ptr<FakePointerController> fakePointerController =
8633 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008634 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008635 fakePointerController->setPosition(100, 200);
8636 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008637 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008638
Garfield Tan888a6a42020-01-09 11:39:16 -08008639 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008640 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008641
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008642 prepareDisplay(DISPLAY_ORIENTATION_0);
8643 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008644 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008645
8646 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008647 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008648
8649 NotifyMotionArgs motionArgs;
8650 processPosition(mapper, 100, 100);
8651 processSync(mapper);
8652
8653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8654 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8655 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8656}
8657
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008658/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008659 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8660 */
8661TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8662 addConfigurationProperty("touch.deviceType", "touchScreen");
8663 prepareAxes(POSITION);
8664 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8665
8666 prepareDisplay(DISPLAY_ORIENTATION_0);
8667 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8668 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8669 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8670 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8671
8672 NotifyMotionArgs args;
8673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8674 ASSERT_EQ(26, args.readTime);
8675
8676 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8677 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8678 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8679
8680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8681 ASSERT_EQ(33, args.readTime);
8682}
8683
8684/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008685 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8686 * events should not be delivered to the listener.
8687 */
8688TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8689 addConfigurationProperty("touch.deviceType", "touchScreen");
8690 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8691 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8692 ViewportType::INTERNAL);
8693 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8694 prepareAxes(POSITION);
8695 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8696
8697 NotifyMotionArgs motionArgs;
8698 processPosition(mapper, 100, 100);
8699 processSync(mapper);
8700
8701 mFakeListener->assertNotifyMotionWasNotCalled();
8702}
8703
Garfield Tanc734e4f2021-01-15 20:01:39 -08008704TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8705 addConfigurationProperty("touch.deviceType", "touchScreen");
8706 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8707 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8708 ViewportType::INTERNAL);
8709 std::optional<DisplayViewport> optionalDisplayViewport =
8710 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8711 ASSERT_TRUE(optionalDisplayViewport.has_value());
8712 DisplayViewport displayViewport = *optionalDisplayViewport;
8713
8714 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8715 prepareAxes(POSITION);
8716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8717
8718 // Finger down
8719 int32_t x = 100, y = 100;
8720 processPosition(mapper, x, y);
8721 processSync(mapper);
8722
8723 NotifyMotionArgs motionArgs;
8724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8725 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8726
8727 // Deactivate display viewport
8728 displayViewport.isActive = false;
8729 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8730 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8731
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008732 // The ongoing touch should be canceled immediately
8733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8734 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8735
8736 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008737 x += 10, y += 10;
8738 processPosition(mapper, x, y);
8739 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008741
8742 // Reactivate display viewport
8743 displayViewport.isActive = true;
8744 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8745 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8746
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008747 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008748 x += 10, y += 10;
8749 processPosition(mapper, x, y);
8750 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8752 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008753}
8754
Arthur Hung7c645402019-01-25 17:45:42 +08008755TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8756 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008757 prepareAxes(POSITION | ID | SLOT);
8758 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008759 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008760
8761 // Create the second touch screen device, and enable multi fingers.
8762 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008763 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008764 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008765 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008766 std::shared_ptr<InputDevice> device2 =
8767 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008768 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008770 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8771 0 /*flat*/, 0 /*fuzz*/);
8772 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8773 0 /*flat*/, 0 /*fuzz*/);
8774 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8775 0 /*flat*/, 0 /*fuzz*/);
8776 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8777 0 /*flat*/, 0 /*fuzz*/);
8778 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8779 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8780 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008781
8782 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008783 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008784 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8785 device2->reset(ARBITRARY_TIME);
8786
8787 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008788 std::shared_ptr<FakePointerController> fakePointerController =
8789 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008790 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008791
8792 // Setup policy for associated displays and show touches.
8793 const uint8_t hdmi1 = 0;
8794 const uint8_t hdmi2 = 1;
8795 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8796 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8797 mFakePolicy->setShowTouches(true);
8798
8799 // Create displays.
8800 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008801 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008802
8803 // Default device will reconfigure above, need additional reconfiguration for another device.
8804 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008805 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8806 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008807
8808 // Two fingers down at default display.
8809 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8810 processPosition(mapper, x1, y1);
8811 processId(mapper, 1);
8812 processSlot(mapper, 1);
8813 processPosition(mapper, x2, y2);
8814 processId(mapper, 2);
8815 processSync(mapper);
8816
8817 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8818 fakePointerController->getSpots().find(DISPLAY_ID);
8819 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8820 ASSERT_EQ(size_t(2), iter->second.size());
8821
8822 // Two fingers down at second display.
8823 processPosition(mapper2, x1, y1);
8824 processId(mapper2, 1);
8825 processSlot(mapper2, 1);
8826 processPosition(mapper2, x2, y2);
8827 processId(mapper2, 2);
8828 processSync(mapper2);
8829
8830 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8831 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8832 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008833
8834 // Disable the show touches configuration and ensure the spots are cleared.
8835 mFakePolicy->setShowTouches(false);
8836 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8837 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8838
8839 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008840}
8841
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008842TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008843 prepareAxes(POSITION);
8844 addConfigurationProperty("touch.deviceType", "touchScreen");
8845 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008846 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008847
8848 NotifyMotionArgs motionArgs;
8849 // Unrotated video frame
8850 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8851 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008852 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008853 processPosition(mapper, 100, 200);
8854 processSync(mapper);
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8856 ASSERT_EQ(frames, motionArgs.videoFrames);
8857
8858 // Subsequent touch events should not have any videoframes
8859 // This is implemented separately in FakeEventHub,
8860 // but that should match the behaviour of TouchVideoDevice.
8861 processPosition(mapper, 200, 200);
8862 processSync(mapper);
8863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8864 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8865}
8866
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008867TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008868 prepareAxes(POSITION);
8869 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008870 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008871 // Unrotated video frame
8872 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8873 NotifyMotionArgs motionArgs;
8874
8875 // Test all 4 orientations
8876 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008877 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8878 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8879 clearViewports();
8880 prepareDisplay(orientation);
8881 std::vector<TouchVideoFrame> frames{frame};
8882 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8883 processPosition(mapper, 100, 200);
8884 processSync(mapper);
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8886 ASSERT_EQ(frames, motionArgs.videoFrames);
8887 }
8888}
8889
8890TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8891 prepareAxes(POSITION);
8892 addConfigurationProperty("touch.deviceType", "touchScreen");
8893 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8894 // orientation-aware are affected by display rotation.
8895 addConfigurationProperty("touch.orientationAware", "0");
8896 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8897 // Unrotated video frame
8898 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8899 NotifyMotionArgs motionArgs;
8900
8901 // Test all 4 orientations
8902 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008903 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8904 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8905 clearViewports();
8906 prepareDisplay(orientation);
8907 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008908 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008909 processPosition(mapper, 100, 200);
8910 processSync(mapper);
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008912 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8913 // compared to the display. This is so that when the window transform (which contains the
8914 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8915 // window's coordinate space.
8916 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008917 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008918
8919 // Release finger.
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008922 }
8923}
8924
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008925TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008926 prepareAxes(POSITION);
8927 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008928 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008929 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8930 // so mix these.
8931 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8932 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8933 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8934 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8935 NotifyMotionArgs motionArgs;
8936
8937 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008938 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008939 processPosition(mapper, 100, 200);
8940 processSync(mapper);
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008942 ASSERT_EQ(frames, motionArgs.videoFrames);
8943}
8944
8945TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8946 prepareAxes(POSITION);
8947 addConfigurationProperty("touch.deviceType", "touchScreen");
8948 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8949 // orientation-aware are affected by display rotation.
8950 addConfigurationProperty("touch.orientationAware", "0");
8951 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8952 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8953 // so mix these.
8954 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8955 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8956 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8957 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8958 NotifyMotionArgs motionArgs;
8959
8960 prepareDisplay(DISPLAY_ORIENTATION_90);
8961 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8962 processPosition(mapper, 100, 200);
8963 processSync(mapper);
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8965 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8966 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8967 // compared to the display. This is so that when the window transform (which contains the
8968 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8969 // window's coordinate space.
8970 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8971 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008972 ASSERT_EQ(frames, motionArgs.videoFrames);
8973}
8974
Arthur Hung9da14732019-09-02 16:16:58 +08008975/**
8976 * If we had defined port associations, but the viewport is not ready, the touch device would be
8977 * expected to be disabled, and it should be enabled after the viewport has found.
8978 */
8979TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008980 constexpr uint8_t hdmi2 = 1;
8981 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008982 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008983
8984 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8985
8986 addConfigurationProperty("touch.deviceType", "touchScreen");
8987 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008988 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008989
8990 ASSERT_EQ(mDevice->isEnabled(), false);
8991
8992 // Add display on hdmi2, the device should be enabled and can receive touch event.
8993 prepareSecondaryDisplay(type, hdmi2);
8994 ASSERT_EQ(mDevice->isEnabled(), true);
8995
8996 // Send a touch event.
8997 processPosition(mapper, 100, 100);
8998 processSync(mapper);
8999
9000 NotifyMotionArgs args;
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9002 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9003}
9004
Arthur Hung421eb1c2020-01-16 00:09:42 +08009005TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009006 addConfigurationProperty("touch.deviceType", "touchScreen");
9007 prepareDisplay(DISPLAY_ORIENTATION_0);
9008 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009009 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009010
9011 NotifyMotionArgs motionArgs;
9012
9013 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9014 // finger down
9015 processId(mapper, 1);
9016 processPosition(mapper, x1, y1);
9017 processSync(mapper);
9018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9019 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9021
9022 // finger move
9023 processId(mapper, 1);
9024 processPosition(mapper, x2, y2);
9025 processSync(mapper);
9026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9029
9030 // finger up.
9031 processId(mapper, -1);
9032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9034 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9036
9037 // new finger down
9038 processId(mapper, 1);
9039 processPosition(mapper, x3, y3);
9040 processSync(mapper);
9041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9042 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9044}
9045
9046/**
arthurhungcc7f9802020-04-30 17:55:40 +08009047 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9048 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009049 */
arthurhungcc7f9802020-04-30 17:55:40 +08009050TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009051 addConfigurationProperty("touch.deviceType", "touchScreen");
9052 prepareDisplay(DISPLAY_ORIENTATION_0);
9053 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009054 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009055
9056 NotifyMotionArgs motionArgs;
9057
9058 // default tool type is finger
9059 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009060 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009061 processPosition(mapper, x1, y1);
9062 processSync(mapper);
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9064 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9065 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9066
9067 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9068 processToolType(mapper, MT_TOOL_PALM);
9069 processSync(mapper);
9070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9071 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9072
9073 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009074 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009075 processPosition(mapper, x2, y2);
9076 processSync(mapper);
9077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9078
9079 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009080 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009081 processSync(mapper);
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9083
9084 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009085 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009086 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009087 processPosition(mapper, x3, y3);
9088 processSync(mapper);
9089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9090 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9092}
9093
arthurhungbf89a482020-04-17 17:37:55 +08009094/**
arthurhungcc7f9802020-04-30 17:55:40 +08009095 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9096 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009097 */
arthurhungcc7f9802020-04-30 17:55:40 +08009098TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009099 addConfigurationProperty("touch.deviceType", "touchScreen");
9100 prepareDisplay(DISPLAY_ORIENTATION_0);
9101 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9102 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9103
9104 NotifyMotionArgs motionArgs;
9105
9106 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009107 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9108 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009109 processPosition(mapper, x1, y1);
9110 processSync(mapper);
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9114
9115 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009116 processSlot(mapper, SECOND_SLOT);
9117 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009118 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009119 processSync(mapper);
9120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009121 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9123
9124 // If the tool type of the first finger changes to MT_TOOL_PALM,
9125 // we expect to receive ACTION_POINTER_UP with cancel flag.
9126 processSlot(mapper, FIRST_SLOT);
9127 processId(mapper, FIRST_TRACKING_ID);
9128 processToolType(mapper, MT_TOOL_PALM);
9129 processSync(mapper);
9130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009131 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009132 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9133
9134 // The following MOVE events of second finger should be processed.
9135 processSlot(mapper, SECOND_SLOT);
9136 processId(mapper, SECOND_TRACKING_ID);
9137 processPosition(mapper, x2 + 1, y2 + 1);
9138 processSync(mapper);
9139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9141 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9142
9143 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9144 // it. Second finger receive move.
9145 processSlot(mapper, FIRST_SLOT);
9146 processId(mapper, INVALID_TRACKING_ID);
9147 processSync(mapper);
9148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9150 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9151
9152 // Second finger keeps moving.
9153 processSlot(mapper, SECOND_SLOT);
9154 processId(mapper, SECOND_TRACKING_ID);
9155 processPosition(mapper, x2 + 2, y2 + 2);
9156 processSync(mapper);
9157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9159 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9160
9161 // Second finger up.
9162 processId(mapper, INVALID_TRACKING_ID);
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9165 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9166 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9167}
9168
9169/**
9170 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9171 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9172 */
9173TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9174 addConfigurationProperty("touch.deviceType", "touchScreen");
9175 prepareDisplay(DISPLAY_ORIENTATION_0);
9176 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9177 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9178
9179 NotifyMotionArgs motionArgs;
9180
9181 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9182 // First finger down.
9183 processId(mapper, FIRST_TRACKING_ID);
9184 processPosition(mapper, x1, y1);
9185 processSync(mapper);
9186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9187 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9189
9190 // Second finger down.
9191 processSlot(mapper, SECOND_SLOT);
9192 processId(mapper, SECOND_TRACKING_ID);
9193 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009194 processSync(mapper);
9195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009196 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9198
arthurhungcc7f9802020-04-30 17:55:40 +08009199 // If the tool type of the first finger changes to MT_TOOL_PALM,
9200 // we expect to receive ACTION_POINTER_UP with cancel flag.
9201 processSlot(mapper, FIRST_SLOT);
9202 processId(mapper, FIRST_TRACKING_ID);
9203 processToolType(mapper, MT_TOOL_PALM);
9204 processSync(mapper);
9205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009206 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009207 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9208
9209 // Second finger keeps moving.
9210 processSlot(mapper, SECOND_SLOT);
9211 processId(mapper, SECOND_TRACKING_ID);
9212 processPosition(mapper, x2 + 1, y2 + 1);
9213 processSync(mapper);
9214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9216
9217 // second finger becomes palm, receive cancel due to only 1 finger is active.
9218 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009219 processToolType(mapper, MT_TOOL_PALM);
9220 processSync(mapper);
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9222 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9223
arthurhungcc7f9802020-04-30 17:55:40 +08009224 // third finger down.
9225 processSlot(mapper, THIRD_SLOT);
9226 processId(mapper, THIRD_TRACKING_ID);
9227 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009228 processPosition(mapper, x3, y3);
9229 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9231 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009233 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9234
9235 // third finger move
9236 processId(mapper, THIRD_TRACKING_ID);
9237 processPosition(mapper, x3 + 1, y3 + 1);
9238 processSync(mapper);
9239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9241
9242 // first finger up, third finger receive move.
9243 processSlot(mapper, FIRST_SLOT);
9244 processId(mapper, INVALID_TRACKING_ID);
9245 processSync(mapper);
9246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9248 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9249
9250 // second finger up, third finger receive move.
9251 processSlot(mapper, SECOND_SLOT);
9252 processId(mapper, INVALID_TRACKING_ID);
9253 processSync(mapper);
9254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9256 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9257
9258 // third finger up.
9259 processSlot(mapper, THIRD_SLOT);
9260 processId(mapper, INVALID_TRACKING_ID);
9261 processSync(mapper);
9262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9263 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9264 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9265}
9266
9267/**
9268 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9269 * and the active finger could still be allowed to receive the events
9270 */
9271TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9272 addConfigurationProperty("touch.deviceType", "touchScreen");
9273 prepareDisplay(DISPLAY_ORIENTATION_0);
9274 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9275 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9276
9277 NotifyMotionArgs motionArgs;
9278
9279 // default tool type is finger
9280 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9281 processId(mapper, FIRST_TRACKING_ID);
9282 processPosition(mapper, x1, y1);
9283 processSync(mapper);
9284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9285 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9287
9288 // Second finger down.
9289 processSlot(mapper, SECOND_SLOT);
9290 processId(mapper, SECOND_TRACKING_ID);
9291 processPosition(mapper, x2, y2);
9292 processSync(mapper);
9293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009294 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9296
9297 // If the tool type of the second finger changes to MT_TOOL_PALM,
9298 // we expect to receive ACTION_POINTER_UP with cancel flag.
9299 processId(mapper, SECOND_TRACKING_ID);
9300 processToolType(mapper, MT_TOOL_PALM);
9301 processSync(mapper);
9302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009303 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009304 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9305
9306 // The following MOVE event should be processed.
9307 processSlot(mapper, FIRST_SLOT);
9308 processId(mapper, FIRST_TRACKING_ID);
9309 processPosition(mapper, x1 + 1, y1 + 1);
9310 processSync(mapper);
9311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9313 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9314
9315 // second finger up.
9316 processSlot(mapper, SECOND_SLOT);
9317 processId(mapper, INVALID_TRACKING_ID);
9318 processSync(mapper);
9319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9321
9322 // first finger keep moving
9323 processSlot(mapper, FIRST_SLOT);
9324 processId(mapper, FIRST_TRACKING_ID);
9325 processPosition(mapper, x1 + 2, y1 + 2);
9326 processSync(mapper);
9327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9329
9330 // first finger up.
9331 processId(mapper, INVALID_TRACKING_ID);
9332 processSync(mapper);
9333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9334 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9335 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009336}
9337
Arthur Hung9ad18942021-06-19 02:04:46 +00009338/**
9339 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9340 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9341 * cause slot be valid again.
9342 */
9343TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9344 addConfigurationProperty("touch.deviceType", "touchScreen");
9345 prepareDisplay(DISPLAY_ORIENTATION_0);
9346 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9347 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9348
9349 NotifyMotionArgs motionArgs;
9350
9351 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9352 // First finger down.
9353 processId(mapper, FIRST_TRACKING_ID);
9354 processPosition(mapper, x1, y1);
9355 processPressure(mapper, RAW_PRESSURE_MAX);
9356 processSync(mapper);
9357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9358 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9359 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9360
9361 // First finger move.
9362 processId(mapper, FIRST_TRACKING_ID);
9363 processPosition(mapper, x1 + 1, y1 + 1);
9364 processPressure(mapper, RAW_PRESSURE_MAX);
9365 processSync(mapper);
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9368 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9369
9370 // Second finger down.
9371 processSlot(mapper, SECOND_SLOT);
9372 processId(mapper, SECOND_TRACKING_ID);
9373 processPosition(mapper, x2, y2);
9374 processPressure(mapper, RAW_PRESSURE_MAX);
9375 processSync(mapper);
9376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009377 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009378 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9379
9380 // second finger up with some unexpected data.
9381 processSlot(mapper, SECOND_SLOT);
9382 processId(mapper, INVALID_TRACKING_ID);
9383 processPosition(mapper, x2, y2);
9384 processSync(mapper);
9385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009386 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009387 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9388
9389 // first finger up with some unexpected data.
9390 processSlot(mapper, FIRST_SLOT);
9391 processId(mapper, INVALID_TRACKING_ID);
9392 processPosition(mapper, x2, y2);
9393 processPressure(mapper, RAW_PRESSURE_MAX);
9394 processSync(mapper);
9395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9396 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9397 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9398}
9399
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009400// --- MultiTouchInputMapperTest_ExternalDevice ---
9401
9402class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9403protected:
Chris Yea52ade12020-08-27 16:49:20 -07009404 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009405};
9406
9407/**
9408 * Expect fallback to internal viewport if device is external and external viewport is not present.
9409 */
9410TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9411 prepareAxes(POSITION);
9412 addConfigurationProperty("touch.deviceType", "touchScreen");
9413 prepareDisplay(DISPLAY_ORIENTATION_0);
9414 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9415
9416 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9417
9418 NotifyMotionArgs motionArgs;
9419
9420 // Expect the event to be sent to the internal viewport,
9421 // because an external viewport is not present.
9422 processPosition(mapper, 100, 100);
9423 processSync(mapper);
9424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9425 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9426
9427 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009428 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009429 processPosition(mapper, 100, 100);
9430 processSync(mapper);
9431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9432 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9433}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009434
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009435TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9436 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9437 std::shared_ptr<FakePointerController> fakePointerController =
9438 std::make_shared<FakePointerController>();
9439 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9440 fakePointerController->setPosition(0, 0);
9441 fakePointerController->setButtonState(0);
9442
9443 // prepare device and capture
9444 prepareDisplay(DISPLAY_ORIENTATION_0);
9445 prepareAxes(POSITION | ID | SLOT);
9446 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9447 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9448 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009449 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009450 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9451
9452 // captured touchpad should be a touchpad source
9453 NotifyDeviceResetArgs resetArgs;
9454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9455 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9456
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009457 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009458
9459 const InputDeviceInfo::MotionRange* relRangeX =
9460 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9461 ASSERT_NE(relRangeX, nullptr);
9462 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9463 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9464 const InputDeviceInfo::MotionRange* relRangeY =
9465 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9466 ASSERT_NE(relRangeY, nullptr);
9467 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9468 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9469
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009470 // run captured pointer tests - note that this is unscaled, so input listener events should be
9471 // identical to what the hardware sends (accounting for any
9472 // calibration).
9473 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009474 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009475 processId(mapper, 1);
9476 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9477 processKey(mapper, BTN_TOUCH, 1);
9478 processSync(mapper);
9479
9480 // expect coord[0] to contain initial location of touch 0
9481 NotifyMotionArgs args;
9482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9483 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9484 ASSERT_EQ(1U, args.pointerCount);
9485 ASSERT_EQ(0, args.pointerProperties[0].id);
9486 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9487 ASSERT_NO_FATAL_FAILURE(
9488 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9489
9490 // FINGER 1 DOWN
9491 processSlot(mapper, 1);
9492 processId(mapper, 2);
9493 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9494 processSync(mapper);
9495
9496 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009498 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009499 ASSERT_EQ(2U, args.pointerCount);
9500 ASSERT_EQ(0, args.pointerProperties[0].id);
9501 ASSERT_EQ(1, args.pointerProperties[1].id);
9502 ASSERT_NO_FATAL_FAILURE(
9503 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9504 ASSERT_NO_FATAL_FAILURE(
9505 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9506
9507 // FINGER 1 MOVE
9508 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9509 processSync(mapper);
9510
9511 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9512 // from move
9513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9515 ASSERT_NO_FATAL_FAILURE(
9516 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9517 ASSERT_NO_FATAL_FAILURE(
9518 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9519
9520 // FINGER 0 MOVE
9521 processSlot(mapper, 0);
9522 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9523 processSync(mapper);
9524
9525 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9528 ASSERT_NO_FATAL_FAILURE(
9529 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9530 ASSERT_NO_FATAL_FAILURE(
9531 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9532
9533 // BUTTON DOWN
9534 processKey(mapper, BTN_LEFT, 1);
9535 processSync(mapper);
9536
9537 // touchinputmapper design sends a move before button press
9538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9542
9543 // BUTTON UP
9544 processKey(mapper, BTN_LEFT, 0);
9545 processSync(mapper);
9546
9547 // touchinputmapper design sends a move after button release
9548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9552
9553 // FINGER 0 UP
9554 processId(mapper, -1);
9555 processSync(mapper);
9556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9557 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9558
9559 // FINGER 1 MOVE
9560 processSlot(mapper, 1);
9561 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9562 processSync(mapper);
9563
9564 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9567 ASSERT_EQ(1U, args.pointerCount);
9568 ASSERT_EQ(1, args.pointerProperties[0].id);
9569 ASSERT_NO_FATAL_FAILURE(
9570 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9571
9572 // FINGER 1 UP
9573 processId(mapper, -1);
9574 processKey(mapper, BTN_TOUCH, 0);
9575 processSync(mapper);
9576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9578
9579 // non captured touchpad should be a mouse source
9580 mFakePolicy->setPointerCapture(false);
9581 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9583 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9584}
9585
9586TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9587 std::shared_ptr<FakePointerController> fakePointerController =
9588 std::make_shared<FakePointerController>();
9589 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9590 fakePointerController->setPosition(0, 0);
9591 fakePointerController->setButtonState(0);
9592
9593 // prepare device and capture
9594 prepareDisplay(DISPLAY_ORIENTATION_0);
9595 prepareAxes(POSITION | ID | SLOT);
9596 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9597 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009598 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009599 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9600 // run uncaptured pointer tests - pushes out generic events
9601 // FINGER 0 DOWN
9602 processId(mapper, 3);
9603 processPosition(mapper, 100, 100);
9604 processKey(mapper, BTN_TOUCH, 1);
9605 processSync(mapper);
9606
9607 // start at (100,100), cursor should be at (0,0) * scale
9608 NotifyMotionArgs args;
9609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9610 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9611 ASSERT_NO_FATAL_FAILURE(
9612 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9613
9614 // FINGER 0 MOVE
9615 processPosition(mapper, 200, 200);
9616 processSync(mapper);
9617
9618 // compute scaling to help with touch position checking
9619 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9620 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9621 float scale =
9622 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9623
9624 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9626 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9628 0, 0, 0, 0, 0, 0, 0));
9629}
9630
9631TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9632 std::shared_ptr<FakePointerController> fakePointerController =
9633 std::make_shared<FakePointerController>();
9634
9635 prepareDisplay(DISPLAY_ORIENTATION_0);
9636 prepareAxes(POSITION | ID | SLOT);
9637 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009638 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009639 mFakePolicy->setPointerCapture(false);
9640 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9641
9642 // uncaptured touchpad should be a pointer device
9643 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9644
9645 // captured touchpad should be a touchpad device
9646 mFakePolicy->setPointerCapture(true);
9647 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9648 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9649}
9650
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009651// --- JoystickInputMapperTest ---
9652
9653class JoystickInputMapperTest : public InputMapperTest {
9654protected:
9655 static const int32_t RAW_X_MIN;
9656 static const int32_t RAW_X_MAX;
9657 static const int32_t RAW_Y_MIN;
9658 static const int32_t RAW_Y_MAX;
9659
9660 void SetUp() override {
9661 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9662 }
9663 void prepareAxes() {
9664 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9665 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9666 }
9667
9668 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9670 }
9671
9672 void processSync(JoystickInputMapper& mapper) {
9673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9674 }
9675
9676 void prepareVirtualDisplay(int32_t orientation) {
9677 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9678 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9679 NO_PORT, ViewportType::VIRTUAL);
9680 }
9681};
9682
9683const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9684const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9685const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9686const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9687
9688TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9689 prepareAxes();
9690 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9691
9692 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9693
9694 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9695
9696 // Send an axis event
9697 processAxis(mapper, ABS_X, 100);
9698 processSync(mapper);
9699
9700 NotifyMotionArgs args;
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9702 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9703
9704 // Send another axis event
9705 processAxis(mapper, ABS_Y, 100);
9706 processSync(mapper);
9707
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9709 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9710}
9711
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009712// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009713
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009714class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009715protected:
9716 static const char* DEVICE_NAME;
9717 static const char* DEVICE_LOCATION;
9718 static const int32_t DEVICE_ID;
9719 static const int32_t DEVICE_GENERATION;
9720 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009721 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009722 static const int32_t EVENTHUB_ID;
9723
9724 std::shared_ptr<FakeEventHub> mFakeEventHub;
9725 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009726 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009727 std::unique_ptr<InstrumentedInputReader> mReader;
9728 std::shared_ptr<InputDevice> mDevice;
9729
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009730 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009731 mFakeEventHub = std::make_unique<FakeEventHub>();
9732 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009733 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009734 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009735 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009736 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9737 }
9738
9739 void SetUp() override { SetUp(DEVICE_CLASSES); }
9740
9741 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009742 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009743 mFakePolicy.clear();
9744 }
9745
9746 void configureDevice(uint32_t changes) {
9747 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9748 mReader->requestRefreshConfiguration(changes);
9749 mReader->loopOnce();
9750 }
9751 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9752 }
9753
9754 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9755 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009756 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009757 InputDeviceIdentifier identifier;
9758 identifier.name = name;
9759 identifier.location = location;
9760 std::shared_ptr<InputDevice> device =
9761 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9762 identifier);
9763 mReader->pushNextDevice(device);
9764 mFakeEventHub->addDevice(eventHubId, name, classes);
9765 mReader->loopOnce();
9766 return device;
9767 }
9768
9769 template <class T, typename... Args>
9770 T& addControllerAndConfigure(Args... args) {
9771 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9772
9773 return controller;
9774 }
9775};
9776
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009777const char* PeripheralControllerTest::DEVICE_NAME = "device";
9778const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9779const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9780const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9781const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009782const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9783 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009784const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009785
9786// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009787class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009788protected:
9789 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009790 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009791 }
9792};
9793
9794TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009795 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009796
9797 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9798 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9799}
9800
9801TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009802 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009803
9804 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9805 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9806}
9807
9808// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009809class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009810protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009811 void SetUp() override {
9812 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9813 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009814};
9815
Chris Ye85758332021-05-16 23:05:17 -07009816TEST_F(LightControllerTest, MonoLight) {
9817 RawLightInfo infoMono = {.id = 1,
9818 .name = "Mono",
9819 .maxBrightness = 255,
9820 .flags = InputLightClass::BRIGHTNESS,
9821 .path = ""};
9822 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009823
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009824 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009825 InputDeviceInfo info;
9826 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009827 std::vector<InputDeviceLightInfo> lights = info.getLights();
9828 ASSERT_EQ(1U, lights.size());
9829 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009830
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009831 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9832 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009833}
9834
9835TEST_F(LightControllerTest, RGBLight) {
9836 RawLightInfo infoRed = {.id = 1,
9837 .name = "red",
9838 .maxBrightness = 255,
9839 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9840 .path = ""};
9841 RawLightInfo infoGreen = {.id = 2,
9842 .name = "green",
9843 .maxBrightness = 255,
9844 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9845 .path = ""};
9846 RawLightInfo infoBlue = {.id = 3,
9847 .name = "blue",
9848 .maxBrightness = 255,
9849 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9850 .path = ""};
9851 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9852 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9853 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9854
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009855 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009856 InputDeviceInfo info;
9857 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009858 std::vector<InputDeviceLightInfo> lights = info.getLights();
9859 ASSERT_EQ(1U, lights.size());
9860 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009861
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009862 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9863 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009864}
9865
9866TEST_F(LightControllerTest, MultiColorRGBLight) {
9867 RawLightInfo infoColor = {.id = 1,
9868 .name = "red",
9869 .maxBrightness = 255,
9870 .flags = InputLightClass::BRIGHTNESS |
9871 InputLightClass::MULTI_INTENSITY |
9872 InputLightClass::MULTI_INDEX,
9873 .path = ""};
9874
9875 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9876
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009877 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009878 InputDeviceInfo info;
9879 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009880 std::vector<InputDeviceLightInfo> lights = info.getLights();
9881 ASSERT_EQ(1U, lights.size());
9882 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009883
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009884 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9885 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009886}
9887
9888TEST_F(LightControllerTest, PlayerIdLight) {
9889 RawLightInfo info1 = {.id = 1,
9890 .name = "player1",
9891 .maxBrightness = 255,
9892 .flags = InputLightClass::BRIGHTNESS,
9893 .path = ""};
9894 RawLightInfo info2 = {.id = 2,
9895 .name = "player2",
9896 .maxBrightness = 255,
9897 .flags = InputLightClass::BRIGHTNESS,
9898 .path = ""};
9899 RawLightInfo info3 = {.id = 3,
9900 .name = "player3",
9901 .maxBrightness = 255,
9902 .flags = InputLightClass::BRIGHTNESS,
9903 .path = ""};
9904 RawLightInfo info4 = {.id = 4,
9905 .name = "player4",
9906 .maxBrightness = 255,
9907 .flags = InputLightClass::BRIGHTNESS,
9908 .path = ""};
9909 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9910 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9911 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9912 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9913
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009914 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009915 InputDeviceInfo info;
9916 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009917 std::vector<InputDeviceLightInfo> lights = info.getLights();
9918 ASSERT_EQ(1U, lights.size());
9919 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009920
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009921 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9922 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9923 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009924}
9925
Michael Wrightd02c5b62014-02-10 15:10:22 -08009926} // namespace android