blob: 74d4f3b99f7fb1409632641547e5fdd398a25e65 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000059static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080060static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000061static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_WIDTH = 480;
63static constexpr int32_t DISPLAY_HEIGHT = 800;
64static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
65static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
66static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070067static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070068static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080069
arthurhungcc7f9802020-04-30 17:55:40 +080070static constexpr int32_t FIRST_SLOT = 0;
71static constexpr int32_t SECOND_SLOT = 1;
72static constexpr int32_t THIRD_SLOT = 2;
73static constexpr int32_t INVALID_TRACKING_ID = -1;
74static constexpr int32_t FIRST_TRACKING_ID = 0;
75static constexpr int32_t SECOND_TRACKING_ID = 1;
76static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080077static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080078static constexpr int32_t BATTERY_STATUS = 4;
79static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080080static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
81static constexpr int32_t LIGHT_COLOR = 0x7F448866;
82static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080083
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080084static constexpr int32_t ACTION_POINTER_0_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_DOWN =
89 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92
Michael Wrightd02c5b62014-02-10 15:10:22 -080093// Error tolerance for floating point assertions.
94static const float EPSILON = 0.001f;
95
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000096using ::testing::AllOf;
97
98MATCHER_P(WithAction, action, "InputEvent with specified action") {
99 return arg.action == action;
100}
101
102MATCHER_P(WithSource, source, "InputEvent with specified source") {
103 return arg.source == source;
104}
105
106MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
107 return arg.displayId == displayId;
108}
109
110MATCHER_P2(WithCoords, x, y, "MotionEvent with specified action") {
111 return arg.pointerCoords[0].getX() == x && arg.pointerCoords[0].getY();
112}
113
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114template<typename T>
115static inline T min(T a, T b) {
116 return a < b ? a : b;
117}
118
119static inline float avg(float x, float y) {
120 return (x + y) / 2;
121}
122
Chris Ye3fdbfef2021-01-06 18:45:18 -0800123// Mapping for light color name and the light color
124const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
125 {"green", LightColor::GREEN},
126 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700128static int32_t getInverseRotation(int32_t orientation) {
129 switch (orientation) {
130 case DISPLAY_ORIENTATION_90:
131 return DISPLAY_ORIENTATION_270;
132 case DISPLAY_ORIENTATION_270:
133 return DISPLAY_ORIENTATION_90;
134 default:
135 return orientation;
136 }
137}
138
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800139static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
140 InputDeviceInfo info;
141 mapper.populateDeviceInfo(&info);
142
143 const InputDeviceInfo::MotionRange* motionRange =
144 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
145 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
146}
147
148static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
149 InputDeviceInfo info;
150 mapper.populateDeviceInfo(&info);
151
152 const InputDeviceInfo::MotionRange* motionRange =
153 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
154 ASSERT_EQ(nullptr, motionRange);
155}
156
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157// --- FakePointerController ---
158
159class FakePointerController : public PointerControllerInterface {
160 bool mHaveBounds;
161 float mMinX, mMinY, mMaxX, mMaxY;
162 float mX, mY;
163 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800164 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166public:
167 FakePointerController() :
168 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800169 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 }
171
Michael Wright17db18e2020-06-26 20:51:44 +0100172 virtual ~FakePointerController() {}
173
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174 void setBounds(float minX, float minY, float maxX, float maxY) {
175 mHaveBounds = true;
176 mMinX = minX;
177 mMinY = minY;
178 mMaxX = maxX;
179 mMaxY = maxY;
180 }
181
Chris Yea52ade12020-08-27 16:49:20 -0700182 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183 mX = x;
184 mY = y;
185 }
186
Chris Yea52ade12020-08-27 16:49:20 -0700187 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
Chris Yea52ade12020-08-27 16:49:20 -0700191 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 *outX = mX;
193 *outY = mY;
194 }
195
Chris Yea52ade12020-08-27 16:49:20 -0700196 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800197
Chris Yea52ade12020-08-27 16:49:20 -0700198 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800199 mDisplayId = viewport.displayId;
200 }
201
Arthur Hung7c645402019-01-25 17:45:42 +0800202 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
203 return mSpotsByDisplay;
204 }
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206private:
Chris Yea52ade12020-08-27 16:49:20 -0700207 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 *outMinX = mMinX;
209 *outMinY = mMinY;
210 *outMaxX = mMaxX;
211 *outMaxY = mMaxY;
212 return mHaveBounds;
213 }
214
Chris Yea52ade12020-08-27 16:49:20 -0700215 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 mX += deltaX;
217 if (mX < mMinX) mX = mMinX;
218 if (mX > mMaxX) mX = mMaxX;
219 mY += deltaY;
220 if (mY < mMinY) mY = mMinY;
221 if (mY > mMaxY) mY = mMaxY;
222 }
223
Chris Yea52ade12020-08-27 16:49:20 -0700224 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225
Chris Yea52ade12020-08-27 16:49:20 -0700226 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
Chris Yea52ade12020-08-27 16:49:20 -0700228 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229
Chris Yea52ade12020-08-27 16:49:20 -0700230 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
231 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800232 std::vector<int32_t> newSpots;
233 // Add spots for fingers that are down.
234 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
235 uint32_t id = idBits.clearFirstMarkedBit();
236 newSpots.push_back(id);
237 }
238
239 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240 }
241
Chris Yea52ade12020-08-27 16:49:20 -0700242 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800243
244 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245};
246
247
248// --- FakeInputReaderPolicy ---
249
250class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700251 std::mutex mLock;
252 std::condition_variable mDevicesChangedCondition;
253
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000255 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700256 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
257 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100258 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700259 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260
261protected:
Chris Yea52ade12020-08-27 16:49:20 -0700262 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263
264public:
265 FakeInputReaderPolicy() {
266 }
267
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700268 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800269 waitForInputDevices([](bool devicesChanged) {
270 if (!devicesChanged) {
271 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
272 }
273 });
274 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700275
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800276 void assertInputDevicesNotChanged() {
277 waitForInputDevices([](bool devicesChanged) {
278 if (devicesChanged) {
279 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
280 }
281 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700282 }
283
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700284 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100285 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100286 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700287 }
288
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700289 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
290 return mConfig.getDisplayViewportByUniqueId(uniqueId);
291 }
292 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
293 return mConfig.getDisplayViewportByType(type);
294 }
295
296 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
297 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700298 }
299
Prabir Pradhan5632d622021-09-06 07:57:20 -0700300 void addDisplayViewport(DisplayViewport viewport) {
301 mViewports.push_back(std::move(viewport));
302 mConfig.setDisplayViewports(mViewports);
303 }
304
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700305 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000306 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700307 std::optional<uint8_t> physicalPort, ViewportType type) {
308 const bool isRotated =
309 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
310 DisplayViewport v;
311 v.displayId = displayId;
312 v.orientation = orientation;
313 v.logicalLeft = 0;
314 v.logicalTop = 0;
315 v.logicalRight = isRotated ? height : width;
316 v.logicalBottom = isRotated ? width : height;
317 v.physicalLeft = 0;
318 v.physicalTop = 0;
319 v.physicalRight = isRotated ? height : width;
320 v.physicalBottom = isRotated ? width : height;
321 v.deviceWidth = isRotated ? height : width;
322 v.deviceHeight = isRotated ? width : height;
323 v.isActive = isActive;
324 v.uniqueId = uniqueId;
325 v.physicalPort = physicalPort;
326 v.type = type;
327
328 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 }
330
Arthur Hung6cd19a42019-08-30 19:04:12 +0800331 bool updateViewport(const DisplayViewport& viewport) {
332 size_t count = mViewports.size();
333 for (size_t i = 0; i < count; i++) {
334 const DisplayViewport& currentViewport = mViewports[i];
335 if (currentViewport.displayId == viewport.displayId) {
336 mViewports[i] = viewport;
337 mConfig.setDisplayViewports(mViewports);
338 return true;
339 }
340 }
341 // no viewport found.
342 return false;
343 }
344
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100345 void addExcludedDeviceName(const std::string& deviceName) {
346 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700349 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
350 mConfig.portAssociations.insert({inputPort, displayPort});
351 }
352
Christine Franks1ba71cc2021-04-07 14:37:42 -0700353 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
354 const std::string& displayUniqueId) {
355 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
356 }
357
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000358 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700359
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000360 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700361
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000362 void setPointerController(std::shared_ptr<FakePointerController> controller) {
363 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 }
365
366 const InputReaderConfiguration* getReaderConfiguration() const {
367 return &mConfig;
368 }
369
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800370 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 return mInputDevices;
372 }
373
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100374 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700375 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700376 return transform;
377 }
378
379 void setTouchAffineTransformation(const TouchAffineTransformation t) {
380 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800381 }
382
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000383 PointerCaptureRequest setPointerCapture(bool enabled) {
384 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
385 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800386 }
387
Arthur Hung7c645402019-01-25 17:45:42 +0800388 void setShowTouches(bool enabled) {
389 mConfig.showTouches = enabled;
390 }
391
Garfield Tan888a6a42020-01-09 11:39:16 -0800392 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
393 mConfig.defaultPointerDisplayId = pointerDisplayId;
394 }
395
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800396 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
397
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000398 void setVelocityControlParams(const VelocityControlParameters& params) {
399 mConfig.pointerVelocityControlParameters = params;
400 mConfig.wheelVelocityControlParameters = params;
401 }
402
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000404 uint32_t mNextPointerCaptureSequenceNumber = 0;
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 *outConfig = mConfig;
408 }
409
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000410 std::shared_ptr<PointerControllerInterface> obtainPointerController(
411 int32_t /*deviceId*/) override {
412 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413 }
414
Chris Yea52ade12020-08-27 16:49:20 -0700415 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700416 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700418 mInputDevicesChanged = true;
419 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 }
421
Chris Yea52ade12020-08-27 16:49:20 -0700422 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
423 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700424 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425 }
426
Chris Yea52ade12020-08-27 16:49:20 -0700427 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800428
429 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
430 std::unique_lock<std::mutex> lock(mLock);
431 base::ScopedLockAssertion assumeLocked(mLock);
432
433 const bool devicesChanged =
434 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
435 return mInputDevicesChanged;
436 });
437 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
438 mInputDevicesChanged = false;
439 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440};
441
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442// --- FakeEventHub ---
443
444class FakeEventHub : public EventHubInterface {
445 struct KeyInfo {
446 int32_t keyCode;
447 uint32_t flags;
448 };
449
Chris Yef59a2f42020-10-16 12:55:26 -0700450 struct SensorInfo {
451 InputDeviceSensorType sensorType;
452 int32_t sensorDataIndex;
453 };
454
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 struct Device {
456 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700457 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 PropertyMap configuration;
459 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
460 KeyedVector<int, bool> relativeAxes;
461 KeyedVector<int32_t, int32_t> keyCodeStates;
462 KeyedVector<int32_t, int32_t> scanCodeStates;
463 KeyedVector<int32_t, int32_t> switchStates;
464 KeyedVector<int32_t, int32_t> absoluteAxisValue;
465 KeyedVector<int32_t, KeyInfo> keysByScanCode;
466 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
467 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100468 // fake mapping which would normally come from keyCharacterMap
469 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700470 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
471 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800472 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700473 bool enabled;
474
475 status_t enable() {
476 enabled = true;
477 return OK;
478 }
479
480 status_t disable() {
481 enabled = false;
482 return OK;
483 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700485 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486 };
487
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700488 std::mutex mLock;
489 std::condition_variable mEventsCondition;
490
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100492 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000493 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600494 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000495 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800496 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
497 // Simulates a device light brightness, from light id to light brightness.
498 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
499 // Simulates a device light intensities, from light id to light intensities map.
500 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
501 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700503public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 virtual ~FakeEventHub() {
505 for (size_t i = 0; i < mDevices.size(); i++) {
506 delete mDevices.valueAt(i);
507 }
508 }
509
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 FakeEventHub() { }
511
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700512 void addDevice(int32_t deviceId, const std::string& name,
513 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 Device* device = new Device(classes);
515 device->identifier.name = name;
516 mDevices.add(deviceId, device);
517
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000518 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 }
520
521 void removeDevice(int32_t deviceId) {
522 delete mDevices.valueFor(deviceId);
523 mDevices.removeItem(deviceId);
524
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000525 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526 }
527
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700528 bool isDeviceEnabled(int32_t deviceId) {
529 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700530 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700531 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
532 return false;
533 }
534 return device->enabled;
535 }
536
537 status_t enableDevice(int32_t deviceId) {
538 status_t result;
539 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700540 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700541 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
542 return BAD_VALUE;
543 }
544 if (device->enabled) {
545 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
546 return OK;
547 }
548 result = device->enable();
549 return result;
550 }
551
552 status_t disableDevice(int32_t deviceId) {
553 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700554 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700555 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
556 return BAD_VALUE;
557 }
558 if (!device->enabled) {
559 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
560 return OK;
561 }
562 return device->disable();
563 }
564
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000566 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 }
568
569 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
570 Device* device = getDevice(deviceId);
571 device->configuration.addProperty(key, value);
572 }
573
574 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
575 Device* device = getDevice(deviceId);
576 device->configuration.addAll(configuration);
577 }
578
579 void addAbsoluteAxis(int32_t deviceId, int axis,
580 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
581 Device* device = getDevice(deviceId);
582
583 RawAbsoluteAxisInfo info;
584 info.valid = true;
585 info.minValue = minValue;
586 info.maxValue = maxValue;
587 info.flat = flat;
588 info.fuzz = fuzz;
589 info.resolution = resolution;
590 device->absoluteAxes.add(axis, info);
591 }
592
593 void addRelativeAxis(int32_t deviceId, int32_t axis) {
594 Device* device = getDevice(deviceId);
595 device->relativeAxes.add(axis, true);
596 }
597
598 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
599 Device* device = getDevice(deviceId);
600 device->keyCodeStates.replaceValueFor(keyCode, state);
601 }
602
603 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
604 Device* device = getDevice(deviceId);
605 device->scanCodeStates.replaceValueFor(scanCode, state);
606 }
607
608 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
609 Device* device = getDevice(deviceId);
610 device->switchStates.replaceValueFor(switchCode, state);
611 }
612
613 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
614 Device* device = getDevice(deviceId);
615 device->absoluteAxisValue.replaceValueFor(axis, value);
616 }
617
618 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
619 int32_t keyCode, uint32_t flags) {
620 Device* device = getDevice(deviceId);
621 KeyInfo info;
622 info.keyCode = keyCode;
623 info.flags = flags;
624 if (scanCode) {
625 device->keysByScanCode.add(scanCode, info);
626 }
627 if (usageCode) {
628 device->keysByUsageCode.add(usageCode, info);
629 }
630 }
631
Philip Junker4af3b3d2021-12-14 10:36:55 +0100632 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
633 Device* device = getDevice(deviceId);
634 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
635 }
636
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 void addLed(int32_t deviceId, int32_t led, bool initialState) {
638 Device* device = getDevice(deviceId);
639 device->leds.add(led, initialState);
640 }
641
Chris Yef59a2f42020-10-16 12:55:26 -0700642 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
643 int32_t sensorDataIndex) {
644 Device* device = getDevice(deviceId);
645 SensorInfo info;
646 info.sensorType = sensorType;
647 info.sensorDataIndex = sensorDataIndex;
648 device->sensorsByAbsCode.emplace(absCode, info);
649 }
650
651 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
652 Device* device = getDevice(deviceId);
653 typename BitArray<MSC_MAX>::Buffer buffer;
654 buffer[mscEvent / 32] = 1 << mscEvent % 32;
655 device->mscBitmask.loadFromBuffer(buffer);
656 }
657
Chris Ye3fdbfef2021-01-06 18:45:18 -0800658 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
659 mRawLightInfos.emplace(rawId, std::move(info));
660 }
661
662 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
663 mLightBrightness.emplace(rawId, brightness);
664 }
665
666 void fakeLightIntensities(int32_t rawId,
667 const std::unordered_map<LightColor, int32_t> intensities) {
668 mLightIntensities.emplace(rawId, std::move(intensities));
669 }
670
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 bool getLedState(int32_t deviceId, int32_t led) {
672 Device* device = getDevice(deviceId);
673 return device->leds.valueFor(led);
674 }
675
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100676 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 return mExcludedDevices;
678 }
679
680 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
681 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800682 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 }
684
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000685 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
686 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700687 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 RawEvent event;
689 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000690 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 event.deviceId = deviceId;
692 event.type = type;
693 event.code = code;
694 event.value = value;
695 mEvents.push_back(event);
696
697 if (type == EV_ABS) {
698 setAbsoluteAxisValue(deviceId, code, value);
699 }
700 }
701
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600702 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
703 std::vector<TouchVideoFrame>> videoFrames) {
704 mVideoFrames = std::move(videoFrames);
705 }
706
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700708 std::unique_lock<std::mutex> lock(mLock);
709 base::ScopedLockAssertion assumeLocked(mLock);
710 const bool queueIsEmpty =
711 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
712 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
713 if (!queueIsEmpty) {
714 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
715 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 }
717
718private:
719 Device* getDevice(int32_t deviceId) const {
720 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100721 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 }
723
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700724 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700726 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 }
728
Chris Yea52ade12020-08-27 16:49:20 -0700729 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 Device* device = getDevice(deviceId);
731 return device ? device->identifier : InputDeviceIdentifier();
732 }
733
Chris Yea52ade12020-08-27 16:49:20 -0700734 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735
Chris Yea52ade12020-08-27 16:49:20 -0700736 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 Device* device = getDevice(deviceId);
738 if (device) {
739 *outConfiguration = device->configuration;
740 }
741 }
742
Chris Yea52ade12020-08-27 16:49:20 -0700743 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
744 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800746 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 ssize_t index = device->absoluteAxes.indexOfKey(axis);
748 if (index >= 0) {
749 *outAxisInfo = device->absoluteAxes.valueAt(index);
750 return OK;
751 }
752 }
753 outAxisInfo->clear();
754 return -1;
755 }
756
Chris Yea52ade12020-08-27 16:49:20 -0700757 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 Device* device = getDevice(deviceId);
759 if (device) {
760 return device->relativeAxes.indexOfKey(axis) >= 0;
761 }
762 return false;
763 }
764
Chris Yea52ade12020-08-27 16:49:20 -0700765 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766
Chris Yef59a2f42020-10-16 12:55:26 -0700767 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
768 Device* device = getDevice(deviceId);
769 if (device) {
770 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
771 }
772 return false;
773 }
774
Chris Yea52ade12020-08-27 16:49:20 -0700775 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
776 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 Device* device = getDevice(deviceId);
778 if (device) {
779 const KeyInfo* key = getKey(device, scanCode, usageCode);
780 if (key) {
781 if (outKeycode) {
782 *outKeycode = key->keyCode;
783 }
784 if (outFlags) {
785 *outFlags = key->flags;
786 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700787 if (outMetaState) {
788 *outMetaState = metaState;
789 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 return OK;
791 }
792 }
793 return NAME_NOT_FOUND;
794 }
795
796 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
797 if (usageCode) {
798 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
799 if (index >= 0) {
800 return &device->keysByUsageCode.valueAt(index);
801 }
802 }
803 if (scanCode) {
804 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
805 if (index >= 0) {
806 return &device->keysByScanCode.valueAt(index);
807 }
808 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700809 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
Chris Yea52ade12020-08-27 16:49:20 -0700812 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813
Chris Yef59a2f42020-10-16 12:55:26 -0700814 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
815 int32_t absCode) {
816 Device* device = getDevice(deviceId);
817 if (!device) {
818 return Errorf("Sensor device not found.");
819 }
820 auto it = device->sensorsByAbsCode.find(absCode);
821 if (it == device->sensorsByAbsCode.end()) {
822 return Errorf("Sensor map not found.");
823 }
824 const SensorInfo& info = it->second;
825 return std::make_pair(info.sensorType, info.sensorDataIndex);
826 }
827
Chris Yea52ade12020-08-27 16:49:20 -0700828 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 mExcludedDevices = devices;
830 }
831
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000832 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
833 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000835 const size_t filledSize = std::min(mEvents.size(), bufferSize);
836 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
837
838 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700839 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000840 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800841 }
842
Chris Yea52ade12020-08-27 16:49:20 -0700843 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600844 auto it = mVideoFrames.find(deviceId);
845 if (it != mVideoFrames.end()) {
846 std::vector<TouchVideoFrame> frames = std::move(it->second);
847 mVideoFrames.erase(deviceId);
848 return frames;
849 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800850 return {};
851 }
852
Chris Yea52ade12020-08-27 16:49:20 -0700853 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 Device* device = getDevice(deviceId);
855 if (device) {
856 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
857 if (index >= 0) {
858 return device->scanCodeStates.valueAt(index);
859 }
860 }
861 return AKEY_STATE_UNKNOWN;
862 }
863
Chris Yea52ade12020-08-27 16:49:20 -0700864 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865 Device* device = getDevice(deviceId);
866 if (device) {
867 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
868 if (index >= 0) {
869 return device->keyCodeStates.valueAt(index);
870 }
871 }
872 return AKEY_STATE_UNKNOWN;
873 }
874
Chris Yea52ade12020-08-27 16:49:20 -0700875 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 Device* device = getDevice(deviceId);
877 if (device) {
878 ssize_t index = device->switchStates.indexOfKey(sw);
879 if (index >= 0) {
880 return device->switchStates.valueAt(index);
881 }
882 }
883 return AKEY_STATE_UNKNOWN;
884 }
885
Chris Yea52ade12020-08-27 16:49:20 -0700886 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
887 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800888 Device* device = getDevice(deviceId);
889 if (device) {
890 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
891 if (index >= 0) {
892 *outValue = device->absoluteAxisValue.valueAt(index);
893 return OK;
894 }
895 }
896 *outValue = 0;
897 return -1;
898 }
899
Philip Junker4af3b3d2021-12-14 10:36:55 +0100900 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
901 Device* device = getDevice(deviceId);
902 if (!device) {
903 return AKEYCODE_UNKNOWN;
904 }
905 auto it = device->keyCodeMapping.find(locationKeyCode);
906 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
907 }
908
Chris Yea52ade12020-08-27 16:49:20 -0700909 // Return true if the device has non-empty key layout.
910 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
911 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 bool result = false;
913 Device* device = getDevice(deviceId);
914 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700915 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 for (size_t i = 0; i < numCodes; i++) {
917 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
918 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
919 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920 }
921 }
922 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
923 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
924 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 }
926 }
927 }
928 }
929 return result;
930 }
931
Chris Yea52ade12020-08-27 16:49:20 -0700932 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 Device* device = getDevice(deviceId);
934 if (device) {
935 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
936 return index >= 0;
937 }
938 return false;
939 }
940
Arthur Hungcb40a002021-08-03 14:31:01 +0000941 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
942 Device* device = getDevice(deviceId);
943 if (!device) {
944 return false;
945 }
946 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
947 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
948 return true;
949 }
950 }
951 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
952 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
953 return true;
954 }
955 }
956 return false;
957 }
958
Chris Yea52ade12020-08-27 16:49:20 -0700959 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 Device* device = getDevice(deviceId);
961 return device && device->leds.indexOfKey(led) >= 0;
962 }
963
Chris Yea52ade12020-08-27 16:49:20 -0700964 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 Device* device = getDevice(deviceId);
966 if (device) {
967 ssize_t index = device->leds.indexOfKey(led);
968 if (index >= 0) {
969 device->leds.replaceValueAt(led, on);
970 } else {
971 ADD_FAILURE()
972 << "Attempted to set the state of an LED that the EventHub declared "
973 "was not present. led=" << led;
974 }
975 }
976 }
977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void getVirtualKeyDefinitions(
979 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 outVirtualKeys.clear();
981
982 Device* device = getDevice(deviceId);
983 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800984 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 }
986 }
987
Chris Yea52ade12020-08-27 16:49:20 -0700988 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700989 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 return false;
994 }
995
Chris Yea52ade12020-08-27 16:49:20 -0700996 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997
Chris Yea52ade12020-08-27 16:49:20 -0700998 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999
Chris Ye87143712020-11-10 05:05:58 +00001000 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
1001
Chris Yee2b1e5c2021-03-10 22:45:12 -08001002 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1003 return BATTERY_CAPACITY;
1004 }
Kim Low03ea0352020-11-06 12:45:07 -08001005
Chris Yee2b1e5c2021-03-10 22:45:12 -08001006 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1007 return BATTERY_STATUS;
1008 }
1009
1010 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
1011
1012 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
1013 return std::nullopt;
1014 }
Kim Low03ea0352020-11-06 12:45:07 -08001015
Chris Ye3fdbfef2021-01-06 18:45:18 -08001016 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1017 std::vector<int32_t> ids;
1018 for (const auto& [rawId, info] : mRawLightInfos) {
1019 ids.push_back(rawId);
1020 }
1021 return ids;
1022 }
1023
1024 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1025 auto it = mRawLightInfos.find(lightId);
1026 if (it == mRawLightInfos.end()) {
1027 return std::nullopt;
1028 }
1029 return it->second;
1030 }
1031
1032 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1033 mLightBrightness.emplace(lightId, brightness);
1034 }
1035
1036 void setLightIntensities(int32_t deviceId, int32_t lightId,
1037 std::unordered_map<LightColor, int32_t> intensities) override {
1038 mLightIntensities.emplace(lightId, intensities);
1039 };
1040
1041 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1042 auto lightIt = mLightBrightness.find(lightId);
1043 if (lightIt == mLightBrightness.end()) {
1044 return std::nullopt;
1045 }
1046 return lightIt->second;
1047 }
1048
1049 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1050 int32_t deviceId, int32_t lightId) override {
1051 auto lightIt = mLightIntensities.find(lightId);
1052 if (lightIt == mLightIntensities.end()) {
1053 return std::nullopt;
1054 }
1055 return lightIt->second;
1056 };
1057
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001058 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001059 return false;
1060 }
1061
Chris Yea52ade12020-08-27 16:49:20 -07001062 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063
Chris Yea52ade12020-08-27 16:49:20 -07001064 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Chris Yea52ade12020-08-27 16:49:20 -07001066 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067
Chris Yea52ade12020-08-27 16:49:20 -07001068 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069};
1070
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071// --- FakeInputMapper ---
1072
1073class FakeInputMapper : public InputMapper {
1074 uint32_t mSources;
1075 int32_t mKeyboardType;
1076 int32_t mMetaState;
1077 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1078 KeyedVector<int32_t, int32_t> mScanCodeStates;
1079 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001080 // fake mapping which would normally come from keyCharacterMap
1081 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001082 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001084 std::mutex mLock;
1085 std::condition_variable mStateChangedCondition;
1086 bool mConfigureWasCalled GUARDED_BY(mLock);
1087 bool mResetWasCalled GUARDED_BY(mLock);
1088 bool mProcessWasCalled GUARDED_BY(mLock);
1089 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090
Arthur Hungc23540e2018-11-29 20:42:11 +08001091 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001093 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1094 : InputMapper(deviceContext),
1095 mSources(sources),
1096 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001098 mConfigureWasCalled(false),
1099 mResetWasCalled(false),
1100 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101
Chris Yea52ade12020-08-27 16:49:20 -07001102 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103
1104 void setKeyboardType(int32_t keyboardType) {
1105 mKeyboardType = keyboardType;
1106 }
1107
1108 void setMetaState(int32_t metaState) {
1109 mMetaState = metaState;
1110 }
1111
1112 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001113 std::unique_lock<std::mutex> lock(mLock);
1114 base::ScopedLockAssertion assumeLocked(mLock);
1115 const bool configureCalled =
1116 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1117 return mConfigureWasCalled;
1118 });
1119 if (!configureCalled) {
1120 FAIL() << "Expected configure() to have been called.";
1121 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001122 mConfigureWasCalled = false;
1123 }
1124
1125 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001126 std::unique_lock<std::mutex> lock(mLock);
1127 base::ScopedLockAssertion assumeLocked(mLock);
1128 const bool resetCalled =
1129 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1130 return mResetWasCalled;
1131 });
1132 if (!resetCalled) {
1133 FAIL() << "Expected reset() to have been called.";
1134 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 mResetWasCalled = false;
1136 }
1137
Yi Kong9b14ac62018-07-17 13:48:38 -07001138 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001139 std::unique_lock<std::mutex> lock(mLock);
1140 base::ScopedLockAssertion assumeLocked(mLock);
1141 const bool processCalled =
1142 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1143 return mProcessWasCalled;
1144 });
1145 if (!processCalled) {
1146 FAIL() << "Expected process() to have been called.";
1147 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 if (outLastEvent) {
1149 *outLastEvent = mLastEvent;
1150 }
1151 mProcessWasCalled = false;
1152 }
1153
1154 void setKeyCodeState(int32_t keyCode, int32_t state) {
1155 mKeyCodeStates.replaceValueFor(keyCode, state);
1156 }
1157
1158 void setScanCodeState(int32_t scanCode, int32_t state) {
1159 mScanCodeStates.replaceValueFor(scanCode, state);
1160 }
1161
1162 void setSwitchState(int32_t switchCode, int32_t state) {
1163 mSwitchStates.replaceValueFor(switchCode, state);
1164 }
1165
1166 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001167 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 }
1169
Philip Junker4af3b3d2021-12-14 10:36:55 +01001170 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1171 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1172 }
1173
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001175 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176
Chris Yea52ade12020-08-27 16:49:20 -07001177 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178 InputMapper::populateDeviceInfo(deviceInfo);
1179
1180 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1181 deviceInfo->setKeyboardType(mKeyboardType);
1182 }
1183 }
1184
Chris Yea52ade12020-08-27 16:49:20 -07001185 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001186 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001188
1189 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001190 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001191 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1192 mViewport = config->getDisplayViewportByPort(*displayPort);
1193 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001194
1195 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 }
1197
Chris Yea52ade12020-08-27 16:49:20 -07001198 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001199 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001201 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 }
1203
Chris Yea52ade12020-08-27 16:49:20 -07001204 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001205 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 mLastEvent = *rawEvent;
1207 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001208 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 }
1210
Chris Yea52ade12020-08-27 16:49:20 -07001211 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001212 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1213 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1214 }
1215
Philip Junker4af3b3d2021-12-14 10:36:55 +01001216 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1217 auto it = mKeyCodeMapping.find(locationKeyCode);
1218 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1219 }
1220
Chris Yea52ade12020-08-27 16:49:20 -07001221 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1223 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1224 }
1225
Chris Yea52ade12020-08-27 16:49:20 -07001226 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001227 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1228 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1229 }
1230
Chris Yea52ade12020-08-27 16:49:20 -07001231 // Return true if the device has non-empty key layout.
1232 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1233 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234 for (size_t i = 0; i < numCodes; i++) {
1235 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1236 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1237 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238 }
1239 }
1240 }
Chris Yea52ade12020-08-27 16:49:20 -07001241 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 return result;
1243 }
1244
1245 virtual int32_t getMetaState() {
1246 return mMetaState;
1247 }
1248
1249 virtual void fadePointer() {
1250 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001251
1252 virtual std::optional<int32_t> getAssociatedDisplay() {
1253 if (mViewport) {
1254 return std::make_optional(mViewport->displayId);
1255 }
1256 return std::nullopt;
1257 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258};
1259
1260
1261// --- InstrumentedInputReader ---
1262
1263class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001264 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265
1266public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001267 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1268 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001269 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001270 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001271
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001272 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001274 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001276 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001277 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278 InputDeviceIdentifier identifier;
1279 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001280 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001282 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 }
1284
Prabir Pradhan28efc192019-11-05 01:10:04 +00001285 // Make the protected loopOnce method accessible to tests.
1286 using InputReader::loopOnce;
1287
Michael Wrightd02c5b62014-02-10 15:10:22 -08001288protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001289 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1290 const InputDeviceIdentifier& identifier)
1291 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001292 if (!mNextDevices.empty()) {
1293 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1294 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295 return device;
1296 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001297 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001298 }
1299
arthurhungdcef2dc2020-08-11 14:47:50 +08001300 // --- FakeInputReaderContext ---
1301 class FakeInputReaderContext : public ContextImpl {
1302 int32_t mGlobalMetaState;
1303 bool mUpdateGlobalMetaStateWasCalled;
1304 int32_t mGeneration;
1305
1306 public:
1307 FakeInputReaderContext(InputReader* reader)
1308 : ContextImpl(reader),
1309 mGlobalMetaState(0),
1310 mUpdateGlobalMetaStateWasCalled(false),
1311 mGeneration(1) {}
1312
1313 virtual ~FakeInputReaderContext() {}
1314
1315 void assertUpdateGlobalMetaStateWasCalled() {
1316 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1317 << "Expected updateGlobalMetaState() to have been called.";
1318 mUpdateGlobalMetaStateWasCalled = false;
1319 }
1320
1321 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1322
1323 uint32_t getGeneration() { return mGeneration; }
1324
1325 void updateGlobalMetaState() override {
1326 mUpdateGlobalMetaStateWasCalled = true;
1327 ContextImpl::updateGlobalMetaState();
1328 }
1329
1330 int32_t getGlobalMetaState() override {
1331 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1332 }
1333
1334 int32_t bumpGeneration() override {
1335 mGeneration = ContextImpl::bumpGeneration();
1336 return mGeneration;
1337 }
1338 } mFakeContext;
1339
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001341
1342public:
1343 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001344};
1345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346// --- InputReaderPolicyTest ---
1347class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001348protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001349 sp<FakeInputReaderPolicy> mFakePolicy;
1350
Chris Yea52ade12020-08-27 16:49:20 -07001351 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1352 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001353};
1354
1355/**
1356 * Check that empty set of viewports is an acceptable configuration.
1357 * Also try to get internal viewport two different ways - by type and by uniqueId.
1358 *
1359 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1360 * Such configuration is not currently allowed.
1361 */
1362TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001363 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364
1365 // We didn't add any viewports yet, so there shouldn't be any.
1366 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001367 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368 ASSERT_FALSE(internalViewport);
1369
1370 // Add an internal viewport, then clear it
1371 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001372 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001373 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374
1375 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001376 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001377 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001378 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379
1380 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001381 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001382 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001383 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384
1385 mFakePolicy->clearViewports();
1386 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001387 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 ASSERT_FALSE(internalViewport);
1391}
1392
1393TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1394 const std::string internalUniqueId = "local:0";
1395 const std::string externalUniqueId = "local:1";
1396 const std::string virtualUniqueId1 = "virtual:2";
1397 const std::string virtualUniqueId2 = "virtual:3";
1398 constexpr int32_t virtualDisplayId1 = 2;
1399 constexpr int32_t virtualDisplayId2 = 3;
1400
1401 // Add an internal viewport
1402 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001403 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1404 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001405 // Add an external viewport
1406 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001407 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1408 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001409 // Add an virtual viewport
1410 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001411 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1412 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 // Add another virtual viewport
1414 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1416 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417
1418 // Check matching by type for internal
1419 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001420 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 ASSERT_TRUE(internalViewport);
1422 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1423
1424 // Check matching by type for external
1425 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001426 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_TRUE(externalViewport);
1428 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1429
1430 // Check matching by uniqueId for virtual viewport #1
1431 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001432 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001433 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001434 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1436 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1437
1438 // Check matching by uniqueId for virtual viewport #2
1439 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1444 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1445}
1446
1447
1448/**
1449 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1450 * that lookup works by checking display id.
1451 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1452 */
1453TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1454 const std::string uniqueId1 = "uniqueId1";
1455 const std::string uniqueId2 = "uniqueId2";
1456 constexpr int32_t displayId1 = 2;
1457 constexpr int32_t displayId2 = 3;
1458
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001459 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1460 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001461 for (const ViewportType& type : types) {
1462 mFakePolicy->clearViewports();
1463 // Add a viewport
1464 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001465 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1466 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001467 // Add another viewport
1468 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001469 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1470 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001471
1472 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001473 std::optional<DisplayViewport> viewport1 =
1474 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 ASSERT_TRUE(viewport1);
1476 ASSERT_EQ(displayId1, viewport1->displayId);
1477 ASSERT_EQ(type, viewport1->type);
1478
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001479 std::optional<DisplayViewport> viewport2 =
1480 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001481 ASSERT_TRUE(viewport2);
1482 ASSERT_EQ(displayId2, viewport2->displayId);
1483 ASSERT_EQ(type, viewport2->type);
1484
1485 // When there are multiple viewports of the same kind, and uniqueId is not specified
1486 // in the call to getDisplayViewport, then that situation is not supported.
1487 // The viewports can be stored in any order, so we cannot rely on the order, since that
1488 // is just implementation detail.
1489 // However, we can check that it still returns *a* viewport, we just cannot assert
1490 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001491 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001492 ASSERT_TRUE(someViewport);
1493 }
1494}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001496/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001497 * When we have multiple internal displays make sure we always return the default display when
1498 * querying by type.
1499 */
1500TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1501 const std::string uniqueId1 = "uniqueId1";
1502 const std::string uniqueId2 = "uniqueId2";
1503 constexpr int32_t nonDefaultDisplayId = 2;
1504 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1505 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1506
1507 // Add the default display first and ensure it gets returned.
1508 mFakePolicy->clearViewports();
1509 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001510 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001511 ViewportType::INTERNAL);
1512 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001513 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001514 ViewportType::INTERNAL);
1515
1516 std::optional<DisplayViewport> viewport =
1517 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1518 ASSERT_TRUE(viewport);
1519 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1520 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1521
1522 // Add the default display second to make sure order doesn't matter.
1523 mFakePolicy->clearViewports();
1524 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001525 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001526 ViewportType::INTERNAL);
1527 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001528 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001529 ViewportType::INTERNAL);
1530
1531 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1532 ASSERT_TRUE(viewport);
1533 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1534 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1535}
1536
1537/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001538 * Check getDisplayViewportByPort
1539 */
1540TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001541 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001542 const std::string uniqueId1 = "uniqueId1";
1543 const std::string uniqueId2 = "uniqueId2";
1544 constexpr int32_t displayId1 = 1;
1545 constexpr int32_t displayId2 = 2;
1546 const uint8_t hdmi1 = 0;
1547 const uint8_t hdmi2 = 1;
1548 const uint8_t hdmi3 = 2;
1549
1550 mFakePolicy->clearViewports();
1551 // Add a viewport that's associated with some display port that's not of interest.
1552 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001553 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1554 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001555 // Add another viewport, connected to HDMI1 port
1556 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001557 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1558 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001559
1560 // Check that correct display viewport was returned by comparing the display ports.
1561 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1562 ASSERT_TRUE(hdmi1Viewport);
1563 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1564 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1565
1566 // Check that we can still get the same viewport using the uniqueId
1567 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1568 ASSERT_TRUE(hdmi1Viewport);
1569 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1570 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1571 ASSERT_EQ(type, hdmi1Viewport->type);
1572
1573 // Check that we cannot find a port with "HDMI2", because we never added one
1574 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1575 ASSERT_FALSE(hdmi2Viewport);
1576}
1577
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578// --- InputReaderTest ---
1579
1580class InputReaderTest : public testing::Test {
1581protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001582 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001583 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001584 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001585 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586
Chris Yea52ade12020-08-27 16:49:20 -07001587 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001588 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001589 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001590 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591
Prabir Pradhan28efc192019-11-05 01:10:04 +00001592 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001593 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 }
1595
Chris Yea52ade12020-08-27 16:49:20 -07001596 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001597 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001599 }
1600
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001601 void addDevice(int32_t eventHubId, const std::string& name,
1602 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001603 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604
1605 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 }
1608 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001609 mReader->loopOnce();
1610 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001611 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1612 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613 }
1614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001615 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001616 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001617 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001618 }
1619
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001620 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001622 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 }
1624
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001626 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001627 ftl::Flags<InputDeviceClass> classes,
1628 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001629 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1631 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001632 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634 return mapper;
1635 }
1636};
1637
Chris Ye98d3f532020-10-01 21:48:59 -07001638TEST_F(InputReaderTest, PolicyGetInputDevices) {
1639 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001640 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001641 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642
1643 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001644 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001647 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1649 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001650 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651}
1652
Chris Yee7310032020-09-22 15:36:28 -07001653TEST_F(InputReaderTest, GetMergedInputDevices) {
1654 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1655 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1656 // Add two subdevices to device
1657 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1658 // Must add at least one mapper or the device will be ignored!
1659 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1660 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1661
1662 // Push same device instance for next device to be added, so they'll have same identifier.
1663 mReader->pushNextDevice(device);
1664 mReader->pushNextDevice(device);
1665 ASSERT_NO_FATAL_FAILURE(
1666 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1667 ASSERT_NO_FATAL_FAILURE(
1668 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1669
1670 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001671 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001672}
1673
Chris Yee14523a2020-12-19 13:46:00 -08001674TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1675 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1676 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1677 // Add two subdevices to device
1678 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1679 // Must add at least one mapper or the device will be ignored!
1680 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1681 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1682
1683 // Push same device instance for next device to be added, so they'll have same identifier.
1684 mReader->pushNextDevice(device);
1685 mReader->pushNextDevice(device);
1686 // Sensor device is initially disabled
1687 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1688 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1689 nullptr));
1690 // Device is disabled because the only sub device is a sensor device and disabled initially.
1691 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1692 ASSERT_FALSE(device->isEnabled());
1693 ASSERT_NO_FATAL_FAILURE(
1694 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1695 // The merged device is enabled if any sub device is enabled
1696 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1697 ASSERT_TRUE(device->isEnabled());
1698}
1699
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001700TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001701 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001702 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001703 constexpr int32_t eventHubId = 1;
1704 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001705 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001707 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001709
Yi Kong9b14ac62018-07-17 13:48:38 -07001710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001711
1712 NotifyDeviceResetArgs resetArgs;
1713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714 ASSERT_EQ(deviceId, resetArgs.deviceId);
1715
1716 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001717 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001718 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001719
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001721 ASSERT_EQ(deviceId, resetArgs.deviceId);
1722 ASSERT_EQ(device->isEnabled(), false);
1723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001724 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001725 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001728 ASSERT_EQ(device->isEnabled(), false);
1729
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001730 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001731 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001733 ASSERT_EQ(deviceId, resetArgs.deviceId);
1734 ASSERT_EQ(device->isEnabled(), true);
1735}
1736
Michael Wrightd02c5b62014-02-10 15:10:22 -08001737TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001738 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001739 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001741 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001743 AINPUT_SOURCE_KEYBOARD, nullptr);
1744 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001745
1746 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1747 AINPUT_SOURCE_ANY, AKEYCODE_A))
1748 << "Should return unknown when the device id is >= 0 but unknown.";
1749
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001750 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1751 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1752 << "Should return unknown when the device id is valid but the sources are not "
1753 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001754
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001755 ASSERT_EQ(AKEY_STATE_DOWN,
1756 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1757 AKEYCODE_A))
1758 << "Should return value provided by mapper when device id is valid and the device "
1759 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001760
1761 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1762 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1763 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1764
1765 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1766 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1767 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1768}
1769
Philip Junker4af3b3d2021-12-14 10:36:55 +01001770TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1771 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1772 constexpr int32_t eventHubId = 1;
1773 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1774 InputDeviceClass::KEYBOARD,
1775 AINPUT_SOURCE_KEYBOARD, nullptr);
1776 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1777
1778 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1779 << "Should return unknown when the device with the specified id is not found.";
1780
1781 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1782 << "Should return correct mapping when device id is valid and mapping exists.";
1783
1784 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1785 << "Should return the location key code when device id is valid and there's no "
1786 "mapping.";
1787}
1788
1789TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1790 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1791 constexpr int32_t eventHubId = 1;
1792 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1793 InputDeviceClass::JOYSTICK,
1794 AINPUT_SOURCE_GAMEPAD, nullptr);
1795 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1796
1797 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1798 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1799}
1800
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001802 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001803 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001805 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 AINPUT_SOURCE_KEYBOARD, nullptr);
1808 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809
1810 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1811 AINPUT_SOURCE_ANY, KEY_A))
1812 << "Should return unknown when the device id is >= 0 but unknown.";
1813
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1815 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1816 << "Should return unknown when the device id is valid but the sources are not "
1817 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 ASSERT_EQ(AKEY_STATE_DOWN,
1820 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1821 KEY_A))
1822 << "Should return value provided by mapper when device id is valid and the device "
1823 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824
1825 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1826 AINPUT_SOURCE_TRACKBALL, KEY_A))
1827 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1828
1829 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1830 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1831 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1832}
1833
1834TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001835 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001836 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001838 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001839 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001840 AINPUT_SOURCE_KEYBOARD, nullptr);
1841 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842
1843 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1844 AINPUT_SOURCE_ANY, SW_LID))
1845 << "Should return unknown when the device id is >= 0 but unknown.";
1846
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1848 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1849 << "Should return unknown when the device id is valid but the sources are not "
1850 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 ASSERT_EQ(AKEY_STATE_DOWN,
1853 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1854 SW_LID))
1855 << "Should return value provided by mapper when device id is valid and the device "
1856 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857
1858 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1859 AINPUT_SOURCE_TRACKBALL, SW_LID))
1860 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1861
1862 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1863 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1864 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1865}
1866
1867TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001869 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001875 mapper.addSupportedKeyCode(AKEYCODE_A);
1876 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001877
1878 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1879 uint8_t flags[4] = { 0, 0, 0, 1 };
1880
1881 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1882 << "Should return false when device id is >= 0 but unknown.";
1883 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1884
1885 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001886 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1887 << "Should return false when device id is valid but the sources are not supported by "
1888 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1890
1891 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001892 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1893 keyCodes, flags))
1894 << "Should return value provided by mapper when device id is valid and the device "
1895 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1897
1898 flags[3] = 1;
1899 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1900 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1901 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1902
1903 flags[3] = 1;
1904 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1905 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1906 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1907}
1908
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001909TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001911 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001912
1913 NotifyConfigurationChangedArgs args;
1914
1915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1916 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1917}
1918
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001919TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001920 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001921 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001922 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001923 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001924 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001925 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001927 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001928
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001929 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001930 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001931 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1932
1933 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001934 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001935 ASSERT_EQ(when, event.when);
1936 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001937 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001938 ASSERT_EQ(EV_KEY, event.type);
1939 ASSERT_EQ(KEY_A, event.code);
1940 ASSERT_EQ(1, event.value);
1941}
1942
Garfield Tan1c7bc862020-01-28 13:24:04 -08001943TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001944 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001945 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001946 constexpr int32_t eventHubId = 1;
1947 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001948 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001949 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001950 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001951 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001952
1953 NotifyDeviceResetArgs resetArgs;
1954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001955 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001957 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001958 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001960 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001961 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001963 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001964 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001966 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001967 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001969 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001970 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001972 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001973 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001974}
1975
Garfield Tan1c7bc862020-01-28 13:24:04 -08001976TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1977 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001978 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001979 constexpr int32_t eventHubId = 1;
1980 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1981 // Must add at least one mapper or the device will be ignored!
1982 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001983 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001984 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1985
1986 NotifyDeviceResetArgs resetArgs;
1987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1988 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1989}
1990
Arthur Hungc23540e2018-11-29 20:42:11 +08001991TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001992 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001993 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001994 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001995 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001996 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1997 FakeInputMapper& mapper =
1998 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001999 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002000
2001 const uint8_t hdmi1 = 1;
2002
2003 // Associated touch screen with second display.
2004 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2005
2006 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002007 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002008 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002009 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002010 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002011 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002012 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002013 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002014 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002015 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002016
2017 // Add the device, and make sure all of the callbacks are triggered.
2018 // The device is added after the input port associations are processed since
2019 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002020 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002023 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002024
Arthur Hung2c9a3342019-07-23 14:18:59 +08002025 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002026 ASSERT_EQ(deviceId, device->getId());
2027 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2028 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002029
2030 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002031 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002032 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002033 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002034}
2035
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002036TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2037 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002038 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002039 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2040 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2041 // Must add at least one mapper or the device will be ignored!
2042 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2043 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2044 mReader->pushNextDevice(device);
2045 mReader->pushNextDevice(device);
2046 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2047 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2048
2049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2050
2051 NotifyDeviceResetArgs resetArgs;
2052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2053 ASSERT_EQ(deviceId, resetArgs.deviceId);
2054 ASSERT_TRUE(device->isEnabled());
2055 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2056 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2057
2058 disableDevice(deviceId);
2059 mReader->loopOnce();
2060
2061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2062 ASSERT_EQ(deviceId, resetArgs.deviceId);
2063 ASSERT_FALSE(device->isEnabled());
2064 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2065 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2066
2067 enableDevice(deviceId);
2068 mReader->loopOnce();
2069
2070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2071 ASSERT_EQ(deviceId, resetArgs.deviceId);
2072 ASSERT_TRUE(device->isEnabled());
2073 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2074 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2075}
2076
2077TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2078 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002079 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002080 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2081 // Add two subdevices to device
2082 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2083 FakeInputMapper& mapperDevice1 =
2084 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2085 FakeInputMapper& mapperDevice2 =
2086 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2087 mReader->pushNextDevice(device);
2088 mReader->pushNextDevice(device);
2089 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2090 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2091
2092 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2093 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2094
2095 ASSERT_EQ(AKEY_STATE_DOWN,
2096 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2097 ASSERT_EQ(AKEY_STATE_DOWN,
2098 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2099 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2100 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2101}
2102
Prabir Pradhan7e186182020-11-10 13:56:45 -08002103TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2104 NotifyPointerCaptureChangedArgs args;
2105
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002106 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002107 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2108 mReader->loopOnce();
2109 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002110 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2111 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002112
2113 mFakePolicy->setPointerCapture(false);
2114 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2115 mReader->loopOnce();
2116 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002117 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002118
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002119 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002120 // does not change.
2121 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2122 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002123 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002124}
2125
Chris Ye87143712020-11-10 05:05:58 +00002126class FakeVibratorInputMapper : public FakeInputMapper {
2127public:
2128 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2129 : FakeInputMapper(deviceContext, sources) {}
2130
2131 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2132};
2133
2134TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2135 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002136 ftl::Flags<InputDeviceClass> deviceClass =
2137 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002138 constexpr int32_t eventHubId = 1;
2139 const char* DEVICE_LOCATION = "BLUETOOTH";
2140 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2141 FakeVibratorInputMapper& mapper =
2142 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2143 mReader->pushNextDevice(device);
2144
2145 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2146 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2147
2148 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2149 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2150}
2151
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002152// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002153
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002154class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002155public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002156 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002157
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002158 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002159
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002160 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2161
Chris Yee2b1e5c2021-03-10 22:45:12 -08002162 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2163
2164 void dump(std::string& dump) override {}
2165
2166 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2167 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002168 }
2169
Chris Yee2b1e5c2021-03-10 22:45:12 -08002170 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2171 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002172 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002173
2174 bool setLightColor(int32_t lightId, int32_t color) override {
2175 getDeviceContext().setLightBrightness(lightId, color >> 24);
2176 return true;
2177 }
2178
2179 std::optional<int32_t> getLightColor(int32_t lightId) override {
2180 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2181 if (!result.has_value()) {
2182 return std::nullopt;
2183 }
2184 return result.value() << 24;
2185 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002186
2187 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2188
2189 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2190
2191private:
2192 InputDeviceContext& mDeviceContext;
2193 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2194 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002195 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002196};
2197
Chris Yee2b1e5c2021-03-10 22:45:12 -08002198TEST_F(InputReaderTest, BatteryGetCapacity) {
2199 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002200 ftl::Flags<InputDeviceClass> deviceClass =
2201 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002202 constexpr int32_t eventHubId = 1;
2203 const char* DEVICE_LOCATION = "BLUETOOTH";
2204 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002205 FakePeripheralController& controller =
2206 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002207 mReader->pushNextDevice(device);
2208
2209 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2210
2211 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2212 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2213}
2214
2215TEST_F(InputReaderTest, BatteryGetStatus) {
2216 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002217 ftl::Flags<InputDeviceClass> deviceClass =
2218 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002219 constexpr int32_t eventHubId = 1;
2220 const char* DEVICE_LOCATION = "BLUETOOTH";
2221 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002222 FakePeripheralController& controller =
2223 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002224 mReader->pushNextDevice(device);
2225
2226 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2227
2228 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2229 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2230}
2231
Chris Ye3fdbfef2021-01-06 18:45:18 -08002232TEST_F(InputReaderTest, LightGetColor) {
2233 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002234 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002235 constexpr int32_t eventHubId = 1;
2236 const char* DEVICE_LOCATION = "BLUETOOTH";
2237 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002238 FakePeripheralController& controller =
2239 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002240 mReader->pushNextDevice(device);
2241 RawLightInfo info = {.id = 1,
2242 .name = "Mono",
2243 .maxBrightness = 255,
2244 .flags = InputLightClass::BRIGHTNESS,
2245 .path = ""};
2246 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2247 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2248
2249 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002250
Chris Yee2b1e5c2021-03-10 22:45:12 -08002251 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2252 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002253 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2254 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2255}
2256
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002257// --- InputReaderIntegrationTest ---
2258
2259// These tests create and interact with the InputReader only through its interface.
2260// The InputReader is started during SetUp(), which starts its processing in its own
2261// thread. The tests use linux uinput to emulate input devices.
2262// NOTE: Interacting with the physical device while these tests are running may cause
2263// the tests to fail.
2264class InputReaderIntegrationTest : public testing::Test {
2265protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002266 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002267 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002268 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002269
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002270 std::shared_ptr<FakePointerController> mFakePointerController;
2271
Chris Yea52ade12020-08-27 16:49:20 -07002272 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002273 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002274 mFakePointerController = std::make_shared<FakePointerController>();
2275 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002276 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2277 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002278
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002279 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2280 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002281 ASSERT_EQ(mReader->start(), OK);
2282
2283 // Since this test is run on a real device, all the input devices connected
2284 // to the test device will show up in mReader. We wait for those input devices to
2285 // show up before beginning the tests.
2286 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2287 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2288 }
2289
Chris Yea52ade12020-08-27 16:49:20 -07002290 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002291 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002292 mReader.reset();
2293 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002294 mFakePolicy.clear();
2295 }
2296};
2297
2298TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2299 // An invalid input device that is only used for this test.
2300 class InvalidUinputDevice : public UinputDevice {
2301 public:
2302 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2303
2304 private:
2305 void configureDevice(int fd, uinput_user_dev* device) override {}
2306 };
2307
2308 const size_t numDevices = mFakePolicy->getInputDevices().size();
2309
2310 // UinputDevice does not set any event or key bits, so InputReader should not
2311 // consider it as a valid device.
2312 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2313 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2315 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2316
2317 invalidDevice.reset();
2318 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2319 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2320 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2321}
2322
2323TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2324 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2325
2326 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2327 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2328 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2329 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2330
2331 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002332 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002333 const auto& it =
2334 std::find_if(inputDevices.begin(), inputDevices.end(),
2335 [&keyboard](const InputDeviceInfo& info) {
2336 return info.getIdentifier().name == keyboard->getName();
2337 });
2338
2339 ASSERT_NE(it, inputDevices.end());
2340 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2341 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2342 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002343
2344 keyboard.reset();
2345 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2347 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2348}
2349
2350TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2351 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2352 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2353
2354 NotifyConfigurationChangedArgs configChangedArgs;
2355 ASSERT_NO_FATAL_FAILURE(
2356 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002357 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002358 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2359
2360 NotifyKeyArgs keyArgs;
2361 keyboard->pressAndReleaseHomeKey();
2362 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2363 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002364 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002365 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002366 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002367 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002368 prevTimestamp = keyArgs.eventTime;
2369
2370 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2371 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002372 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002373 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002374 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002375}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002376
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002377/**
2378 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2379 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2380 * are passed to the listener.
2381 */
2382static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2383TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2384 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2385 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2386 NotifyKeyArgs keyArgs;
2387
2388 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2390 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2391 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2392
2393 controller->pressAndReleaseKey(BTN_GEAR_UP);
2394 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2395 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2396 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2397}
2398
Arthur Hungaab25622020-01-16 11:22:11 +08002399// --- TouchProcessTest ---
2400class TouchIntegrationTest : public InputReaderIntegrationTest {
2401protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002402 const std::string UNIQUE_ID = "local:0";
2403
Chris Yea52ade12020-08-27 16:49:20 -07002404 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002405 InputReaderIntegrationTest::SetUp();
2406 // At least add an internal display.
2407 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2408 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002409 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002410
2411 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2414 }
2415
2416 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2417 int32_t orientation, const std::string& uniqueId,
2418 std::optional<uint8_t> physicalPort,
2419 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002420 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2421 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002422 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2423 }
2424
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002425 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2426 NotifyMotionArgs args;
2427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2428 EXPECT_EQ(action, args.action);
2429 ASSERT_EQ(points.size(), args.pointerCount);
2430 for (size_t i = 0; i < args.pointerCount; i++) {
2431 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2432 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2433 }
2434 }
2435
Arthur Hungaab25622020-01-16 11:22:11 +08002436 std::unique_ptr<UinputTouchScreen> mDevice;
2437};
2438
2439TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2440 NotifyMotionArgs args;
2441 const Point centerPoint = mDevice->getCenterPoint();
2442
2443 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002444 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002445 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002446 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002447 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2448 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2449
2450 // ACTION_MOVE
2451 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002452 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2455
2456 // ACTION_UP
2457 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002458 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002459 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2460 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2461}
2462
2463TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2464 NotifyMotionArgs args;
2465 const Point centerPoint = mDevice->getCenterPoint();
2466
2467 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002468 mDevice->sendSlot(FIRST_SLOT);
2469 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002470 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002471 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2474
2475 // ACTION_POINTER_DOWN (Second slot)
2476 const Point secondPoint = centerPoint + Point(100, 100);
2477 mDevice->sendSlot(SECOND_SLOT);
2478 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002479 mDevice->sendDown(secondPoint);
2480 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002482 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002483
2484 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002485 mDevice->sendMove(secondPoint + Point(1, 1));
2486 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002487 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2489
2490 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002491 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002492 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002493 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002494 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002495
2496 // ACTION_UP
2497 mDevice->sendSlot(FIRST_SLOT);
2498 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002499 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2501 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2502}
2503
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002504/**
2505 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2506 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2507 * data?
2508 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2509 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2510 * for Pointer 0 only is generated after.
2511 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2512 * events, we will not miss any information.
2513 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2514 * event generated afterwards that contains the newest movement of pointer 0.
2515 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2516 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2517 * losing information about non-palm pointers.
2518 */
2519TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2520 NotifyMotionArgs args;
2521 const Point centerPoint = mDevice->getCenterPoint();
2522
2523 // ACTION_DOWN
2524 mDevice->sendSlot(FIRST_SLOT);
2525 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2526 mDevice->sendDown(centerPoint);
2527 mDevice->sendSync();
2528 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2529
2530 // ACTION_POINTER_DOWN (Second slot)
2531 const Point secondPoint = centerPoint + Point(100, 100);
2532 mDevice->sendSlot(SECOND_SLOT);
2533 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2534 mDevice->sendDown(secondPoint);
2535 mDevice->sendSync();
2536 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2537
2538 // ACTION_MOVE (First slot)
2539 mDevice->sendSlot(FIRST_SLOT);
2540 mDevice->sendMove(centerPoint + Point(5, 5));
2541 // ACTION_POINTER_UP (Second slot)
2542 mDevice->sendSlot(SECOND_SLOT);
2543 mDevice->sendPointerUp();
2544 // Send a single sync for the above 2 pointer updates
2545 mDevice->sendSync();
2546
2547 // First, we should get POINTER_UP for the second pointer
2548 assertReceivedMotion(ACTION_POINTER_1_UP,
2549 {/*first pointer */ centerPoint + Point(5, 5),
2550 /*second pointer*/ secondPoint});
2551
2552 // Next, the MOVE event for the first pointer
2553 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2554}
2555
2556/**
2557 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2558 * move, and then it will go up, all in the same frame.
2559 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2560 * gets sent to the listener.
2561 */
2562TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2563 NotifyMotionArgs args;
2564 const Point centerPoint = mDevice->getCenterPoint();
2565
2566 // ACTION_DOWN
2567 mDevice->sendSlot(FIRST_SLOT);
2568 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2569 mDevice->sendDown(centerPoint);
2570 mDevice->sendSync();
2571 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2572
2573 // ACTION_POINTER_DOWN (Second slot)
2574 const Point secondPoint = centerPoint + Point(100, 100);
2575 mDevice->sendSlot(SECOND_SLOT);
2576 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2577 mDevice->sendDown(secondPoint);
2578 mDevice->sendSync();
2579 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2580
2581 // ACTION_MOVE (First slot)
2582 mDevice->sendSlot(FIRST_SLOT);
2583 mDevice->sendMove(centerPoint + Point(5, 5));
2584 // ACTION_POINTER_UP (Second slot)
2585 mDevice->sendSlot(SECOND_SLOT);
2586 mDevice->sendMove(secondPoint + Point(6, 6));
2587 mDevice->sendPointerUp();
2588 // Send a single sync for the above 2 pointer updates
2589 mDevice->sendSync();
2590
2591 // First, we should get POINTER_UP for the second pointer
2592 // The movement of the second pointer during the liftoff frame is ignored.
2593 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2594 assertReceivedMotion(ACTION_POINTER_1_UP,
2595 {/*first pointer */ centerPoint + Point(5, 5),
2596 /*second pointer*/ secondPoint});
2597
2598 // Next, the MOVE event for the first pointer
2599 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2600}
2601
Arthur Hungaab25622020-01-16 11:22:11 +08002602TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2603 NotifyMotionArgs args;
2604 const Point centerPoint = mDevice->getCenterPoint();
2605
2606 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002607 mDevice->sendSlot(FIRST_SLOT);
2608 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002609 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002610 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002611 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2612 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2613
arthurhungcc7f9802020-04-30 17:55:40 +08002614 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002615 const Point secondPoint = centerPoint + Point(100, 100);
2616 mDevice->sendSlot(SECOND_SLOT);
2617 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2618 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002619 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002620 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002621 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002622
arthurhungcc7f9802020-04-30 17:55:40 +08002623 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002624 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002625 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002626 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2628
arthurhungcc7f9802020-04-30 17:55:40 +08002629 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2630 // a palm event.
2631 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002632 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002633 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002634 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002635 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002636 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002637
arthurhungcc7f9802020-04-30 17:55:40 +08002638 // Send up to second slot, expect first slot send moving.
2639 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002640 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002641 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002643
arthurhungcc7f9802020-04-30 17:55:40 +08002644 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002645 mDevice->sendSlot(FIRST_SLOT);
2646 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002647 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002648
arthurhungcc7f9802020-04-30 17:55:40 +08002649 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2650 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002651}
2652
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654class InputDeviceTest : public testing::Test {
2655protected:
2656 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002657 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658 static const int32_t DEVICE_ID;
2659 static const int32_t DEVICE_GENERATION;
2660 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002661 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002662 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002664 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002666 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002667 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002668 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669
Chris Yea52ade12020-08-27 16:49:20 -07002670 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002671 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002673 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002674 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002675 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676 InputDeviceIdentifier identifier;
2677 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002678 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002679 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002680 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002681 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002682 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002683 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684 }
2685
Chris Yea52ade12020-08-27 16:49:20 -07002686 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002687 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 }
2690};
2691
2692const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002693const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002694const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2696const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002697const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002698 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002699const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700
2701TEST_F(InputDeviceTest, ImmutableProperties) {
2702 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002703 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002704 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705}
2706
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002707TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2708 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002709}
2710
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2712 // Configuration.
2713 InputReaderConfiguration config;
2714 mDevice->configure(ARBITRARY_TIME, &config, 0);
2715
2716 // Reset.
2717 mDevice->reset(ARBITRARY_TIME);
2718
2719 NotifyDeviceResetArgs resetArgs;
2720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2721 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2722 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2723
2724 // Metadata.
2725 ASSERT_TRUE(mDevice->isIgnored());
2726 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2727
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002728 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002730 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2732 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2733
2734 // State queries.
2735 ASSERT_EQ(0, mDevice->getMetaState());
2736
2737 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2738 << "Ignored device should return unknown key code state.";
2739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2740 << "Ignored device should return unknown scan code state.";
2741 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2742 << "Ignored device should return unknown switch state.";
2743
2744 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2745 uint8_t flags[2] = { 0, 1 };
2746 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2747 << "Ignored device should never mark any key codes.";
2748 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2749 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2750}
2751
2752TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2753 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002754 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002756 FakeInputMapper& mapper1 =
2757 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002758 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2759 mapper1.setMetaState(AMETA_ALT_ON);
2760 mapper1.addSupportedKeyCode(AKEYCODE_A);
2761 mapper1.addSupportedKeyCode(AKEYCODE_B);
2762 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2763 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2764 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2765 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2766 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002768 FakeInputMapper& mapper2 =
2769 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002770 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771
2772 InputReaderConfiguration config;
2773 mDevice->configure(ARBITRARY_TIME, &config, 0);
2774
2775 String8 propertyValue;
2776 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2777 << "Device should have read configuration during configuration phase.";
2778 ASSERT_STREQ("value", propertyValue.string());
2779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002780 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2781 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782
2783 // Reset
2784 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002785 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2786 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787
2788 NotifyDeviceResetArgs resetArgs;
2789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2790 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2791 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2792
2793 // Metadata.
2794 ASSERT_FALSE(mDevice->isIgnored());
2795 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2796
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002797 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002799 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2801 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2802
2803 // State queries.
2804 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2805 << "Should query mappers and combine meta states.";
2806
2807 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2808 << "Should return unknown key code state when source not supported.";
2809 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2810 << "Should return unknown scan code state when source not supported.";
2811 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2812 << "Should return unknown switch state when source not supported.";
2813
2814 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2815 << "Should query mapper when source is supported.";
2816 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2817 << "Should query mapper when source is supported.";
2818 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2819 << "Should query mapper when source is supported.";
2820
2821 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2822 uint8_t flags[4] = { 0, 0, 0, 1 };
2823 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2824 << "Should do nothing when source is unsupported.";
2825 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2826 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2827 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2828 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2829
2830 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2831 << "Should query mapper when source is supported.";
2832 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2833 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2834 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2835 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2836
2837 // Event handling.
2838 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002839 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002840 mDevice->process(&event, 1);
2841
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002842 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2843 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844}
2845
Arthur Hung2c9a3342019-07-23 14:18:59 +08002846// A single input device is associated with a specific display. Check that:
2847// 1. Device is disabled if the viewport corresponding to the associated display is not found
2848// 2. Device is disabled when setEnabled API is called
2849TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002850 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002851
2852 // First Configuration.
2853 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2854
2855 // Device should be enabled by default.
2856 ASSERT_TRUE(mDevice->isEnabled());
2857
2858 // Prepare associated info.
2859 constexpr uint8_t hdmi = 1;
2860 const std::string UNIQUE_ID = "local:1";
2861
2862 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2863 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2864 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2865 // Device should be disabled because it is associated with a specific display via
2866 // input port <-> display port association, but the corresponding display is not found
2867 ASSERT_FALSE(mDevice->isEnabled());
2868
2869 // Prepare displays.
2870 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002871 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2872 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002873 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2874 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2875 ASSERT_TRUE(mDevice->isEnabled());
2876
2877 // Device should be disabled after set disable.
2878 mFakePolicy->addDisabledDevice(mDevice->getId());
2879 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2880 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2881 ASSERT_FALSE(mDevice->isEnabled());
2882
2883 // Device should still be disabled even found the associated display.
2884 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2885 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2886 ASSERT_FALSE(mDevice->isEnabled());
2887}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888
Christine Franks1ba71cc2021-04-07 14:37:42 -07002889TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2890 // Device should be enabled by default.
2891 mFakePolicy->clearViewports();
2892 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2893 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2894 ASSERT_TRUE(mDevice->isEnabled());
2895
2896 // Device should be disabled because it is associated with a specific display, but the
2897 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002898 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002899 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2900 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2901 ASSERT_FALSE(mDevice->isEnabled());
2902
2903 // Device should be enabled when a display is found.
2904 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2905 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2906 NO_PORT, ViewportType::INTERNAL);
2907 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2908 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2909 ASSERT_TRUE(mDevice->isEnabled());
2910
2911 // Device should be disabled after set disable.
2912 mFakePolicy->addDisabledDevice(mDevice->getId());
2913 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2914 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2915 ASSERT_FALSE(mDevice->isEnabled());
2916
2917 // Device should still be disabled even found the associated display.
2918 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2919 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2920 ASSERT_FALSE(mDevice->isEnabled());
2921}
2922
Christine Franks2a2293c2022-01-18 11:51:16 -08002923TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2924 mFakePolicy->clearViewports();
2925 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2926 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2927
Christine Franks2a2293c2022-01-18 11:51:16 -08002928 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2929 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2930 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2931 NO_PORT, ViewportType::INTERNAL);
2932 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2933 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2934 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2935}
2936
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002937/**
2938 * This test reproduces a crash caused by a dangling reference that remains after device is added
2939 * and removed. The reference is accessed in InputDevice::dump(..);
2940 */
2941TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2942 constexpr int32_t TEST_EVENTHUB_ID = 10;
2943 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2944
2945 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
2946 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
2947 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2948 std::string dumpStr, eventHubDevStr;
2949 device.dump(dumpStr, eventHubDevStr);
2950}
2951
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952// --- InputMapperTest ---
2953
2954class InputMapperTest : public testing::Test {
2955protected:
2956 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002957 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 static const int32_t DEVICE_ID;
2959 static const int32_t DEVICE_GENERATION;
2960 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002961 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002962 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002964 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002966 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002967 std::unique_ptr<InstrumentedInputReader> mReader;
2968 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002970 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002971 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002973 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002974 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002975 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002976 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002977 // Consume the device reset notification generated when adding a new device.
2978 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979 }
2980
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002981 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002982 SetUp(DEVICE_CLASSES);
2983 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002984
Chris Yea52ade12020-08-27 16:49:20 -07002985 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002986 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 }
2989
2990 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002991 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992 }
2993
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002994 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002995 if (!changes ||
2996 (changes &
2997 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2998 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002999 mReader->requestRefreshConfiguration(changes);
3000 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003001 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003002 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhan36690412022-08-05 22:26:56 +00003003 // Loop the reader to flush the input listener queue.
3004 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003005 }
3006
arthurhungdcef2dc2020-08-11 14:47:50 +08003007 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3008 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003009 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003010 InputDeviceIdentifier identifier;
3011 identifier.name = name;
3012 identifier.location = location;
3013 std::shared_ptr<InputDevice> device =
3014 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3015 identifier);
3016 mReader->pushNextDevice(device);
3017 mFakeEventHub->addDevice(eventHubId, name, classes);
3018 mReader->loopOnce();
3019 return device;
3020 }
3021
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003022 template <class T, typename... Args>
3023 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003024 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003025 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003027 mapper.reset(ARBITRARY_TIME);
Prabir Pradhan36690412022-08-05 22:26:56 +00003028 // Loop the reader to flush the input listener queue.
3029 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003030 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 }
3032
3033 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003034 int32_t orientation, const std::string& uniqueId,
3035 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003036 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3037 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003038 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3039 }
3040
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003041 void clearViewports() {
3042 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043 }
3044
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003045 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3046 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 RawEvent event;
3048 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003049 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003050 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 event.type = type;
3052 event.code = code;
3053 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003054 mapper.process(&event);
Prabir Pradhan36690412022-08-05 22:26:56 +00003055 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003056 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057 }
3058
3059 static void assertMotionRange(const InputDeviceInfo& info,
3060 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3061 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003062 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3064 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3065 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3066 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3067 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3068 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3069 }
3070
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003071 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3072 float size, float touchMajor, float touchMinor, float toolMajor,
3073 float toolMinor, float orientation, float distance,
3074 float scaledAxisEpsilon = 1.f) {
3075 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3076 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3078 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003079 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3080 scaledAxisEpsilon);
3081 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3082 scaledAxisEpsilon);
3083 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3084 scaledAxisEpsilon);
3085 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3086 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3088 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3089 }
3090
Michael Wright17db18e2020-06-26 20:51:44 +01003091 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003093 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094 ASSERT_NEAR(x, actualX, 1);
3095 ASSERT_NEAR(y, actualY, 1);
3096 }
3097};
3098
3099const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003100const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003101const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3103const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003104const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3105 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003106const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107
3108// --- SwitchInputMapperTest ---
3109
3110class SwitchInputMapperTest : public InputMapperTest {
3111protected:
3112};
3113
3114TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003115 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003117 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118}
3119
3120TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003121 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003123 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003124 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003126 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003127 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128}
3129
3130TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003131 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137
3138 NotifySwitchArgs args;
3139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3140 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003141 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3142 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 args.switchMask);
3144 ASSERT_EQ(uint32_t(0), args.policyFlags);
3145}
3146
Chris Ye87143712020-11-10 05:05:58 +00003147// --- VibratorInputMapperTest ---
3148class VibratorInputMapperTest : public InputMapperTest {
3149protected:
3150 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3151};
3152
3153TEST_F(VibratorInputMapperTest, GetSources) {
3154 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3155
3156 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3157}
3158
3159TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3160 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3161
3162 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3163}
3164
3165TEST_F(VibratorInputMapperTest, Vibrate) {
3166 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003167 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003168 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3169
3170 VibrationElement pattern(2);
3171 VibrationSequence sequence(2);
3172 pattern.duration = std::chrono::milliseconds(200);
3173 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3174 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3175 sequence.addElement(pattern);
3176 pattern.duration = std::chrono::milliseconds(500);
3177 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3178 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3179 sequence.addElement(pattern);
3180
3181 std::vector<int64_t> timings = {0, 1};
3182 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3183
3184 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003185 // Start vibrating
3186 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003187 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003188 // Verify vibrator state listener was notified.
3189 mReader->loopOnce();
3190 NotifyVibratorStateArgs args;
3191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3192 ASSERT_EQ(DEVICE_ID, args.deviceId);
3193 ASSERT_TRUE(args.isOn);
3194 // Stop vibrating
3195 mapper.cancelVibrate(VIBRATION_TOKEN);
3196 ASSERT_FALSE(mapper.isVibrating());
3197 // Verify vibrator state listener was notified.
3198 mReader->loopOnce();
3199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3200 ASSERT_EQ(DEVICE_ID, args.deviceId);
3201 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003202}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203
Chris Yef59a2f42020-10-16 12:55:26 -07003204// --- SensorInputMapperTest ---
3205
3206class SensorInputMapperTest : public InputMapperTest {
3207protected:
3208 static const int32_t ACCEL_RAW_MIN;
3209 static const int32_t ACCEL_RAW_MAX;
3210 static const int32_t ACCEL_RAW_FUZZ;
3211 static const int32_t ACCEL_RAW_FLAT;
3212 static const int32_t ACCEL_RAW_RESOLUTION;
3213
3214 static const int32_t GYRO_RAW_MIN;
3215 static const int32_t GYRO_RAW_MAX;
3216 static const int32_t GYRO_RAW_FUZZ;
3217 static const int32_t GYRO_RAW_FLAT;
3218 static const int32_t GYRO_RAW_RESOLUTION;
3219
3220 static const float GRAVITY_MS2_UNIT;
3221 static const float DEGREE_RADIAN_UNIT;
3222
3223 void prepareAccelAxes();
3224 void prepareGyroAxes();
3225 void setAccelProperties();
3226 void setGyroProperties();
3227 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3228};
3229
3230const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3231const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3232const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3233const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3234const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3235
3236const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3237const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3238const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3239const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3240const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3241
3242const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3243const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3244
3245void SensorInputMapperTest::prepareAccelAxes() {
3246 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3247 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3248 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3249 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3250 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3251 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3252}
3253
3254void SensorInputMapperTest::prepareGyroAxes() {
3255 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3256 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3258 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3259 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3260 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3261}
3262
3263void SensorInputMapperTest::setAccelProperties() {
3264 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3265 /* sensorDataIndex */ 0);
3266 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3267 /* sensorDataIndex */ 1);
3268 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3269 /* sensorDataIndex */ 2);
3270 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3271 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3272 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3273 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3274 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3275}
3276
3277void SensorInputMapperTest::setGyroProperties() {
3278 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3279 /* sensorDataIndex */ 0);
3280 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3281 /* sensorDataIndex */ 1);
3282 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3283 /* sensorDataIndex */ 2);
3284 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3285 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3286 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3287 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3288 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3289}
3290
3291TEST_F(SensorInputMapperTest, GetSources) {
3292 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3293
3294 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3295}
3296
3297TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3298 setAccelProperties();
3299 prepareAccelAxes();
3300 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3301
3302 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3303 std::chrono::microseconds(10000),
3304 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003305 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003311
3312 NotifySensorArgs args;
3313 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3314 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3315 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3316
3317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3318 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3319 ASSERT_EQ(args.deviceId, DEVICE_ID);
3320 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3321 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3322 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3323 ASSERT_EQ(args.values, values);
3324 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3325}
3326
3327TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3328 setGyroProperties();
3329 prepareGyroAxes();
3330 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3331
3332 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3333 std::chrono::microseconds(10000),
3334 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003335 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003341
3342 NotifySensorArgs args;
3343 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3344 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3345 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3346
3347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3348 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3349 ASSERT_EQ(args.deviceId, DEVICE_ID);
3350 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3351 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3352 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3353 ASSERT_EQ(args.values, values);
3354 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3355}
3356
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357// --- KeyboardInputMapperTest ---
3358
3359class KeyboardInputMapperTest : public InputMapperTest {
3360protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003361 const std::string UNIQUE_ID = "local:0";
3362
3363 void prepareDisplay(int32_t orientation);
3364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003365 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003366 int32_t originalKeyCode, int32_t rotatedKeyCode,
3367 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368};
3369
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003370/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3371 * orientation.
3372 */
3373void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003374 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3375 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003376}
3377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003378void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003379 int32_t originalScanCode, int32_t originalKeyCode,
3380 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 NotifyKeyArgs args;
3382
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3386 ASSERT_EQ(originalScanCode, args.scanCode);
3387 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003388 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3392 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3393 ASSERT_EQ(originalScanCode, args.scanCode);
3394 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003395 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396}
3397
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003399 KeyboardInputMapper& mapper =
3400 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3401 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003403 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404}
3405
3406TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3407 const int32_t USAGE_A = 0x070004;
3408 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003409 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3410 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003411 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3412 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3413 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003415 KeyboardInputMapper& mapper =
3416 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3417 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003418 // Initial metastate is AMETA_NONE.
3419 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420
3421 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423 NotifyKeyArgs args;
3424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3425 ASSERT_EQ(DEVICE_ID, args.deviceId);
3426 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3427 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3428 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3429 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3430 ASSERT_EQ(KEY_HOME, args.scanCode);
3431 ASSERT_EQ(AMETA_NONE, args.metaState);
3432 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3433 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3434 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3435
3436 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003437 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3439 ASSERT_EQ(DEVICE_ID, args.deviceId);
3440 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3441 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3442 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3443 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3444 ASSERT_EQ(KEY_HOME, args.scanCode);
3445 ASSERT_EQ(AMETA_NONE, args.metaState);
3446 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3447 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3448 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3449
3450 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3454 ASSERT_EQ(DEVICE_ID, args.deviceId);
3455 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3456 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3457 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3458 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3459 ASSERT_EQ(0, args.scanCode);
3460 ASSERT_EQ(AMETA_NONE, args.metaState);
3461 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3462 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3463 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3464
3465 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3467 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3469 ASSERT_EQ(DEVICE_ID, args.deviceId);
3470 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3471 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3472 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3473 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3474 ASSERT_EQ(0, args.scanCode);
3475 ASSERT_EQ(AMETA_NONE, args.metaState);
3476 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3477 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3478 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3479
3480 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3484 ASSERT_EQ(DEVICE_ID, args.deviceId);
3485 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3486 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3487 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3488 ASSERT_EQ(0, args.keyCode);
3489 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3490 ASSERT_EQ(AMETA_NONE, args.metaState);
3491 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3492 ASSERT_EQ(0U, args.policyFlags);
3493 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3494
3495 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3497 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3499 ASSERT_EQ(DEVICE_ID, args.deviceId);
3500 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3501 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3502 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3503 ASSERT_EQ(0, args.keyCode);
3504 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3505 ASSERT_EQ(AMETA_NONE, args.metaState);
3506 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3507 ASSERT_EQ(0U, args.policyFlags);
3508 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3509}
3510
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511/**
3512 * Ensure that the readTime is set to the time when the EV_KEY is received.
3513 */
3514TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3515 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3516
3517 KeyboardInputMapper& mapper =
3518 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3519 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3520 NotifyKeyArgs args;
3521
3522 // Key down
3523 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3525 ASSERT_EQ(12, args.readTime);
3526
3527 // Key up
3528 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3530 ASSERT_EQ(15, args.readTime);
3531}
3532
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003534 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003536 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3537 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3538 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003540 KeyboardInputMapper& mapper =
3541 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3542 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543
Arthur Hung95f68612022-04-07 14:08:22 +08003544 // Initial metastate is AMETA_NONE.
3545 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
3547 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549 NotifyKeyArgs args;
3550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3551 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003552 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003553 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003554
3555 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003556 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560
3561 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003562 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3564 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566
3567 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003568 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3570 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003571 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003572 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573}
3574
3575TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3577 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003581 KeyboardInputMapper& mapper =
3582 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3583 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003585 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3587 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3588 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3589 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3590 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3591 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3593 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3594}
3595
3596TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003597 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3599 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3600 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003601
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003603 KeyboardInputMapper& mapper =
3604 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3605 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003607 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003608 ASSERT_NO_FATAL_FAILURE(
3609 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3610 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3611 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3613 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3615 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003617 clearViewports();
3618 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003619 ASSERT_NO_FATAL_FAILURE(
3620 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3622 AKEYCODE_DPAD_UP, DISPLAY_ID));
3623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3624 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3626 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003628 clearViewports();
3629 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003630 ASSERT_NO_FATAL_FAILURE(
3631 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3633 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3635 AKEYCODE_DPAD_UP, DISPLAY_ID));
3636 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3637 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003639 clearViewports();
3640 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003641 ASSERT_NO_FATAL_FAILURE(
3642 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3644 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3646 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3648 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003649
3650 // Special case: if orientation changes while key is down, we still emit the same keycode
3651 // in the key up as we did in the key down.
3652 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003653 clearViewports();
3654 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3657 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3658 ASSERT_EQ(KEY_UP, args.scanCode);
3659 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3660
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003661 clearViewports();
3662 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3665 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3666 ASSERT_EQ(KEY_UP, args.scanCode);
3667 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3668}
3669
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003670TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3671 // If the keyboard is not orientation aware,
3672 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003673 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003674
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003675 KeyboardInputMapper& mapper =
3676 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3677 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003678 NotifyKeyArgs args;
3679
3680 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3685 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3686
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003687 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3692 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3693}
3694
3695TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3696 // If the keyboard is orientation aware,
3697 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003698 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003699
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003700 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003701 KeyboardInputMapper& mapper =
3702 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3703 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003704 NotifyKeyArgs args;
3705
3706 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3707 // ^--- already checked by the previous test
3708
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003709 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003710 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3715 ASSERT_EQ(DISPLAY_ID, args.displayId);
3716
3717 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003718 clearViewports();
3719 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003720 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3725 ASSERT_EQ(newDisplayId, args.displayId);
3726}
3727
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003729 KeyboardInputMapper& mapper =
3730 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3731 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003733 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003736 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738}
3739
Philip Junker4af3b3d2021-12-14 10:36:55 +01003740TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3741 KeyboardInputMapper& mapper =
3742 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3743 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3744
3745 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3746 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3747 << "If a mapping is available, the result is equal to the mapping";
3748
3749 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3750 << "If no mapping is available, the result is the key location";
3751}
3752
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003754 KeyboardInputMapper& mapper =
3755 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3756 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003759 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003762 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003763}
3764
3765TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 KeyboardInputMapper& mapper =
3767 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3768 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771
3772 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3773 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775 ASSERT_TRUE(flags[0]);
3776 ASSERT_FALSE(flags[1]);
3777}
3778
3779TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3781 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3782 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3784 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 KeyboardInputMapper& mapper =
3788 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3789 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003790 // Initial metastate is AMETA_NONE.
3791 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792
3793 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003794 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3795 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3796 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
3798 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003801 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3802 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805
3806 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003809 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3810 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3811 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003812 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813
3814 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3818 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3819 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003820 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821
3822 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3826 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3827 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003828 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829
3830 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003833 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3834 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3835 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003836 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837
3838 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003841 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3842 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3843 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003844 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845}
3846
Chris Yea52ade12020-08-27 16:49:20 -07003847TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3848 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3849 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3850 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3851 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3852
3853 KeyboardInputMapper& mapper =
3854 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3855 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3856
Chris Yea52ade12020-08-27 16:49:20 -07003857 // Meta state should be AMETA_NONE after reset
3858 mapper.reset(ARBITRARY_TIME);
3859 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3860 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3861 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3862 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3863
3864 NotifyKeyArgs args;
3865 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3868 ASSERT_EQ(AMETA_NONE, args.metaState);
3869 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3870 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3871 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3872
3873 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003874 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3876 ASSERT_EQ(AMETA_NONE, args.metaState);
3877 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3878 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3879 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3880}
3881
Arthur Hung2c9a3342019-07-23 14:18:59 +08003882TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3883 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003884 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3885 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3886 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3887 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003888
3889 // keyboard 2.
3890 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003891 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003892 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003893 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003894 std::shared_ptr<InputDevice> device2 =
3895 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003896 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003897
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003898 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3899 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3900 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3901 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003902
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003903 KeyboardInputMapper& mapper =
3904 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3905 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003906
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003907 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003908 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003909 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003910 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3911 device2->reset(ARBITRARY_TIME);
3912
3913 // Prepared displays and associated info.
3914 constexpr uint8_t hdmi1 = 0;
3915 constexpr uint8_t hdmi2 = 1;
3916 const std::string SECONDARY_UNIQUE_ID = "local:1";
3917
3918 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3919 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3920
3921 // No associated display viewport found, should disable the device.
3922 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3923 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3924 ASSERT_FALSE(device2->isEnabled());
3925
3926 // Prepare second display.
3927 constexpr int32_t newDisplayId = 2;
3928 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003929 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003930 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003931 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003932 // Default device will reconfigure above, need additional reconfiguration for another device.
3933 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3934 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3935
3936 // Device should be enabled after the associated display is found.
3937 ASSERT_TRUE(mDevice->isEnabled());
3938 ASSERT_TRUE(device2->isEnabled());
3939
3940 // Test pad key events
3941 ASSERT_NO_FATAL_FAILURE(
3942 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3943 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3944 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3945 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3946 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3947 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3948 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3949
3950 ASSERT_NO_FATAL_FAILURE(
3951 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3952 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3953 AKEYCODE_DPAD_RIGHT, newDisplayId));
3954 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3955 AKEYCODE_DPAD_DOWN, newDisplayId));
3956 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3957 AKEYCODE_DPAD_LEFT, newDisplayId));
3958}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959
arthurhungc903df12020-08-11 15:08:42 +08003960TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3961 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3962 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3963 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3965 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3966 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3967
3968 KeyboardInputMapper& mapper =
3969 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3970 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003971 // Initial metastate is AMETA_NONE.
3972 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003973
3974 // Initialization should have turned all of the lights off.
3975 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3976 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3977 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3978
3979 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003982 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3983 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3984
3985 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003988 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3989 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3990
3991 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003994 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3995 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3996
3997 mFakeEventHub->removeDevice(EVENTHUB_ID);
3998 mReader->loopOnce();
3999
4000 // keyboard 2 should default toggle keys.
4001 const std::string USB2 = "USB2";
4002 const std::string DEVICE_NAME2 = "KEYBOARD2";
4003 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4004 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4005 std::shared_ptr<InputDevice> device2 =
4006 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004007 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004008 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4009 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4010 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4011 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4012 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4013 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4014
arthurhung6fe95782020-10-05 22:41:16 +08004015 KeyboardInputMapper& mapper2 =
4016 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4017 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004018 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4019 device2->reset(ARBITRARY_TIME);
4020
4021 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4022 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4023 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004024 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4025 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004026}
4027
Arthur Hungcb40a002021-08-03 14:31:01 +00004028TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4029 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4030 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4032
4033 // Suppose we have two mappers. (DPAD + KEYBOARD)
4034 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4035 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4036 KeyboardInputMapper& mapper =
4037 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4038 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004039 // Initial metastate is AMETA_NONE.
4040 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004041
4042 mReader->toggleCapsLockState(DEVICE_ID);
4043 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4044}
4045
Arthur Hungfb3cc112022-04-13 07:39:50 +00004046TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4047 // keyboard 1.
4048 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4049 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4050 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4052 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4054
4055 KeyboardInputMapper& mapper1 =
4056 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4057 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4058
4059 // keyboard 2.
4060 const std::string USB2 = "USB2";
4061 const std::string DEVICE_NAME2 = "KEYBOARD2";
4062 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4063 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4064 std::shared_ptr<InputDevice> device2 =
4065 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4066 ftl::Flags<InputDeviceClass>(0));
4067 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4068 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4069 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4070 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4071 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4072 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4073
4074 KeyboardInputMapper& mapper2 =
4075 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4076 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4077 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4078 device2->reset(ARBITRARY_TIME);
4079
Arthur Hung95f68612022-04-07 14:08:22 +08004080 // Initial metastate is AMETA_NONE.
4081 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4082 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4083
4084 // Toggle num lock on and off.
4085 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4086 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004087 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4088 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4089 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4090
4091 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4092 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4093 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4094 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4095 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4096
4097 // Toggle caps lock on and off.
4098 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4099 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4100 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4101 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4102 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4103
4104 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4105 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4106 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4107 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4108 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4109
4110 // Toggle scroll lock on and off.
4111 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4112 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4113 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4114 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4115 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4116
4117 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4118 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4119 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4120 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4121 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4122}
4123
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004124// --- KeyboardInputMapperTest_ExternalDevice ---
4125
4126class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4127protected:
Chris Yea52ade12020-08-27 16:49:20 -07004128 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004129};
4130
4131TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004132 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4133 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004134
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4138 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140 KeyboardInputMapper& mapper =
4141 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4142 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 NotifyKeyArgs args;
4146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4147 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4148
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4151 ASSERT_EQ(uint32_t(0), args.policyFlags);
4152
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4155 ASSERT_EQ(uint32_t(0), args.policyFlags);
4156
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4159 ASSERT_EQ(uint32_t(0), args.policyFlags);
4160
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4168}
4169
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004170TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004171 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004172
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004173 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4174 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4175 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004176
Powei Fengd041c5d2019-05-03 17:11:33 -07004177 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004178 KeyboardInputMapper& mapper =
4179 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4180 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004181
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004183 NotifyKeyArgs args;
4184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4185 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4186
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004187 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4189 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4190
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4193 ASSERT_EQ(uint32_t(0), args.policyFlags);
4194
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004195 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4197 ASSERT_EQ(uint32_t(0), args.policyFlags);
4198
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4201 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4202
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004203 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4205 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4206}
4207
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208// --- CursorInputMapperTest ---
4209
4210class CursorInputMapperTest : public InputMapperTest {
4211protected:
4212 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4213
Michael Wright17db18e2020-06-26 20:51:44 +01004214 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
Chris Yea52ade12020-08-27 16:49:20 -07004216 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 InputMapperTest::SetUp();
4218
Michael Wright17db18e2020-06-26 20:51:44 +01004219 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004220 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221 }
4222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4224 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004225
4226 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004227 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4228 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4229 }
4230
4231 void prepareSecondaryDisplay() {
4232 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4233 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4234 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004235 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004236
4237 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4238 float pressure) {
4239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4240 0.0f, 0.0f, 0.0f, EPSILON));
4241 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242};
4243
4244const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4245
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004246void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4247 int32_t originalY, int32_t rotatedX,
4248 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 NotifyMotionArgs args;
4250
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004256 ASSERT_NO_FATAL_FAILURE(
4257 assertCursorPointerCoords(args.pointerCoords[0],
4258 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4259 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260}
4261
4262TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004264 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004266 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267}
4268
4269TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004271 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004273 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274}
4275
4276TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004278 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
4280 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004281 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
4283 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004284 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4285 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4287 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4288
4289 // When the bounds are set, then there should be a valid motion range.
4290 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4291
4292 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004293 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294
4295 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4296 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4297 1, 800 - 1, 0.0f, 0.0f));
4298 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4299 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4300 2, 480 - 1, 0.0f, 0.0f));
4301 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4302 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4303 0.0f, 1.0f, 0.0f, 0.0f));
4304}
4305
4306TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004308 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
4310 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004311 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312
4313 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4314 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4315 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4316 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4317 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4318 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4319 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4320 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4321 0.0f, 1.0f, 0.0f, 0.0f));
4322}
4323
4324TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004326 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327
arthurhungdcef2dc2020-08-11 14:47:50 +08004328 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329
4330 NotifyMotionArgs args;
4331
4332 // Button press.
4333 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4337 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4338 ASSERT_EQ(DEVICE_ID, args.deviceId);
4339 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4340 ASSERT_EQ(uint32_t(0), args.policyFlags);
4341 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4342 ASSERT_EQ(0, args.flags);
4343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4344 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4345 ASSERT_EQ(0, args.edgeFlags);
4346 ASSERT_EQ(uint32_t(1), args.pointerCount);
4347 ASSERT_EQ(0, args.pointerProperties[0].id);
4348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004349 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4351 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4352 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4353
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4355 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4356 ASSERT_EQ(DEVICE_ID, args.deviceId);
4357 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4358 ASSERT_EQ(uint32_t(0), args.policyFlags);
4359 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4360 ASSERT_EQ(0, args.flags);
4361 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4362 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4363 ASSERT_EQ(0, args.edgeFlags);
4364 ASSERT_EQ(uint32_t(1), args.pointerCount);
4365 ASSERT_EQ(0, args.pointerProperties[0].id);
4366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004367 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4369 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4370 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4371
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004373 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4374 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4376 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4377 ASSERT_EQ(DEVICE_ID, args.deviceId);
4378 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4379 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4381 ASSERT_EQ(0, args.flags);
4382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4383 ASSERT_EQ(0, args.buttonState);
4384 ASSERT_EQ(0, args.edgeFlags);
4385 ASSERT_EQ(uint32_t(1), args.pointerCount);
4386 ASSERT_EQ(0, args.pointerProperties[0].id);
4387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004388 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4390 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4391 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4392
4393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4394 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4395 ASSERT_EQ(DEVICE_ID, args.deviceId);
4396 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4397 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4399 ASSERT_EQ(0, args.flags);
4400 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4401 ASSERT_EQ(0, args.buttonState);
4402 ASSERT_EQ(0, args.edgeFlags);
4403 ASSERT_EQ(uint32_t(1), args.pointerCount);
4404 ASSERT_EQ(0, args.pointerProperties[0].id);
4405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004406 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4408 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4409 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4410}
4411
4412TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004414 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415
4416 NotifyMotionArgs args;
4417
4418 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004423 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4424 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4425 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426
4427 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004432 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4433 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434}
4435
4436TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004438 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
4440 NotifyMotionArgs args;
4441
4442 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004447 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4450 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004451 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004452
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004458 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459
4460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463}
4464
4465TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004467 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468
4469 NotifyMotionArgs args;
4470
4471 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4477 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004478 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4479 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4480 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004484 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4485 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4486 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004494 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4495 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4496 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497
4498 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004503 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004504
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004507 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508}
4509
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004510TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004511 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004513 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4514 // need to be rotated.
4515 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004516 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004518 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4527}
4528
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004529TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004530 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004532 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4533 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004534 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004536 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004537 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4539 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4540 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4541 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4542 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4543 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4544 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4545 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4546
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004547 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004548 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4553 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4554 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4555 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4556 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004558 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004559 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4561 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4564 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4565 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4566 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4567 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4568
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004569 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004570 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004571 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4575 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4576 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4577 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4578 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579}
4580
4581TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004583 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584
4585 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4586 mFakePointerController->setPosition(100, 200);
4587 mFakePointerController->setButtonState(0);
4588
4589 NotifyMotionArgs motionArgs;
4590 NotifyKeyArgs keyArgs;
4591
4592 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4596 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4597 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4598 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004599 ASSERT_NO_FATAL_FAILURE(
4600 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4604 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4605 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004606 ASSERT_NO_FATAL_FAILURE(
4607 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004608
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_EQ(0, motionArgs.buttonState);
4614 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004615 ASSERT_NO_FATAL_FAILURE(
4616 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617
4618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004619 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_EQ(0, motionArgs.buttonState);
4621 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004622 ASSERT_NO_FATAL_FAILURE(
4623 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004624
4625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627 ASSERT_EQ(0, motionArgs.buttonState);
4628 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004629 ASSERT_NO_FATAL_FAILURE(
4630 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631
4632 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4637 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4638 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4639 motionArgs.buttonState);
4640 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4641 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004642 ASSERT_NO_FATAL_FAILURE(
4643 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4647 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4648 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4649 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004650 ASSERT_NO_FATAL_FAILURE(
4651 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4655 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4656 motionArgs.buttonState);
4657 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4658 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004659 ASSERT_NO_FATAL_FAILURE(
4660 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4667 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(
4669 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004670
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004673 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4674 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004675 ASSERT_NO_FATAL_FAILURE(
4676 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4682 ASSERT_EQ(0, motionArgs.buttonState);
4683 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004684 ASSERT_NO_FATAL_FAILURE(
4685 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004688
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690 ASSERT_EQ(0, motionArgs.buttonState);
4691 ASSERT_EQ(0, mFakePointerController->getButtonState());
4692 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004693 ASSERT_NO_FATAL_FAILURE(
4694 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004695
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4697 ASSERT_EQ(0, motionArgs.buttonState);
4698 ASSERT_EQ(0, mFakePointerController->getButtonState());
4699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004700 ASSERT_NO_FATAL_FAILURE(
4701 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702
4703 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4707 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4708 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004709
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004711 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4713 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004714 ASSERT_NO_FATAL_FAILURE(
4715 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004716
4717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4719 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4720 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004721 ASSERT_NO_FATAL_FAILURE(
4722 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_EQ(0, motionArgs.buttonState);
4729 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004730 ASSERT_NO_FATAL_FAILURE(
4731 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004732
4733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004735 ASSERT_EQ(0, motionArgs.buttonState);
4736 ASSERT_EQ(0, mFakePointerController->getButtonState());
4737
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004738 ASSERT_NO_FATAL_FAILURE(
4739 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4741 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4742 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4743
4744 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4748 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4749 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004750
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004753 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4754 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004755 ASSERT_NO_FATAL_FAILURE(
4756 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004757
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4759 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4760 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4761 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004762 ASSERT_NO_FATAL_FAILURE(
4763 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004768 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 ASSERT_EQ(0, motionArgs.buttonState);
4770 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004771 ASSERT_NO_FATAL_FAILURE(
4772 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004773
4774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4776 ASSERT_EQ(0, motionArgs.buttonState);
4777 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004778 ASSERT_NO_FATAL_FAILURE(
4779 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004780
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4782 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4783 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4784
4785 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4789 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4790 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004791
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004793 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4795 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004796 ASSERT_NO_FATAL_FAILURE(
4797 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004798
4799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4801 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4802 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004803 ASSERT_NO_FATAL_FAILURE(
4804 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_EQ(0, motionArgs.buttonState);
4811 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004812 ASSERT_NO_FATAL_FAILURE(
4813 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4817 ASSERT_EQ(0, motionArgs.buttonState);
4818 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004819 ASSERT_NO_FATAL_FAILURE(
4820 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004821
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4823 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4824 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4825
4826 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4830 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4831 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004832
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4836 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004837 ASSERT_NO_FATAL_FAILURE(
4838 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004839
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4842 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4843 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004844 ASSERT_NO_FATAL_FAILURE(
4845 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004850 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 ASSERT_EQ(0, motionArgs.buttonState);
4852 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004853 ASSERT_NO_FATAL_FAILURE(
4854 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004855
4856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4857 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4858 ASSERT_EQ(0, motionArgs.buttonState);
4859 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004860 ASSERT_NO_FATAL_FAILURE(
4861 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004862
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4864 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4865 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4866}
4867
4868TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004870 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871
4872 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4873 mFakePointerController->setPosition(100, 200);
4874 mFakePointerController->setButtonState(0);
4875
4876 NotifyMotionArgs args;
4877
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004882 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4883 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4885 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004886 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004887}
4888
4889TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004890 addConfigurationProperty("cursor.mode", "pointer");
4891 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004892 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004893
4894 NotifyDeviceResetArgs resetArgs;
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4896 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4897 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4898
4899 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4900 mFakePointerController->setPosition(100, 200);
4901 mFakePointerController->setButtonState(0);
4902
4903 NotifyMotionArgs args;
4904
4905 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4910 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4913 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004914 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004915
4916 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4920 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4921 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4923 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4925 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4926 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4928 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4929
4930 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004931 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4932 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4934 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4937 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4939 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4940 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4942 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4943
4944 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4949 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4952 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004953 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004954
4955 // Disable pointer capture and check that the device generation got bumped
4956 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004957 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004958 mFakePolicy->setPointerCapture(false);
4959 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004960 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004961
4962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004963 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4964
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4969 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4972 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004973 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974}
4975
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004976/**
4977 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4978 * pointer acceleration or speed processing should not be applied.
4979 */
4980TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4981 addConfigurationProperty("cursor.mode", "pointer");
4982 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4983 100.f /*high threshold*/, 10.f /*acceleration*/);
4984 mFakePolicy->setVelocityControlParams(testParams);
4985 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4986
4987 NotifyDeviceResetArgs resetArgs;
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4989 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4990 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4991
4992 NotifyMotionArgs args;
4993
4994 // Move and verify scale is applied.
4995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4999 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5001 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5002 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5003 ASSERT_GT(relX, 10);
5004 ASSERT_GT(relY, 20);
5005
5006 // Enable Pointer Capture
5007 mFakePolicy->setPointerCapture(true);
5008 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5009 NotifyPointerCaptureChangedArgs captureArgs;
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5011 ASSERT_TRUE(captureArgs.request.enable);
5012
5013 // Move and verify scale is not applied.
5014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5018 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5020 ASSERT_EQ(10, args.pointerCoords[0].getX());
5021 ASSERT_EQ(20, args.pointerCoords[0].getY());
5022}
5023
Prabir Pradhan258e2b92022-06-24 18:37:04 +00005024TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5025 addConfigurationProperty("cursor.mode", "pointer");
5026 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5027
5028 NotifyDeviceResetArgs resetArgs;
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5030 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5031 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5032
5033 // Ensure the display is rotated.
5034 prepareDisplay(DISPLAY_ORIENTATION_90);
5035
5036 NotifyMotionArgs args;
5037
5038 // Verify that the coordinates are rotated.
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5043 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5044 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5045 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5046 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5047
5048 // Enable Pointer Capture.
5049 mFakePolicy->setPointerCapture(true);
5050 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5051 NotifyPointerCaptureChangedArgs captureArgs;
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5053 ASSERT_TRUE(captureArgs.request.enable);
5054
5055 // Move and verify rotation is not applied.
5056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5060 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5062 ASSERT_EQ(10, args.pointerCoords[0].getX());
5063 ASSERT_EQ(20, args.pointerCoords[0].getY());
5064}
5065
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005066TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005067 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005068
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005069 // Set up the default display.
5070 prepareDisplay(DISPLAY_ORIENTATION_90);
5071
5072 // Set up the secondary display as the display on which the pointer should be shown.
5073 // The InputDevice is not associated with any display.
5074 prepareSecondaryDisplay();
5075 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005076 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5077
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005078 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005079 mFakePointerController->setPosition(100, 200);
5080 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005081
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005082 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5087 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5088 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005089 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005090}
5091
5092TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5093 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5094
5095 // Set up the default display.
5096 prepareDisplay(DISPLAY_ORIENTATION_90);
5097
5098 // Set up the secondary display as the display on which the pointer should be shown,
5099 // and associate the InputDevice with the secondary display.
5100 prepareSecondaryDisplay();
5101 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5102 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5103 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5104
5105 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5106 mFakePointerController->setPosition(100, 200);
5107 mFakePointerController->setButtonState(0);
5108
5109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5113 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5114 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
5115 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5116}
5117
5118TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5119 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5120
5121 // Set up the default display as the display on which the pointer should be shown.
5122 prepareDisplay(DISPLAY_ORIENTATION_90);
5123 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5124
5125 // Associate the InputDevice with the secondary display.
5126 prepareSecondaryDisplay();
5127 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5128 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5129
5130 // The mapper should not generate any events because it is associated with a display that is
5131 // different from the pointer display.
5132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005136}
5137
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138// --- TouchInputMapperTest ---
5139
5140class TouchInputMapperTest : public InputMapperTest {
5141protected:
5142 static const int32_t RAW_X_MIN;
5143 static const int32_t RAW_X_MAX;
5144 static const int32_t RAW_Y_MIN;
5145 static const int32_t RAW_Y_MAX;
5146 static const int32_t RAW_TOUCH_MIN;
5147 static const int32_t RAW_TOUCH_MAX;
5148 static const int32_t RAW_TOOL_MIN;
5149 static const int32_t RAW_TOOL_MAX;
5150 static const int32_t RAW_PRESSURE_MIN;
5151 static const int32_t RAW_PRESSURE_MAX;
5152 static const int32_t RAW_ORIENTATION_MIN;
5153 static const int32_t RAW_ORIENTATION_MAX;
5154 static const int32_t RAW_DISTANCE_MIN;
5155 static const int32_t RAW_DISTANCE_MAX;
5156 static const int32_t RAW_TILT_MIN;
5157 static const int32_t RAW_TILT_MAX;
5158 static const int32_t RAW_ID_MIN;
5159 static const int32_t RAW_ID_MAX;
5160 static const int32_t RAW_SLOT_MIN;
5161 static const int32_t RAW_SLOT_MAX;
5162 static const float X_PRECISION;
5163 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005164 static const float X_PRECISION_VIRTUAL;
5165 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166
5167 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005168 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169
5170 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5171
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005172 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005173 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005174
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175 enum Axes {
5176 POSITION = 1 << 0,
5177 TOUCH = 1 << 1,
5178 TOOL = 1 << 2,
5179 PRESSURE = 1 << 3,
5180 ORIENTATION = 1 << 4,
5181 MINOR = 1 << 5,
5182 ID = 1 << 6,
5183 DISTANCE = 1 << 7,
5184 TILT = 1 << 8,
5185 SLOT = 1 << 9,
5186 TOOL_TYPE = 1 << 10,
5187 };
5188
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005189 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5190 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005191 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005193 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194 int32_t toRawX(float displayX);
5195 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005196 int32_t toRotatedRawX(float displayX);
5197 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005198 float toCookedX(float rawX, float rawY);
5199 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005201 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005202 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005203 float toDisplayY(int32_t rawY, int32_t displayHeight);
5204
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205};
5206
5207const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5208const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5209const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5210const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5211const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5212const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5213const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5214const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005215const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5216const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5218const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5219const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5220const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5221const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5222const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5223const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5224const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5225const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5226const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5227const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5228const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005229const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5230 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5231const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5232 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005233const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5234 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235
5236const float TouchInputMapperTest::GEOMETRIC_SCALE =
5237 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5238 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5239
5240const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5241 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5242 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5243};
5244
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005245void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005246 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5247 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005248}
5249
5250void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5251 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5252 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253}
5254
Santos Cordonfa5cf462017-04-05 10:37:00 -07005255void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005256 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5257 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5258 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005259}
5260
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005262 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5263 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5264 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5265 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266}
5267
Jason Gerecke489fda82012-09-07 17:19:40 -07005268void TouchInputMapperTest::prepareLocationCalibration() {
5269 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5270}
5271
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272int32_t TouchInputMapperTest::toRawX(float displayX) {
5273 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5274}
5275
5276int32_t TouchInputMapperTest::toRawY(float displayY) {
5277 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5278}
5279
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005280int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5281 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5282}
5283
5284int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5285 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5286}
5287
Jason Gerecke489fda82012-09-07 17:19:40 -07005288float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5289 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5290 return rawX;
5291}
5292
5293float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5294 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5295 return rawY;
5296}
5297
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005299 return toDisplayX(rawX, DISPLAY_WIDTH);
5300}
5301
5302float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5303 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304}
5305
5306float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005307 return toDisplayY(rawY, DISPLAY_HEIGHT);
5308}
5309
5310float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5311 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312}
5313
5314
5315// --- SingleTouchInputMapperTest ---
5316
5317class SingleTouchInputMapperTest : public TouchInputMapperTest {
5318protected:
5319 void prepareButtons();
5320 void prepareAxes(int axes);
5321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005322 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5323 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5324 void processUp(SingleTouchInputMapper& mappery);
5325 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5326 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5327 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5328 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5329 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5330 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331};
5332
5333void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005334 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335}
5336
5337void SingleTouchInputMapperTest::prepareAxes(int axes) {
5338 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005339 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5340 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341 }
5342 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005343 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5344 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345 }
5346 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005347 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5348 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349 }
5350 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005351 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5352 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353 }
5354 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005355 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5356 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357 }
5358}
5359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005360void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364}
5365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369}
5370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005371void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373}
5374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377}
5378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005379void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5380 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382}
5383
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386}
5387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005388void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5389 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392}
5393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005394void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5395 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397}
5398
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005399void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401}
5402
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 prepareButtons();
5405 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005406 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005408 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409}
5410
5411TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005412 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5413 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 prepareButtons();
5415 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005416 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005418 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419}
5420
5421TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 prepareButtons();
5423 prepareAxes(POSITION);
5424 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005425 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428}
5429
5430TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 prepareButtons();
5432 prepareAxes(POSITION);
5433 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005434 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005436 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437}
5438
5439TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 addConfigurationProperty("touch.deviceType", "touchScreen");
5441 prepareDisplay(DISPLAY_ORIENTATION_0);
5442 prepareButtons();
5443 prepareAxes(POSITION);
5444 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005445 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446
5447 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005448 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449
5450 // Virtual key is down.
5451 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5452 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5453 processDown(mapper, x, y);
5454 processSync(mapper);
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5456
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005457 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458
5459 // Virtual key is up.
5460 processUp(mapper);
5461 processSync(mapper);
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005464 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465}
5466
5467TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468 addConfigurationProperty("touch.deviceType", "touchScreen");
5469 prepareDisplay(DISPLAY_ORIENTATION_0);
5470 prepareButtons();
5471 prepareAxes(POSITION);
5472 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005473 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474
5475 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005476 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477
5478 // Virtual key is down.
5479 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5480 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5481 processDown(mapper, x, y);
5482 processSync(mapper);
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5484
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005485 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486
5487 // Virtual key is up.
5488 processUp(mapper);
5489 processSync(mapper);
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493}
5494
5495TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496 addConfigurationProperty("touch.deviceType", "touchScreen");
5497 prepareDisplay(DISPLAY_ORIENTATION_0);
5498 prepareButtons();
5499 prepareAxes(POSITION);
5500 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005501 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502
5503 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5504 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005505 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506 ASSERT_TRUE(flags[0]);
5507 ASSERT_FALSE(flags[1]);
5508}
5509
5510TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 addConfigurationProperty("touch.deviceType", "touchScreen");
5512 prepareDisplay(DISPLAY_ORIENTATION_0);
5513 prepareButtons();
5514 prepareAxes(POSITION);
5515 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005516 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517
arthurhungdcef2dc2020-08-11 14:47:50 +08005518 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519
5520 NotifyKeyArgs args;
5521
5522 // Press virtual key.
5523 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5524 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5525 processDown(mapper, x, y);
5526 processSync(mapper);
5527
5528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5529 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5530 ASSERT_EQ(DEVICE_ID, args.deviceId);
5531 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5532 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5533 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5534 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5535 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5536 ASSERT_EQ(KEY_HOME, args.scanCode);
5537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5538 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5539
5540 // Release virtual key.
5541 processUp(mapper);
5542 processSync(mapper);
5543
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5545 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5546 ASSERT_EQ(DEVICE_ID, args.deviceId);
5547 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5548 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5549 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5550 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5551 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5552 ASSERT_EQ(KEY_HOME, args.scanCode);
5553 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5554 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5555
5556 // Should not have sent any motions.
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5558}
5559
5560TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561 addConfigurationProperty("touch.deviceType", "touchScreen");
5562 prepareDisplay(DISPLAY_ORIENTATION_0);
5563 prepareButtons();
5564 prepareAxes(POSITION);
5565 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005566 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567
arthurhungdcef2dc2020-08-11 14:47:50 +08005568 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569
5570 NotifyKeyArgs keyArgs;
5571
5572 // Press virtual key.
5573 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5574 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5575 processDown(mapper, x, y);
5576 processSync(mapper);
5577
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5579 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5580 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5581 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5582 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5583 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5584 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5585 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5586 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5588 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5589
5590 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5591 // into the display area.
5592 y -= 100;
5593 processMove(mapper, x, y);
5594 processSync(mapper);
5595
5596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5597 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5598 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5599 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5600 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5601 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5602 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5603 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5604 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5605 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5607 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5608
5609 NotifyMotionArgs motionArgs;
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5612 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5613 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5614 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5616 ASSERT_EQ(0, motionArgs.flags);
5617 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5618 ASSERT_EQ(0, motionArgs.buttonState);
5619 ASSERT_EQ(0, motionArgs.edgeFlags);
5620 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5621 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5624 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5625 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5626 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5628
5629 // Keep moving out of bounds. Should generate a pointer move.
5630 y -= 50;
5631 processMove(mapper, x, y);
5632 processSync(mapper);
5633
5634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5635 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5636 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5637 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5638 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5640 ASSERT_EQ(0, motionArgs.flags);
5641 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5642 ASSERT_EQ(0, motionArgs.buttonState);
5643 ASSERT_EQ(0, motionArgs.edgeFlags);
5644 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5645 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5648 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5649 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5650 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5651 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5652
5653 // Release out of bounds. Should generate a pointer up.
5654 processUp(mapper);
5655 processSync(mapper);
5656
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5659 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5660 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5661 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5662 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5663 ASSERT_EQ(0, motionArgs.flags);
5664 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5665 ASSERT_EQ(0, motionArgs.buttonState);
5666 ASSERT_EQ(0, motionArgs.edgeFlags);
5667 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5668 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5671 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5672 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5673 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5674 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5675
5676 // Should not have sent any more keys or motions.
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5679}
5680
5681TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682 addConfigurationProperty("touch.deviceType", "touchScreen");
5683 prepareDisplay(DISPLAY_ORIENTATION_0);
5684 prepareButtons();
5685 prepareAxes(POSITION);
5686 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005687 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688
arthurhungdcef2dc2020-08-11 14:47:50 +08005689 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690
5691 NotifyMotionArgs motionArgs;
5692
5693 // Initially go down out of bounds.
5694 int32_t x = -10;
5695 int32_t y = -10;
5696 processDown(mapper, x, y);
5697 processSync(mapper);
5698
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5700
5701 // Move into the display area. Should generate a pointer down.
5702 x = 50;
5703 y = 75;
5704 processMove(mapper, x, y);
5705 processSync(mapper);
5706
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5709 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5710 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5711 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5713 ASSERT_EQ(0, motionArgs.flags);
5714 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5715 ASSERT_EQ(0, motionArgs.buttonState);
5716 ASSERT_EQ(0, motionArgs.edgeFlags);
5717 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5721 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5722 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5723 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5724 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5725
5726 // Release. Should generate a pointer up.
5727 processUp(mapper);
5728 processSync(mapper);
5729
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5732 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5733 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5734 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5735 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5736 ASSERT_EQ(0, motionArgs.flags);
5737 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5738 ASSERT_EQ(0, motionArgs.buttonState);
5739 ASSERT_EQ(0, motionArgs.edgeFlags);
5740 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5741 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5744 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5745 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5746 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5747 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5748
5749 // Should not have sent any more keys or motions.
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5752}
5753
Santos Cordonfa5cf462017-04-05 10:37:00 -07005754TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005755 addConfigurationProperty("touch.deviceType", "touchScreen");
5756 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5757
5758 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5759 prepareButtons();
5760 prepareAxes(POSITION);
5761 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005762 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005763
arthurhungdcef2dc2020-08-11 14:47:50 +08005764 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005765
5766 NotifyMotionArgs motionArgs;
5767
5768 // Down.
5769 int32_t x = 100;
5770 int32_t y = 125;
5771 processDown(mapper, x, y);
5772 processSync(mapper);
5773
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5775 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5776 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5777 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5778 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5779 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5780 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5781 ASSERT_EQ(0, motionArgs.flags);
5782 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5783 ASSERT_EQ(0, motionArgs.buttonState);
5784 ASSERT_EQ(0, motionArgs.edgeFlags);
5785 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5786 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5789 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5790 1, 0, 0, 0, 0, 0, 0, 0));
5791 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5792 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5793 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5794
5795 // Move.
5796 x += 50;
5797 y += 75;
5798 processMove(mapper, x, y);
5799 processSync(mapper);
5800
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5802 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5803 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5804 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5805 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5806 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5808 ASSERT_EQ(0, motionArgs.flags);
5809 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5810 ASSERT_EQ(0, motionArgs.buttonState);
5811 ASSERT_EQ(0, motionArgs.edgeFlags);
5812 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5813 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5816 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5817 1, 0, 0, 0, 0, 0, 0, 0));
5818 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5819 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5821
5822 // Up.
5823 processUp(mapper);
5824 processSync(mapper);
5825
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5827 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5828 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5829 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5830 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5831 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5832 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5833 ASSERT_EQ(0, motionArgs.flags);
5834 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5835 ASSERT_EQ(0, motionArgs.buttonState);
5836 ASSERT_EQ(0, motionArgs.edgeFlags);
5837 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5838 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5839 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5841 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5842 1, 0, 0, 0, 0, 0, 0, 0));
5843 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5844 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5846
5847 // Should not have sent any more keys or motions.
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5850}
5851
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853 addConfigurationProperty("touch.deviceType", "touchScreen");
5854 prepareDisplay(DISPLAY_ORIENTATION_0);
5855 prepareButtons();
5856 prepareAxes(POSITION);
5857 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005858 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859
arthurhungdcef2dc2020-08-11 14:47:50 +08005860 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861
5862 NotifyMotionArgs motionArgs;
5863
5864 // Down.
5865 int32_t x = 100;
5866 int32_t y = 125;
5867 processDown(mapper, x, y);
5868 processSync(mapper);
5869
5870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5871 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5872 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5873 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5874 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5875 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5876 ASSERT_EQ(0, motionArgs.flags);
5877 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5878 ASSERT_EQ(0, motionArgs.buttonState);
5879 ASSERT_EQ(0, motionArgs.edgeFlags);
5880 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5881 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5884 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5885 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5886 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5887 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5888
5889 // Move.
5890 x += 50;
5891 y += 75;
5892 processMove(mapper, x, y);
5893 processSync(mapper);
5894
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5896 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5897 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5898 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5899 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5901 ASSERT_EQ(0, motionArgs.flags);
5902 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5903 ASSERT_EQ(0, motionArgs.buttonState);
5904 ASSERT_EQ(0, motionArgs.edgeFlags);
5905 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5906 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5909 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5910 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5911 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5913
5914 // Up.
5915 processUp(mapper);
5916 processSync(mapper);
5917
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5920 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5921 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5922 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5923 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5924 ASSERT_EQ(0, motionArgs.flags);
5925 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5926 ASSERT_EQ(0, motionArgs.buttonState);
5927 ASSERT_EQ(0, motionArgs.edgeFlags);
5928 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5929 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5932 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5933 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5934 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5935 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5936
5937 // Should not have sent any more keys or motions.
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5940}
5941
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005942TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943 addConfigurationProperty("touch.deviceType", "touchScreen");
5944 prepareButtons();
5945 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005946 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5947 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005948 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949
5950 NotifyMotionArgs args;
5951
5952 // Rotation 90.
5953 prepareDisplay(DISPLAY_ORIENTATION_90);
5954 processDown(mapper, toRawX(50), toRawY(75));
5955 processSync(mapper);
5956
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5958 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5959 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5960
5961 processUp(mapper);
5962 processSync(mapper);
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5964}
5965
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005966TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 addConfigurationProperty("touch.deviceType", "touchScreen");
5968 prepareButtons();
5969 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005970 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5971 // orientation-aware are affected by display rotation.
5972 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005973 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974
5975 NotifyMotionArgs args;
5976
5977 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005978 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 prepareDisplay(DISPLAY_ORIENTATION_0);
5980 processDown(mapper, toRawX(50), toRawY(75));
5981 processSync(mapper);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5984 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5985 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5986
5987 processUp(mapper);
5988 processSync(mapper);
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5990
5991 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005992 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005994 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 processSync(mapper);
5996
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5998 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5999 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6000
6001 processUp(mapper);
6002 processSync(mapper);
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6004
6005 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006006 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007 prepareDisplay(DISPLAY_ORIENTATION_180);
6008 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6009 processSync(mapper);
6010
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6012 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6013 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6014
6015 processUp(mapper);
6016 processSync(mapper);
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6018
6019 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006020 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006021 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006022 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 processSync(mapper);
6024
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6026 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6027 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6028
6029 processUp(mapper);
6030 processSync(mapper);
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6032}
6033
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006034TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6035 addConfigurationProperty("touch.deviceType", "touchScreen");
6036 prepareButtons();
6037 prepareAxes(POSITION);
6038 addConfigurationProperty("touch.orientationAware", "1");
6039 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6040 clearViewports();
6041 prepareDisplay(DISPLAY_ORIENTATION_0);
6042 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6043 NotifyMotionArgs args;
6044
6045 // Orientation 0.
6046 processDown(mapper, toRawX(50), toRawY(75));
6047 processSync(mapper);
6048
6049 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6050 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6051 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6052
6053 processUp(mapper);
6054 processSync(mapper);
6055 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6056}
6057
6058TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6059 addConfigurationProperty("touch.deviceType", "touchScreen");
6060 prepareButtons();
6061 prepareAxes(POSITION);
6062 addConfigurationProperty("touch.orientationAware", "1");
6063 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6064 clearViewports();
6065 prepareDisplay(DISPLAY_ORIENTATION_0);
6066 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6067 NotifyMotionArgs args;
6068
6069 // Orientation 90.
6070 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6071 processSync(mapper);
6072
6073 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6074 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6075 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6076
6077 processUp(mapper);
6078 processSync(mapper);
6079 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6080}
6081
6082TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6083 addConfigurationProperty("touch.deviceType", "touchScreen");
6084 prepareButtons();
6085 prepareAxes(POSITION);
6086 addConfigurationProperty("touch.orientationAware", "1");
6087 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6088 clearViewports();
6089 prepareDisplay(DISPLAY_ORIENTATION_0);
6090 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6091 NotifyMotionArgs args;
6092
6093 // Orientation 180.
6094 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6095 processSync(mapper);
6096
6097 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6098 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6099 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6100
6101 processUp(mapper);
6102 processSync(mapper);
6103 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6104}
6105
6106TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6107 addConfigurationProperty("touch.deviceType", "touchScreen");
6108 prepareButtons();
6109 prepareAxes(POSITION);
6110 addConfigurationProperty("touch.orientationAware", "1");
6111 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6112 clearViewports();
6113 prepareDisplay(DISPLAY_ORIENTATION_0);
6114 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6115 NotifyMotionArgs args;
6116
6117 // Orientation 270.
6118 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6119 processSync(mapper);
6120
6121 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6122 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6123 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6124
6125 processUp(mapper);
6126 processSync(mapper);
6127 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6128}
6129
6130TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6131 addConfigurationProperty("touch.deviceType", "touchScreen");
6132 prepareButtons();
6133 prepareAxes(POSITION);
6134 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6135 // orientation-aware are affected by display rotation.
6136 addConfigurationProperty("touch.orientationAware", "0");
6137 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6138 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6139
6140 NotifyMotionArgs args;
6141
6142 // Orientation 90, Rotation 0.
6143 clearViewports();
6144 prepareDisplay(DISPLAY_ORIENTATION_0);
6145 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6146 processSync(mapper);
6147
6148 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6149 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6150 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6151
6152 processUp(mapper);
6153 processSync(mapper);
6154 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6155
6156 // Orientation 90, Rotation 90.
6157 clearViewports();
6158 prepareDisplay(DISPLAY_ORIENTATION_90);
6159 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6160 processSync(mapper);
6161
6162 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6163 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6164 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6165
6166 processUp(mapper);
6167 processSync(mapper);
6168 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6169
6170 // Orientation 90, Rotation 180.
6171 clearViewports();
6172 prepareDisplay(DISPLAY_ORIENTATION_180);
6173 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6174 processSync(mapper);
6175
6176 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6177 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6178 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6179
6180 processUp(mapper);
6181 processSync(mapper);
6182 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6183
6184 // Orientation 90, Rotation 270.
6185 clearViewports();
6186 prepareDisplay(DISPLAY_ORIENTATION_270);
6187 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6188 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6189 processSync(mapper);
6190
6191 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6192 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6193 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6194
6195 processUp(mapper);
6196 processSync(mapper);
6197 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6198}
6199
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201 addConfigurationProperty("touch.deviceType", "touchScreen");
6202 prepareDisplay(DISPLAY_ORIENTATION_0);
6203 prepareButtons();
6204 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006205 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206
6207 // These calculations are based on the input device calibration documentation.
6208 int32_t rawX = 100;
6209 int32_t rawY = 200;
6210 int32_t rawPressure = 10;
6211 int32_t rawToolMajor = 12;
6212 int32_t rawDistance = 2;
6213 int32_t rawTiltX = 30;
6214 int32_t rawTiltY = 110;
6215
6216 float x = toDisplayX(rawX);
6217 float y = toDisplayY(rawY);
6218 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6219 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6220 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6221 float distance = float(rawDistance);
6222
6223 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6224 float tiltScale = M_PI / 180;
6225 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6226 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6227 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6228 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6229
6230 processDown(mapper, rawX, rawY);
6231 processPressure(mapper, rawPressure);
6232 processToolMajor(mapper, rawToolMajor);
6233 processDistance(mapper, rawDistance);
6234 processTilt(mapper, rawTiltX, rawTiltY);
6235 processSync(mapper);
6236
6237 NotifyMotionArgs args;
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6240 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6241 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6242}
6243
Jason Gerecke489fda82012-09-07 17:19:40 -07006244TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006245 addConfigurationProperty("touch.deviceType", "touchScreen");
6246 prepareDisplay(DISPLAY_ORIENTATION_0);
6247 prepareLocationCalibration();
6248 prepareButtons();
6249 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006250 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006251
6252 int32_t rawX = 100;
6253 int32_t rawY = 200;
6254
6255 float x = toDisplayX(toCookedX(rawX, rawY));
6256 float y = toDisplayY(toCookedY(rawX, rawY));
6257
6258 processDown(mapper, rawX, rawY);
6259 processSync(mapper);
6260
6261 NotifyMotionArgs args;
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6264 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6265}
6266
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006268 addConfigurationProperty("touch.deviceType", "touchScreen");
6269 prepareDisplay(DISPLAY_ORIENTATION_0);
6270 prepareButtons();
6271 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006272 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 NotifyMotionArgs motionArgs;
6275 NotifyKeyArgs keyArgs;
6276
6277 processDown(mapper, 100, 200);
6278 processSync(mapper);
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6281 ASSERT_EQ(0, motionArgs.buttonState);
6282
6283 // press BTN_LEFT, release BTN_LEFT
6284 processKey(mapper, BTN_LEFT, 1);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6288 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6289
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6292 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6293
Michael Wrightd02c5b62014-02-10 15:10:22 -08006294 processKey(mapper, BTN_LEFT, 0);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303
6304 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6305 processKey(mapper, BTN_RIGHT, 1);
6306 processKey(mapper, BTN_MIDDLE, 1);
6307 processSync(mapper);
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6310 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6311 motionArgs.buttonState);
6312
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6315 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6316
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6319 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6320 motionArgs.buttonState);
6321
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322 processKey(mapper, BTN_RIGHT, 0);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331
6332 processKey(mapper, BTN_MIDDLE, 0);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006340 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341
6342 // press BTN_BACK, release BTN_BACK
6343 processKey(mapper, BTN_BACK, 1);
6344 processSync(mapper);
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6346 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6347 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006348
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006351 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6352
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6355 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356
6357 processKey(mapper, BTN_BACK, 0);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006365 ASSERT_EQ(0, motionArgs.buttonState);
6366
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6368 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6369 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6370
6371 // press BTN_SIDE, release BTN_SIDE
6372 processKey(mapper, BTN_SIDE, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6375 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6376 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006377
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006380 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6381
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6384 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385
6386 processKey(mapper, BTN_SIDE, 0);
6387 processSync(mapper);
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006394 ASSERT_EQ(0, motionArgs.buttonState);
6395
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6397 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6398 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6399
6400 // press BTN_FORWARD, release BTN_FORWARD
6401 processKey(mapper, BTN_FORWARD, 1);
6402 processSync(mapper);
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6404 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6405 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006409 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6410
6411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6413 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414
6415 processKey(mapper, BTN_FORWARD, 0);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006420
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006423 ASSERT_EQ(0, motionArgs.buttonState);
6424
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6426 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6427 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6428
6429 // press BTN_EXTRA, release BTN_EXTRA
6430 processKey(mapper, BTN_EXTRA, 1);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6433 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6434 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006435
Michael Wrightd02c5b62014-02-10 15:10:22 -08006436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6439
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6442 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443
6444 processKey(mapper, BTN_EXTRA, 0);
6445 processSync(mapper);
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006448 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006449
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006452 ASSERT_EQ(0, motionArgs.buttonState);
6453
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6455 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6456 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6457
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6459
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 // press BTN_STYLUS, release BTN_STYLUS
6461 processKey(mapper, BTN_STYLUS, 1);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006465 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6466
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6469 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006470
6471 processKey(mapper, BTN_STYLUS, 0);
6472 processSync(mapper);
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006476
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480
6481 // press BTN_STYLUS2, release BTN_STYLUS2
6482 processKey(mapper, BTN_STYLUS2, 1);
6483 processSync(mapper);
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006486 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6487
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6490 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006491
6492 processKey(mapper, BTN_STYLUS2, 0);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006497
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006500 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501
6502 // release touch
6503 processUp(mapper);
6504 processSync(mapper);
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6507 ASSERT_EQ(0, motionArgs.buttonState);
6508}
6509
6510TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 addConfigurationProperty("touch.deviceType", "touchScreen");
6512 prepareDisplay(DISPLAY_ORIENTATION_0);
6513 prepareButtons();
6514 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006515 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516
6517 NotifyMotionArgs motionArgs;
6518
6519 // default tool type is finger
6520 processDown(mapper, 100, 200);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6525
6526 // eraser
6527 processKey(mapper, BTN_TOOL_RUBBER, 1);
6528 processSync(mapper);
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6532
6533 // stylus
6534 processKey(mapper, BTN_TOOL_RUBBER, 0);
6535 processKey(mapper, BTN_TOOL_PEN, 1);
6536 processSync(mapper);
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6540
6541 // brush
6542 processKey(mapper, BTN_TOOL_PEN, 0);
6543 processKey(mapper, BTN_TOOL_BRUSH, 1);
6544 processSync(mapper);
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6548
6549 // pencil
6550 processKey(mapper, BTN_TOOL_BRUSH, 0);
6551 processKey(mapper, BTN_TOOL_PENCIL, 1);
6552 processSync(mapper);
6553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6556
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006557 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006558 processKey(mapper, BTN_TOOL_PENCIL, 0);
6559 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6560 processSync(mapper);
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6564
6565 // mouse
6566 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6567 processKey(mapper, BTN_TOOL_MOUSE, 1);
6568 processSync(mapper);
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6572
6573 // lens
6574 processKey(mapper, BTN_TOOL_MOUSE, 0);
6575 processKey(mapper, BTN_TOOL_LENS, 1);
6576 processSync(mapper);
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6580
6581 // double-tap
6582 processKey(mapper, BTN_TOOL_LENS, 0);
6583 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6588
6589 // triple-tap
6590 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6591 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6596
6597 // quad-tap
6598 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6599 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6600 processSync(mapper);
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6604
6605 // finger
6606 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6607 processKey(mapper, BTN_TOOL_FINGER, 1);
6608 processSync(mapper);
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6612
6613 // stylus trumps finger
6614 processKey(mapper, BTN_TOOL_PEN, 1);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6619
6620 // eraser trumps stylus
6621 processKey(mapper, BTN_TOOL_RUBBER, 1);
6622 processSync(mapper);
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6626
6627 // mouse trumps eraser
6628 processKey(mapper, BTN_TOOL_MOUSE, 1);
6629 processSync(mapper);
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6633
6634 // back to default tool type
6635 processKey(mapper, BTN_TOOL_MOUSE, 0);
6636 processKey(mapper, BTN_TOOL_RUBBER, 0);
6637 processKey(mapper, BTN_TOOL_PEN, 0);
6638 processKey(mapper, BTN_TOOL_FINGER, 0);
6639 processSync(mapper);
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6643}
6644
6645TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006646 addConfigurationProperty("touch.deviceType", "touchScreen");
6647 prepareDisplay(DISPLAY_ORIENTATION_0);
6648 prepareButtons();
6649 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006650 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006651 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006652
6653 NotifyMotionArgs motionArgs;
6654
6655 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6656 processKey(mapper, BTN_TOOL_FINGER, 1);
6657 processMove(mapper, 100, 200);
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6662 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6663
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6667 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6668
6669 // move a little
6670 processMove(mapper, 150, 250);
6671 processSync(mapper);
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6675 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6676
6677 // down when BTN_TOUCH is pressed, pressure defaults to 1
6678 processKey(mapper, BTN_TOUCH, 1);
6679 processSync(mapper);
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6683 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6684
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6688 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6689
6690 // up when BTN_TOUCH is released, hover restored
6691 processKey(mapper, BTN_TOUCH, 0);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6697
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6701 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6702
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6704 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6706 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6707
6708 // exit hover when pointer goes away
6709 processKey(mapper, BTN_TOOL_FINGER, 0);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6712 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6714 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6715}
6716
6717TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006718 addConfigurationProperty("touch.deviceType", "touchScreen");
6719 prepareDisplay(DISPLAY_ORIENTATION_0);
6720 prepareButtons();
6721 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006722 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006723
6724 NotifyMotionArgs motionArgs;
6725
6726 // initially hovering because pressure is 0
6727 processDown(mapper, 100, 200);
6728 processPressure(mapper, 0);
6729 processSync(mapper);
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6733 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6734
6735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6736 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6738 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6739
6740 // move a little
6741 processMove(mapper, 150, 250);
6742 processSync(mapper);
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6744 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6746 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6747
6748 // down when pressure is non-zero
6749 processPressure(mapper, RAW_PRESSURE_MAX);
6750 processSync(mapper);
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6754 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6755
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6759 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6760
6761 // up when pressure becomes 0, hover restored
6762 processPressure(mapper, 0);
6763 processSync(mapper);
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6765 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6767 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6768
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6772 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6773
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6777 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6778
6779 // exit hover when pointer goes away
6780 processUp(mapper);
6781 processSync(mapper);
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6785 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6786}
6787
lilinnane74b35f2022-07-19 16:00:50 +08006788TEST_F(SingleTouchInputMapperTest,
6789 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6790 addConfigurationProperty("touch.deviceType", "touchScreen");
6791 prepareDisplay(DISPLAY_ORIENTATION_0);
6792 prepareButtons();
6793 prepareAxes(POSITION);
6794 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6795 NotifyMotionArgs motionArgs;
6796
6797 // Down.
6798 processDown(mapper, 100, 200);
6799 processSync(mapper);
6800
6801 // We should receive a down event
6802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6803 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6804
6805 // Change display id
6806 clearViewports();
6807 prepareSecondaryDisplay(ViewportType::INTERNAL);
6808
6809 // We should receive a cancel event
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6811 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6812 // Then receive reset called
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6814}
6815
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006816TEST_F(SingleTouchInputMapperTest,
6817 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6818 addConfigurationProperty("touch.deviceType", "touchScreen");
6819 prepareDisplay(DISPLAY_ORIENTATION_0);
6820 prepareButtons();
6821 prepareAxes(POSITION);
6822 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6824 NotifyMotionArgs motionArgs;
6825
6826 // Start a new gesture.
6827 processDown(mapper, 100, 200);
6828 processSync(mapper);
6829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6830 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6831
6832 // Make the viewport inactive. This will put the device in disabled mode.
6833 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6834 viewport->isActive = false;
6835 mFakePolicy->updateViewport(*viewport);
6836 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6837
6838 // We should receive a cancel event for the ongoing gesture.
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6840 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6841 // Then we should be notified that the device was reset.
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6843
6844 // No events are generated while the viewport is inactive.
6845 processMove(mapper, 101, 201);
6846 processSync(mapper);
6847 processDown(mapper, 102, 202);
6848 processSync(mapper);
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6850
6851 // Make the viewport active again. The device should resume processing events.
6852 viewport->isActive = true;
6853 mFakePolicy->updateViewport(*viewport);
6854 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6855
6856 // The device is reset because it changes back to direct mode, without generating any events.
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6859
6860 // Start a new gesture.
6861 processDown(mapper, 100, 200);
6862 processSync(mapper);
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6864 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6865
6866 // No more events.
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6869}
6870
Prabir Pradhan5632d622021-09-06 07:57:20 -07006871// --- TouchDisplayProjectionTest ---
6872
6873class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6874public:
6875 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6876 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6877 // rotated equivalent of the given un-rotated physical display bounds.
6878 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6879 uint32_t inverseRotationFlags;
6880 auto width = DISPLAY_WIDTH;
6881 auto height = DISPLAY_HEIGHT;
6882 switch (orientation) {
6883 case DISPLAY_ORIENTATION_90:
6884 inverseRotationFlags = ui::Transform::ROT_270;
6885 std::swap(width, height);
6886 break;
6887 case DISPLAY_ORIENTATION_180:
6888 inverseRotationFlags = ui::Transform::ROT_180;
6889 break;
6890 case DISPLAY_ORIENTATION_270:
6891 inverseRotationFlags = ui::Transform::ROT_90;
6892 std::swap(width, height);
6893 break;
6894 case DISPLAY_ORIENTATION_0:
6895 inverseRotationFlags = ui::Transform::ROT_0;
6896 break;
6897 default:
6898 FAIL() << "Invalid orientation: " << orientation;
6899 }
6900
6901 const ui::Transform rotation(inverseRotationFlags, width, height);
6902 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6903
6904 std::optional<DisplayViewport> internalViewport =
6905 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6906 DisplayViewport& v = *internalViewport;
6907 v.displayId = DISPLAY_ID;
6908 v.orientation = orientation;
6909
6910 v.logicalLeft = 0;
6911 v.logicalTop = 0;
6912 v.logicalRight = 100;
6913 v.logicalBottom = 100;
6914
6915 v.physicalLeft = rotatedPhysicalDisplay.left;
6916 v.physicalTop = rotatedPhysicalDisplay.top;
6917 v.physicalRight = rotatedPhysicalDisplay.right;
6918 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6919
6920 v.deviceWidth = width;
6921 v.deviceHeight = height;
6922
6923 v.isActive = true;
6924 v.uniqueId = UNIQUE_ID;
6925 v.type = ViewportType::INTERNAL;
6926 mFakePolicy->updateViewport(v);
6927 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6928 }
6929
6930 void assertReceivedMove(const Point& point) {
6931 NotifyMotionArgs motionArgs;
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6933 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6934 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6936 1, 0, 0, 0, 0, 0, 0, 0));
6937 }
6938};
6939
6940TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6941 addConfigurationProperty("touch.deviceType", "touchScreen");
6942 prepareDisplay(DISPLAY_ORIENTATION_0);
6943
6944 prepareButtons();
6945 prepareAxes(POSITION);
6946 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6947
6948 NotifyMotionArgs motionArgs;
6949
6950 // Configure the DisplayViewport such that the logical display maps to a subsection of
6951 // the display panel called the physical display. Here, the physical display is bounded by the
6952 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6953 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6954 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6955 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6956
6957 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6958 DISPLAY_ORIENTATION_270}) {
6959 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6960
6961 // Touches outside the physical display should be ignored, and should not generate any
6962 // events. Ensure touches at the following points that lie outside of the physical display
6963 // area do not generate any events.
6964 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6965 processDown(mapper, toRawX(point.x), toRawY(point.y));
6966 processSync(mapper);
6967 processUp(mapper);
6968 processSync(mapper);
6969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6970 << "Unexpected event generated for touch outside physical display at point: "
6971 << point.x << ", " << point.y;
6972 }
6973 }
6974}
6975
6976TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6977 addConfigurationProperty("touch.deviceType", "touchScreen");
6978 prepareDisplay(DISPLAY_ORIENTATION_0);
6979
6980 prepareButtons();
6981 prepareAxes(POSITION);
6982 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6983
6984 NotifyMotionArgs motionArgs;
6985
6986 // Configure the DisplayViewport such that the logical display maps to a subsection of
6987 // the display panel called the physical display. Here, the physical display is bounded by the
6988 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6989 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6990
6991 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6992 DISPLAY_ORIENTATION_270}) {
6993 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6994
6995 // Touches that start outside the physical display should be ignored until it enters the
6996 // physical display bounds, at which point it should generate a down event. Start a touch at
6997 // the point (5, 100), which is outside the physical display bounds.
6998 static const Point kOutsidePoint{5, 100};
6999 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7000 processSync(mapper);
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7002
7003 // Move the touch into the physical display area. This should generate a pointer down.
7004 processMove(mapper, toRawX(11), toRawY(21));
7005 processSync(mapper);
7006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7007 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7008 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7009 ASSERT_NO_FATAL_FAILURE(
7010 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7011
7012 // Move the touch inside the physical display area. This should generate a pointer move.
7013 processMove(mapper, toRawX(69), toRawY(159));
7014 processSync(mapper);
7015 assertReceivedMove({69, 159});
7016
7017 // Move outside the physical display area. Since the pointer is already down, this should
7018 // now continue generating events.
7019 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7020 processSync(mapper);
7021 assertReceivedMove(kOutsidePoint);
7022
7023 // Release. This should generate a pointer up.
7024 processUp(mapper);
7025 processSync(mapper);
7026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7027 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7029 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7030
7031 // Ensure no more events were generated.
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7034 }
7035}
7036
Michael Wrightd02c5b62014-02-10 15:10:22 -08007037// --- MultiTouchInputMapperTest ---
7038
7039class MultiTouchInputMapperTest : public TouchInputMapperTest {
7040protected:
7041 void prepareAxes(int axes);
7042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007043 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7044 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7045 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7046 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7047 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7048 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7049 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7050 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7051 void processId(MultiTouchInputMapper& mapper, int32_t id);
7052 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7053 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7054 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7055 void processMTSync(MultiTouchInputMapper& mapper);
7056 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057};
7058
7059void MultiTouchInputMapperTest::prepareAxes(int axes) {
7060 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007061 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7062 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007063 }
7064 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007065 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7066 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007068 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7069 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070 }
7071 }
7072 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007073 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7074 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007076 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007077 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007078 }
7079 }
7080 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007081 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7082 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007083 }
7084 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007085 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7086 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007087 }
7088 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007089 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7090 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091 }
7092 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007093 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7094 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007095 }
7096 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7098 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 }
7100 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007101 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102 }
7103}
7104
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007105void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7106 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109}
7110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007111void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7112 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114}
7115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007116void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7117 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007119}
7120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007121void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123}
7124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007125void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127}
7128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007129void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7130 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132}
7133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007134void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007136}
7137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007138void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140}
7141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007142void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144}
7145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007146void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007148}
7149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007150void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152}
7153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007154void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7155 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157}
7158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007159void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161}
7162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007163void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165}
7166
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168 addConfigurationProperty("touch.deviceType", "touchScreen");
7169 prepareDisplay(DISPLAY_ORIENTATION_0);
7170 prepareAxes(POSITION);
7171 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007172 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173
arthurhungdcef2dc2020-08-11 14:47:50 +08007174 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175
7176 NotifyMotionArgs motionArgs;
7177
7178 // Two fingers down at once.
7179 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7180 processPosition(mapper, x1, y1);
7181 processMTSync(mapper);
7182 processPosition(mapper, x2, y2);
7183 processMTSync(mapper);
7184 processSync(mapper);
7185
7186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7187 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7188 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7189 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7190 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7192 ASSERT_EQ(0, motionArgs.flags);
7193 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7194 ASSERT_EQ(0, motionArgs.buttonState);
7195 ASSERT_EQ(0, motionArgs.edgeFlags);
7196 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7197 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7200 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7201 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7202 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7203 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7204
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7207 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7208 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7209 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007210 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211 ASSERT_EQ(0, motionArgs.flags);
7212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7213 ASSERT_EQ(0, motionArgs.buttonState);
7214 ASSERT_EQ(0, motionArgs.edgeFlags);
7215 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7216 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7218 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7221 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7223 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7224 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7225 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7226 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7227
7228 // Move.
7229 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7230 processPosition(mapper, x1, y1);
7231 processMTSync(mapper);
7232 processPosition(mapper, x2, y2);
7233 processMTSync(mapper);
7234 processSync(mapper);
7235
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7238 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7239 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7240 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7242 ASSERT_EQ(0, motionArgs.flags);
7243 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7244 ASSERT_EQ(0, motionArgs.buttonState);
7245 ASSERT_EQ(0, motionArgs.edgeFlags);
7246 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7247 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7249 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7252 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7254 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7255 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7256 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7257 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7258
7259 // First finger up.
7260 x2 += 15; y2 -= 20;
7261 processPosition(mapper, x2, y2);
7262 processMTSync(mapper);
7263 processSync(mapper);
7264
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7266 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7267 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7268 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7269 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007270 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007271 ASSERT_EQ(0, motionArgs.flags);
7272 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7273 ASSERT_EQ(0, motionArgs.buttonState);
7274 ASSERT_EQ(0, motionArgs.edgeFlags);
7275 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7276 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7278 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7279 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7281 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7283 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7284 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7285 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7287
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7290 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7291 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7292 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7294 ASSERT_EQ(0, motionArgs.flags);
7295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7296 ASSERT_EQ(0, motionArgs.buttonState);
7297 ASSERT_EQ(0, motionArgs.edgeFlags);
7298 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7299 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7302 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7303 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7304 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7305 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7306
7307 // Move.
7308 x2 += 20; y2 -= 25;
7309 processPosition(mapper, x2, y2);
7310 processMTSync(mapper);
7311 processSync(mapper);
7312
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7314 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7315 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7316 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7317 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7319 ASSERT_EQ(0, motionArgs.flags);
7320 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7321 ASSERT_EQ(0, motionArgs.buttonState);
7322 ASSERT_EQ(0, motionArgs.edgeFlags);
7323 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7324 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7327 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7328 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7329 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7330 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7331
7332 // New finger down.
7333 int32_t x3 = 700, y3 = 300;
7334 processPosition(mapper, x2, y2);
7335 processMTSync(mapper);
7336 processPosition(mapper, x3, y3);
7337 processMTSync(mapper);
7338 processSync(mapper);
7339
7340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7342 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7343 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7344 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007345 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007346 ASSERT_EQ(0, motionArgs.flags);
7347 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7348 ASSERT_EQ(0, motionArgs.buttonState);
7349 ASSERT_EQ(0, motionArgs.edgeFlags);
7350 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7351 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7353 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7356 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7358 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7359 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7360 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7362
7363 // Second finger up.
7364 x3 += 30; y3 -= 20;
7365 processPosition(mapper, x3, y3);
7366 processMTSync(mapper);
7367 processSync(mapper);
7368
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7371 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7372 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7373 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007374 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007375 ASSERT_EQ(0, motionArgs.flags);
7376 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7377 ASSERT_EQ(0, motionArgs.buttonState);
7378 ASSERT_EQ(0, motionArgs.edgeFlags);
7379 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7380 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7382 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7385 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7387 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7388 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7389 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7390 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7391
7392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7394 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7395 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7396 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7398 ASSERT_EQ(0, motionArgs.flags);
7399 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7400 ASSERT_EQ(0, motionArgs.buttonState);
7401 ASSERT_EQ(0, motionArgs.edgeFlags);
7402 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7403 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7406 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7407 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7408 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7410
7411 // Last finger up.
7412 processMTSync(mapper);
7413 processSync(mapper);
7414
7415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7416 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7417 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7418 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7419 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7420 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7421 ASSERT_EQ(0, motionArgs.flags);
7422 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7423 ASSERT_EQ(0, motionArgs.buttonState);
7424 ASSERT_EQ(0, motionArgs.edgeFlags);
7425 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7426 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7429 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7430 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7431 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7432 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7433
7434 // Should not have sent any more keys or motions.
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7437}
7438
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007439TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7440 addConfigurationProperty("touch.deviceType", "touchScreen");
7441 prepareDisplay(DISPLAY_ORIENTATION_0);
7442
7443 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7444 /*fuzz*/ 0, /*resolution*/ 10);
7445 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7446 /*fuzz*/ 0, /*resolution*/ 11);
7447 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7448 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7449 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7450 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7451 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7452 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7453 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7454 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7455
7456 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7457
7458 // X and Y axes
7459 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7460 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7461 // Touch major and minor
7462 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7463 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7464 // Tool major and minor
7465 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7466 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7467}
7468
7469TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7470 addConfigurationProperty("touch.deviceType", "touchScreen");
7471 prepareDisplay(DISPLAY_ORIENTATION_0);
7472
7473 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7474 /*fuzz*/ 0, /*resolution*/ 10);
7475 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7476 /*fuzz*/ 0, /*resolution*/ 11);
7477
7478 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7479
7480 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7481
7482 // Touch major and minor
7483 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7484 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7485 // Tool major and minor
7486 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7487 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7488}
7489
Michael Wrightd02c5b62014-02-10 15:10:22 -08007490TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007491 addConfigurationProperty("touch.deviceType", "touchScreen");
7492 prepareDisplay(DISPLAY_ORIENTATION_0);
7493 prepareAxes(POSITION | ID);
7494 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007495 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007496
arthurhungdcef2dc2020-08-11 14:47:50 +08007497 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007498
7499 NotifyMotionArgs motionArgs;
7500
7501 // Two fingers down at once.
7502 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7503 processPosition(mapper, x1, y1);
7504 processId(mapper, 1);
7505 processMTSync(mapper);
7506 processPosition(mapper, x2, y2);
7507 processId(mapper, 2);
7508 processMTSync(mapper);
7509 processSync(mapper);
7510
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7512 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7513 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7517 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7518
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007520 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007521 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7522 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7524 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7527 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7529 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7530
7531 // Move.
7532 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7533 processPosition(mapper, x1, y1);
7534 processId(mapper, 1);
7535 processMTSync(mapper);
7536 processPosition(mapper, x2, y2);
7537 processId(mapper, 2);
7538 processMTSync(mapper);
7539 processSync(mapper);
7540
7541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7543 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7544 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7546 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7549 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7551 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7552
7553 // First finger up.
7554 x2 += 15; y2 -= 20;
7555 processPosition(mapper, x2, y2);
7556 processId(mapper, 2);
7557 processMTSync(mapper);
7558 processSync(mapper);
7559
7560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007561 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007562 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7563 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7565 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7568 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7570 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7571
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7574 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7575 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7578 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7579
7580 // Move.
7581 x2 += 20; y2 -= 25;
7582 processPosition(mapper, x2, y2);
7583 processId(mapper, 2);
7584 processMTSync(mapper);
7585 processSync(mapper);
7586
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7589 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7590 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7593 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7594
7595 // New finger down.
7596 int32_t x3 = 700, y3 = 300;
7597 processPosition(mapper, x2, y2);
7598 processId(mapper, 2);
7599 processMTSync(mapper);
7600 processPosition(mapper, x3, y3);
7601 processId(mapper, 3);
7602 processMTSync(mapper);
7603 processSync(mapper);
7604
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007606 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7608 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7610 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7613 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7615 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7616
7617 // Second finger up.
7618 x3 += 30; y3 -= 20;
7619 processPosition(mapper, x3, y3);
7620 processId(mapper, 3);
7621 processMTSync(mapper);
7622 processSync(mapper);
7623
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007625 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7629 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7632 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7634 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7635
7636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7638 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7639 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7642 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7643
7644 // Last finger up.
7645 processMTSync(mapper);
7646 processSync(mapper);
7647
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7649 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7650 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7654 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7655
7656 // Should not have sent any more keys or motions.
7657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7659}
7660
7661TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007662 addConfigurationProperty("touch.deviceType", "touchScreen");
7663 prepareDisplay(DISPLAY_ORIENTATION_0);
7664 prepareAxes(POSITION | ID | SLOT);
7665 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007666 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667
arthurhungdcef2dc2020-08-11 14:47:50 +08007668 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007669
7670 NotifyMotionArgs motionArgs;
7671
7672 // Two fingers down at once.
7673 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7674 processPosition(mapper, x1, y1);
7675 processId(mapper, 1);
7676 processSlot(mapper, 1);
7677 processPosition(mapper, x2, y2);
7678 processId(mapper, 2);
7679 processSync(mapper);
7680
7681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7682 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7683 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7684 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7687 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7688
7689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007690 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007691 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7692 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7694 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7697 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7699 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7700
7701 // Move.
7702 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7703 processSlot(mapper, 0);
7704 processPosition(mapper, x1, y1);
7705 processSlot(mapper, 1);
7706 processPosition(mapper, x2, y2);
7707 processSync(mapper);
7708
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7711 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7712 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7714 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7717 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7719 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7720
7721 // First finger up.
7722 x2 += 15; y2 -= 20;
7723 processSlot(mapper, 0);
7724 processId(mapper, -1);
7725 processSlot(mapper, 1);
7726 processPosition(mapper, x2, y2);
7727 processSync(mapper);
7728
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007730 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007731 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7732 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7734 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7737 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7739 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7740
7741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7743 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7744 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7747 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7748
7749 // Move.
7750 x2 += 20; y2 -= 25;
7751 processPosition(mapper, x2, y2);
7752 processSync(mapper);
7753
7754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7756 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7757 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7760 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7761
7762 // New finger down.
7763 int32_t x3 = 700, y3 = 300;
7764 processPosition(mapper, x2, y2);
7765 processSlot(mapper, 0);
7766 processId(mapper, 3);
7767 processPosition(mapper, x3, y3);
7768 processSync(mapper);
7769
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007771 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7775 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7778 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7780 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7781
7782 // Second finger up.
7783 x3 += 30; y3 -= 20;
7784 processSlot(mapper, 1);
7785 processId(mapper, -1);
7786 processSlot(mapper, 0);
7787 processPosition(mapper, x3, y3);
7788 processSync(mapper);
7789
7790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007791 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007792 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7793 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7795 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7796 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7798 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7800 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7801
7802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7804 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7805 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7808 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7809
7810 // Last finger up.
7811 processId(mapper, -1);
7812 processSync(mapper);
7813
7814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7815 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7816 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7817 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7820 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7821
7822 // Should not have sent any more keys or motions.
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7825}
7826
7827TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007828 addConfigurationProperty("touch.deviceType", "touchScreen");
7829 prepareDisplay(DISPLAY_ORIENTATION_0);
7830 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007831 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007832
7833 // These calculations are based on the input device calibration documentation.
7834 int32_t rawX = 100;
7835 int32_t rawY = 200;
7836 int32_t rawTouchMajor = 7;
7837 int32_t rawTouchMinor = 6;
7838 int32_t rawToolMajor = 9;
7839 int32_t rawToolMinor = 8;
7840 int32_t rawPressure = 11;
7841 int32_t rawDistance = 0;
7842 int32_t rawOrientation = 3;
7843 int32_t id = 5;
7844
7845 float x = toDisplayX(rawX);
7846 float y = toDisplayY(rawY);
7847 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7848 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7849 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7850 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7851 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7852 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7853 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7854 float distance = float(rawDistance);
7855
7856 processPosition(mapper, rawX, rawY);
7857 processTouchMajor(mapper, rawTouchMajor);
7858 processTouchMinor(mapper, rawTouchMinor);
7859 processToolMajor(mapper, rawToolMajor);
7860 processToolMinor(mapper, rawToolMinor);
7861 processPressure(mapper, rawPressure);
7862 processOrientation(mapper, rawOrientation);
7863 processDistance(mapper, rawDistance);
7864 processId(mapper, id);
7865 processMTSync(mapper);
7866 processSync(mapper);
7867
7868 NotifyMotionArgs args;
7869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7870 ASSERT_EQ(0, args.pointerProperties[0].id);
7871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7872 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7873 orientation, distance));
7874}
7875
7876TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877 addConfigurationProperty("touch.deviceType", "touchScreen");
7878 prepareDisplay(DISPLAY_ORIENTATION_0);
7879 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7880 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007881 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007882
7883 // These calculations are based on the input device calibration documentation.
7884 int32_t rawX = 100;
7885 int32_t rawY = 200;
7886 int32_t rawTouchMajor = 140;
7887 int32_t rawTouchMinor = 120;
7888 int32_t rawToolMajor = 180;
7889 int32_t rawToolMinor = 160;
7890
7891 float x = toDisplayX(rawX);
7892 float y = toDisplayY(rawY);
7893 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7894 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7895 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7896 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7897 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7898
7899 processPosition(mapper, rawX, rawY);
7900 processTouchMajor(mapper, rawTouchMajor);
7901 processTouchMinor(mapper, rawTouchMinor);
7902 processToolMajor(mapper, rawToolMajor);
7903 processToolMinor(mapper, rawToolMinor);
7904 processMTSync(mapper);
7905 processSync(mapper);
7906
7907 NotifyMotionArgs args;
7908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7910 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7911}
7912
7913TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007914 addConfigurationProperty("touch.deviceType", "touchScreen");
7915 prepareDisplay(DISPLAY_ORIENTATION_0);
7916 prepareAxes(POSITION | TOUCH | TOOL);
7917 addConfigurationProperty("touch.size.calibration", "diameter");
7918 addConfigurationProperty("touch.size.scale", "10");
7919 addConfigurationProperty("touch.size.bias", "160");
7920 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007921 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007922
7923 // These calculations are based on the input device calibration documentation.
7924 // Note: We only provide a single common touch/tool value because the device is assumed
7925 // not to emit separate values for each pointer (isSummed = 1).
7926 int32_t rawX = 100;
7927 int32_t rawY = 200;
7928 int32_t rawX2 = 150;
7929 int32_t rawY2 = 250;
7930 int32_t rawTouchMajor = 5;
7931 int32_t rawToolMajor = 8;
7932
7933 float x = toDisplayX(rawX);
7934 float y = toDisplayY(rawY);
7935 float x2 = toDisplayX(rawX2);
7936 float y2 = toDisplayY(rawY2);
7937 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7938 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7939 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7940
7941 processPosition(mapper, rawX, rawY);
7942 processTouchMajor(mapper, rawTouchMajor);
7943 processToolMajor(mapper, rawToolMajor);
7944 processMTSync(mapper);
7945 processPosition(mapper, rawX2, rawY2);
7946 processTouchMajor(mapper, rawTouchMajor);
7947 processToolMajor(mapper, rawToolMajor);
7948 processMTSync(mapper);
7949 processSync(mapper);
7950
7951 NotifyMotionArgs args;
7952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7953 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7954
7955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007956 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957 ASSERT_EQ(size_t(2), args.pointerCount);
7958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7959 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7961 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7962}
7963
7964TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007965 addConfigurationProperty("touch.deviceType", "touchScreen");
7966 prepareDisplay(DISPLAY_ORIENTATION_0);
7967 prepareAxes(POSITION | TOUCH | TOOL);
7968 addConfigurationProperty("touch.size.calibration", "area");
7969 addConfigurationProperty("touch.size.scale", "43");
7970 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007971 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972
7973 // These calculations are based on the input device calibration documentation.
7974 int32_t rawX = 100;
7975 int32_t rawY = 200;
7976 int32_t rawTouchMajor = 5;
7977 int32_t rawToolMajor = 8;
7978
7979 float x = toDisplayX(rawX);
7980 float y = toDisplayY(rawY);
7981 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7982 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7983 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7984
7985 processPosition(mapper, rawX, rawY);
7986 processTouchMajor(mapper, rawTouchMajor);
7987 processToolMajor(mapper, rawToolMajor);
7988 processMTSync(mapper);
7989 processSync(mapper);
7990
7991 NotifyMotionArgs args;
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7994 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7995}
7996
7997TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998 addConfigurationProperty("touch.deviceType", "touchScreen");
7999 prepareDisplay(DISPLAY_ORIENTATION_0);
8000 prepareAxes(POSITION | PRESSURE);
8001 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8002 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008003 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008004
Michael Wrightaa449c92017-12-13 21:21:43 +00008005 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008006 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008007 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8008 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8009 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8010
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 // These calculations are based on the input device calibration documentation.
8012 int32_t rawX = 100;
8013 int32_t rawY = 200;
8014 int32_t rawPressure = 60;
8015
8016 float x = toDisplayX(rawX);
8017 float y = toDisplayY(rawY);
8018 float pressure = float(rawPressure) * 0.01f;
8019
8020 processPosition(mapper, rawX, rawY);
8021 processPressure(mapper, rawPressure);
8022 processMTSync(mapper);
8023 processSync(mapper);
8024
8025 NotifyMotionArgs args;
8026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8028 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8029}
8030
8031TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008032 addConfigurationProperty("touch.deviceType", "touchScreen");
8033 prepareDisplay(DISPLAY_ORIENTATION_0);
8034 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008035 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036
8037 NotifyMotionArgs motionArgs;
8038 NotifyKeyArgs keyArgs;
8039
8040 processId(mapper, 1);
8041 processPosition(mapper, 100, 200);
8042 processSync(mapper);
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8045 ASSERT_EQ(0, motionArgs.buttonState);
8046
8047 // press BTN_LEFT, release BTN_LEFT
8048 processKey(mapper, BTN_LEFT, 1);
8049 processSync(mapper);
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8052 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8053
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8056 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8057
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058 processKey(mapper, BTN_LEFT, 0);
8059 processSync(mapper);
8060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008063
8064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008066 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067
8068 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8069 processKey(mapper, BTN_RIGHT, 1);
8070 processKey(mapper, BTN_MIDDLE, 1);
8071 processSync(mapper);
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8074 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8075 motionArgs.buttonState);
8076
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8078 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8079 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8080
8081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8083 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8084 motionArgs.buttonState);
8085
Michael Wrightd02c5b62014-02-10 15:10:22 -08008086 processKey(mapper, BTN_RIGHT, 0);
8087 processSync(mapper);
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008089 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008091
8092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008094 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095
8096 processKey(mapper, BTN_MIDDLE, 0);
8097 processSync(mapper);
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008099 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008101
8102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008104 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008105
8106 // press BTN_BACK, release BTN_BACK
8107 processKey(mapper, BTN_BACK, 1);
8108 processSync(mapper);
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8110 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8111 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008112
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008115 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8116
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8119 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120
8121 processKey(mapper, BTN_BACK, 0);
8122 processSync(mapper);
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008126
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008129 ASSERT_EQ(0, motionArgs.buttonState);
8130
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8132 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8133 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8134
8135 // press BTN_SIDE, release BTN_SIDE
8136 processKey(mapper, BTN_SIDE, 1);
8137 processSync(mapper);
8138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8139 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8140 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008141
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008144 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8145
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8148 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149
8150 processKey(mapper, BTN_SIDE, 0);
8151 processSync(mapper);
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008158 ASSERT_EQ(0, motionArgs.buttonState);
8159
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8161 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8162 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8163
8164 // press BTN_FORWARD, release BTN_FORWARD
8165 processKey(mapper, BTN_FORWARD, 1);
8166 processSync(mapper);
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8168 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8169 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008170
Michael Wrightd02c5b62014-02-10 15:10:22 -08008171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008173 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8174
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8176 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8177 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178
8179 processKey(mapper, BTN_FORWARD, 0);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008184
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008187 ASSERT_EQ(0, motionArgs.buttonState);
8188
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8190 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8191 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8192
8193 // press BTN_EXTRA, release BTN_EXTRA
8194 processKey(mapper, BTN_EXTRA, 1);
8195 processSync(mapper);
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8197 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8198 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008199
Michael Wrightd02c5b62014-02-10 15:10:22 -08008200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008202 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8203
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8205 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8206 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207
8208 processKey(mapper, BTN_EXTRA, 0);
8209 processSync(mapper);
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008212 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008213
8214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008216 ASSERT_EQ(0, motionArgs.buttonState);
8217
Michael Wrightd02c5b62014-02-10 15:10:22 -08008218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8219 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8220 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8221
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8223
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 // press BTN_STYLUS, release BTN_STYLUS
8225 processKey(mapper, BTN_STYLUS, 1);
8226 processSync(mapper);
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008229 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8230
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8233 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234
8235 processKey(mapper, BTN_STYLUS, 0);
8236 processSync(mapper);
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008240
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008243 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008244
8245 // press BTN_STYLUS2, release BTN_STYLUS2
8246 processKey(mapper, BTN_STYLUS2, 1);
8247 processSync(mapper);
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008250 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8251
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8254 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008255
8256 processKey(mapper, BTN_STYLUS2, 0);
8257 processSync(mapper);
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008259 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008261
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008264 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008265
8266 // release touch
8267 processId(mapper, -1);
8268 processSync(mapper);
8269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8270 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8271 ASSERT_EQ(0, motionArgs.buttonState);
8272}
8273
8274TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008275 addConfigurationProperty("touch.deviceType", "touchScreen");
8276 prepareDisplay(DISPLAY_ORIENTATION_0);
8277 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008278 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279
8280 NotifyMotionArgs motionArgs;
8281
8282 // default tool type is finger
8283 processId(mapper, 1);
8284 processPosition(mapper, 100, 200);
8285 processSync(mapper);
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8287 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8289
8290 // eraser
8291 processKey(mapper, BTN_TOOL_RUBBER, 1);
8292 processSync(mapper);
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8296
8297 // stylus
8298 processKey(mapper, BTN_TOOL_RUBBER, 0);
8299 processKey(mapper, BTN_TOOL_PEN, 1);
8300 processSync(mapper);
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8304
8305 // brush
8306 processKey(mapper, BTN_TOOL_PEN, 0);
8307 processKey(mapper, BTN_TOOL_BRUSH, 1);
8308 processSync(mapper);
8309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8312
8313 // pencil
8314 processKey(mapper, BTN_TOOL_BRUSH, 0);
8315 processKey(mapper, BTN_TOOL_PENCIL, 1);
8316 processSync(mapper);
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8320
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008321 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008322 processKey(mapper, BTN_TOOL_PENCIL, 0);
8323 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8324 processSync(mapper);
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8328
8329 // mouse
8330 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8331 processKey(mapper, BTN_TOOL_MOUSE, 1);
8332 processSync(mapper);
8333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8336
8337 // lens
8338 processKey(mapper, BTN_TOOL_MOUSE, 0);
8339 processKey(mapper, BTN_TOOL_LENS, 1);
8340 processSync(mapper);
8341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8344
8345 // double-tap
8346 processKey(mapper, BTN_TOOL_LENS, 0);
8347 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8348 processSync(mapper);
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8352
8353 // triple-tap
8354 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8355 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8356 processSync(mapper);
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8360
8361 // quad-tap
8362 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8363 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8364 processSync(mapper);
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8368
8369 // finger
8370 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8371 processKey(mapper, BTN_TOOL_FINGER, 1);
8372 processSync(mapper);
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8376
8377 // stylus trumps finger
8378 processKey(mapper, BTN_TOOL_PEN, 1);
8379 processSync(mapper);
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8383
8384 // eraser trumps stylus
8385 processKey(mapper, BTN_TOOL_RUBBER, 1);
8386 processSync(mapper);
8387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8390
8391 // mouse trumps eraser
8392 processKey(mapper, BTN_TOOL_MOUSE, 1);
8393 processSync(mapper);
8394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8397
8398 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8399 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8400 processSync(mapper);
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8404
8405 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8406 processToolType(mapper, MT_TOOL_PEN);
8407 processSync(mapper);
8408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8411
8412 // back to default tool type
8413 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8414 processKey(mapper, BTN_TOOL_MOUSE, 0);
8415 processKey(mapper, BTN_TOOL_RUBBER, 0);
8416 processKey(mapper, BTN_TOOL_PEN, 0);
8417 processKey(mapper, BTN_TOOL_FINGER, 0);
8418 processSync(mapper);
8419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8422}
8423
8424TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008425 addConfigurationProperty("touch.deviceType", "touchScreen");
8426 prepareDisplay(DISPLAY_ORIENTATION_0);
8427 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008428 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008429 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430
8431 NotifyMotionArgs motionArgs;
8432
8433 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8434 processId(mapper, 1);
8435 processPosition(mapper, 100, 200);
8436 processSync(mapper);
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8438 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8440 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8441
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8445 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8446
8447 // move a little
8448 processPosition(mapper, 150, 250);
8449 processSync(mapper);
8450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8453 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8454
8455 // down when BTN_TOUCH is pressed, pressure defaults to 1
8456 processKey(mapper, BTN_TOUCH, 1);
8457 processSync(mapper);
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8459 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8461 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8462
8463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8464 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8466 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8467
8468 // up when BTN_TOUCH is released, hover restored
8469 processKey(mapper, BTN_TOUCH, 0);
8470 processSync(mapper);
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8472 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8474 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8475
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8477 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8479 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8480
8481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8482 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8484 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8485
8486 // exit hover when pointer goes away
8487 processId(mapper, -1);
8488 processSync(mapper);
8489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8490 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8492 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8493}
8494
8495TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496 addConfigurationProperty("touch.deviceType", "touchScreen");
8497 prepareDisplay(DISPLAY_ORIENTATION_0);
8498 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008499 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008500
8501 NotifyMotionArgs motionArgs;
8502
8503 // initially hovering because pressure is 0
8504 processId(mapper, 1);
8505 processPosition(mapper, 100, 200);
8506 processPressure(mapper, 0);
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8511 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8512
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8516 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8517
8518 // move a little
8519 processPosition(mapper, 150, 250);
8520 processSync(mapper);
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8522 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8524 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8525
8526 // down when pressure becomes non-zero
8527 processPressure(mapper, RAW_PRESSURE_MAX);
8528 processSync(mapper);
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8532 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8533
8534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8535 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8537 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8538
8539 // up when pressure becomes 0, hover restored
8540 processPressure(mapper, 0);
8541 processSync(mapper);
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8543 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8545 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8546
8547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8548 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8550 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8551
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8553 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8555 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8556
8557 // exit hover when pointer goes away
8558 processId(mapper, -1);
8559 processSync(mapper);
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8563 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8564}
8565
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008566/**
8567 * Set the input device port <--> display port associations, and check that the
8568 * events are routed to the display that matches the display port.
8569 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8570 */
8571TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008572 const std::string usb2 = "USB2";
8573 const uint8_t hdmi1 = 0;
8574 const uint8_t hdmi2 = 1;
8575 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008576 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008577
8578 addConfigurationProperty("touch.deviceType", "touchScreen");
8579 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008580 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008581
8582 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8583 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8584
8585 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8586 // for this input device is specified, and the matching viewport is not present,
8587 // the input device should be disabled (at the mapper level).
8588
8589 // Add viewport for display 2 on hdmi2
8590 prepareSecondaryDisplay(type, hdmi2);
8591 // Send a touch event
8592 processPosition(mapper, 100, 100);
8593 processSync(mapper);
8594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8595
8596 // Add viewport for display 1 on hdmi1
8597 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8598 // Send a touch event again
8599 processPosition(mapper, 100, 100);
8600 processSync(mapper);
8601
8602 NotifyMotionArgs args;
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8604 ASSERT_EQ(DISPLAY_ID, args.displayId);
8605}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008606
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008607TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8608 addConfigurationProperty("touch.deviceType", "touchScreen");
8609 prepareAxes(POSITION);
8610 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8611
8612 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8613
8614 prepareDisplay(DISPLAY_ORIENTATION_0);
8615 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8616
8617 // Send a touch event
8618 processPosition(mapper, 100, 100);
8619 processSync(mapper);
8620
8621 NotifyMotionArgs args;
8622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8623 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8624}
8625
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008626TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008627 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008628 std::shared_ptr<FakePointerController> fakePointerController =
8629 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008630 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008631 fakePointerController->setPosition(100, 200);
8632 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008633 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008634
Garfield Tan888a6a42020-01-09 11:39:16 -08008635 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008636 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008637
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008638 prepareDisplay(DISPLAY_ORIENTATION_0);
8639 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008640 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008641
8642 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008643 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008644
8645 NotifyMotionArgs motionArgs;
8646 processPosition(mapper, 100, 100);
8647 processSync(mapper);
8648
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8650 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8651 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8652}
8653
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008654/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008655 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8656 */
8657TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8658 addConfigurationProperty("touch.deviceType", "touchScreen");
8659 prepareAxes(POSITION);
8660 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8661
8662 prepareDisplay(DISPLAY_ORIENTATION_0);
8663 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8664 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8665 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8666 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8667
8668 NotifyMotionArgs args;
8669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8670 ASSERT_EQ(26, args.readTime);
8671
8672 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8673 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8674 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8675
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8677 ASSERT_EQ(33, args.readTime);
8678}
8679
8680/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008681 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8682 * events should not be delivered to the listener.
8683 */
8684TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8685 addConfigurationProperty("touch.deviceType", "touchScreen");
8686 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8687 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8688 ViewportType::INTERNAL);
8689 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8690 prepareAxes(POSITION);
8691 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8692
8693 NotifyMotionArgs motionArgs;
8694 processPosition(mapper, 100, 100);
8695 processSync(mapper);
8696
8697 mFakeListener->assertNotifyMotionWasNotCalled();
8698}
8699
Garfield Tanc734e4f2021-01-15 20:01:39 -08008700TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8701 addConfigurationProperty("touch.deviceType", "touchScreen");
8702 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8703 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8704 ViewportType::INTERNAL);
8705 std::optional<DisplayViewport> optionalDisplayViewport =
8706 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8707 ASSERT_TRUE(optionalDisplayViewport.has_value());
8708 DisplayViewport displayViewport = *optionalDisplayViewport;
8709
8710 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8711 prepareAxes(POSITION);
8712 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8713
8714 // Finger down
8715 int32_t x = 100, y = 100;
8716 processPosition(mapper, x, y);
8717 processSync(mapper);
8718
8719 NotifyMotionArgs motionArgs;
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8721 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8722
8723 // Deactivate display viewport
8724 displayViewport.isActive = false;
8725 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8726 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8727
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008728 // The ongoing touch should be canceled immediately
8729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8730 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8731
8732 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008733 x += 10, y += 10;
8734 processPosition(mapper, x, y);
8735 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008737
8738 // Reactivate display viewport
8739 displayViewport.isActive = true;
8740 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8741 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8742
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008743 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008744 x += 10, y += 10;
8745 processPosition(mapper, x, y);
8746 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8748 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008749}
8750
Arthur Hung7c645402019-01-25 17:45:42 +08008751TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8752 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008753 prepareAxes(POSITION | ID | SLOT);
8754 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008755 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008756
8757 // Create the second touch screen device, and enable multi fingers.
8758 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008759 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008760 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008761 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008762 std::shared_ptr<InputDevice> device2 =
8763 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008764 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008765
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008766 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8767 0 /*flat*/, 0 /*fuzz*/);
8768 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8769 0 /*flat*/, 0 /*fuzz*/);
8770 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8771 0 /*flat*/, 0 /*fuzz*/);
8772 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8773 0 /*flat*/, 0 /*fuzz*/);
8774 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8775 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8776 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008777
8778 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008779 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008780 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8781 device2->reset(ARBITRARY_TIME);
8782
8783 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008784 std::shared_ptr<FakePointerController> fakePointerController =
8785 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008786 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008787
8788 // Setup policy for associated displays and show touches.
8789 const uint8_t hdmi1 = 0;
8790 const uint8_t hdmi2 = 1;
8791 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8792 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8793 mFakePolicy->setShowTouches(true);
8794
8795 // Create displays.
8796 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008797 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008798
8799 // Default device will reconfigure above, need additional reconfiguration for another device.
8800 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008801 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008802
8803 // Two fingers down at default display.
8804 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8805 processPosition(mapper, x1, y1);
8806 processId(mapper, 1);
8807 processSlot(mapper, 1);
8808 processPosition(mapper, x2, y2);
8809 processId(mapper, 2);
8810 processSync(mapper);
8811
8812 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8813 fakePointerController->getSpots().find(DISPLAY_ID);
8814 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8815 ASSERT_EQ(size_t(2), iter->second.size());
8816
8817 // Two fingers down at second display.
8818 processPosition(mapper2, x1, y1);
8819 processId(mapper2, 1);
8820 processSlot(mapper2, 1);
8821 processPosition(mapper2, x2, y2);
8822 processId(mapper2, 2);
8823 processSync(mapper2);
8824
8825 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8826 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8827 ASSERT_EQ(size_t(2), iter->second.size());
8828}
8829
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008830TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008831 prepareAxes(POSITION);
8832 addConfigurationProperty("touch.deviceType", "touchScreen");
8833 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008834 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008835
8836 NotifyMotionArgs motionArgs;
8837 // Unrotated video frame
8838 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8839 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008840 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008841 processPosition(mapper, 100, 200);
8842 processSync(mapper);
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8844 ASSERT_EQ(frames, motionArgs.videoFrames);
8845
8846 // Subsequent touch events should not have any videoframes
8847 // This is implemented separately in FakeEventHub,
8848 // but that should match the behaviour of TouchVideoDevice.
8849 processPosition(mapper, 200, 200);
8850 processSync(mapper);
8851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8852 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8853}
8854
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008855TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008856 prepareAxes(POSITION);
8857 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008858 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008859 // Unrotated video frame
8860 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8861 NotifyMotionArgs motionArgs;
8862
8863 // Test all 4 orientations
8864 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008865 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8866 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8867 clearViewports();
8868 prepareDisplay(orientation);
8869 std::vector<TouchVideoFrame> frames{frame};
8870 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8871 processPosition(mapper, 100, 200);
8872 processSync(mapper);
8873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8874 ASSERT_EQ(frames, motionArgs.videoFrames);
8875 }
8876}
8877
8878TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8879 prepareAxes(POSITION);
8880 addConfigurationProperty("touch.deviceType", "touchScreen");
8881 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8882 // orientation-aware are affected by display rotation.
8883 addConfigurationProperty("touch.orientationAware", "0");
8884 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8885 // Unrotated video frame
8886 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8887 NotifyMotionArgs motionArgs;
8888
8889 // Test all 4 orientations
8890 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008891 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8892 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8893 clearViewports();
8894 prepareDisplay(orientation);
8895 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008896 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008897 processPosition(mapper, 100, 200);
8898 processSync(mapper);
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008900 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8901 // compared to the display. This is so that when the window transform (which contains the
8902 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8903 // window's coordinate space.
8904 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008905 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnane74b35f2022-07-19 16:00:50 +08008906
8907 // Release finger.
8908 processSync(mapper);
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008910 }
8911}
8912
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008913TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008914 prepareAxes(POSITION);
8915 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008916 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008917 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8918 // so mix these.
8919 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8920 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8921 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8922 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8923 NotifyMotionArgs motionArgs;
8924
8925 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008926 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008927 processPosition(mapper, 100, 200);
8928 processSync(mapper);
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008930 ASSERT_EQ(frames, motionArgs.videoFrames);
8931}
8932
8933TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8934 prepareAxes(POSITION);
8935 addConfigurationProperty("touch.deviceType", "touchScreen");
8936 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8937 // orientation-aware are affected by display rotation.
8938 addConfigurationProperty("touch.orientationAware", "0");
8939 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8940 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8941 // so mix these.
8942 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8943 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8944 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8945 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8946 NotifyMotionArgs motionArgs;
8947
8948 prepareDisplay(DISPLAY_ORIENTATION_90);
8949 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8950 processPosition(mapper, 100, 200);
8951 processSync(mapper);
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8953 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8954 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8955 // compared to the display. This is so that when the window transform (which contains the
8956 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8957 // window's coordinate space.
8958 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8959 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008960 ASSERT_EQ(frames, motionArgs.videoFrames);
8961}
8962
Arthur Hung9da14732019-09-02 16:16:58 +08008963/**
8964 * If we had defined port associations, but the viewport is not ready, the touch device would be
8965 * expected to be disabled, and it should be enabled after the viewport has found.
8966 */
8967TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008968 constexpr uint8_t hdmi2 = 1;
8969 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008970 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008971
8972 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8973
8974 addConfigurationProperty("touch.deviceType", "touchScreen");
8975 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008976 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008977
8978 ASSERT_EQ(mDevice->isEnabled(), false);
8979
8980 // Add display on hdmi2, the device should be enabled and can receive touch event.
8981 prepareSecondaryDisplay(type, hdmi2);
8982 ASSERT_EQ(mDevice->isEnabled(), true);
8983
8984 // Send a touch event.
8985 processPosition(mapper, 100, 100);
8986 processSync(mapper);
8987
8988 NotifyMotionArgs args;
8989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8990 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8991}
8992
Arthur Hung421eb1c2020-01-16 00:09:42 +08008993TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008994 addConfigurationProperty("touch.deviceType", "touchScreen");
8995 prepareDisplay(DISPLAY_ORIENTATION_0);
8996 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008997 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008998
8999 NotifyMotionArgs motionArgs;
9000
9001 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9002 // finger down
9003 processId(mapper, 1);
9004 processPosition(mapper, x1, y1);
9005 processSync(mapper);
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9007 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9009
9010 // finger move
9011 processId(mapper, 1);
9012 processPosition(mapper, x2, y2);
9013 processSync(mapper);
9014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9016 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9017
9018 // finger up.
9019 processId(mapper, -1);
9020 processSync(mapper);
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9022 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9024
9025 // new finger down
9026 processId(mapper, 1);
9027 processPosition(mapper, x3, y3);
9028 processSync(mapper);
9029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9030 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9032}
9033
9034/**
arthurhungcc7f9802020-04-30 17:55:40 +08009035 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9036 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009037 */
arthurhungcc7f9802020-04-30 17:55:40 +08009038TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009039 addConfigurationProperty("touch.deviceType", "touchScreen");
9040 prepareDisplay(DISPLAY_ORIENTATION_0);
9041 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009042 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009043
9044 NotifyMotionArgs motionArgs;
9045
9046 // default tool type is finger
9047 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009048 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009049 processPosition(mapper, x1, y1);
9050 processSync(mapper);
9051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9052 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9054
9055 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9056 processToolType(mapper, MT_TOOL_PALM);
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9059 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9060
9061 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009062 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009063 processPosition(mapper, x2, y2);
9064 processSync(mapper);
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9066
9067 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009068 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009069 processSync(mapper);
9070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9071
9072 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009073 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009074 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009075 processPosition(mapper, x3, y3);
9076 processSync(mapper);
9077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9078 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9080}
9081
arthurhungbf89a482020-04-17 17:37:55 +08009082/**
arthurhungcc7f9802020-04-30 17:55:40 +08009083 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9084 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009085 */
arthurhungcc7f9802020-04-30 17:55:40 +08009086TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009087 addConfigurationProperty("touch.deviceType", "touchScreen");
9088 prepareDisplay(DISPLAY_ORIENTATION_0);
9089 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9090 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9091
9092 NotifyMotionArgs motionArgs;
9093
9094 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009095 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9096 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009097 processPosition(mapper, x1, y1);
9098 processSync(mapper);
9099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9100 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9102
9103 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009104 processSlot(mapper, SECOND_SLOT);
9105 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009106 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009109 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9111
9112 // If the tool type of the first finger changes to MT_TOOL_PALM,
9113 // we expect to receive ACTION_POINTER_UP with cancel flag.
9114 processSlot(mapper, FIRST_SLOT);
9115 processId(mapper, FIRST_TRACKING_ID);
9116 processToolType(mapper, MT_TOOL_PALM);
9117 processSync(mapper);
9118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009119 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009120 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9121
9122 // The following MOVE events of second finger should be processed.
9123 processSlot(mapper, SECOND_SLOT);
9124 processId(mapper, SECOND_TRACKING_ID);
9125 processPosition(mapper, x2 + 1, y2 + 1);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9129 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9130
9131 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9132 // it. Second finger receive move.
9133 processSlot(mapper, FIRST_SLOT);
9134 processId(mapper, INVALID_TRACKING_ID);
9135 processSync(mapper);
9136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9138 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9139
9140 // Second finger keeps moving.
9141 processSlot(mapper, SECOND_SLOT);
9142 processId(mapper, SECOND_TRACKING_ID);
9143 processPosition(mapper, x2 + 2, y2 + 2);
9144 processSync(mapper);
9145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9147 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9148
9149 // Second finger up.
9150 processId(mapper, INVALID_TRACKING_ID);
9151 processSync(mapper);
9152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9153 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9154 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9155}
9156
9157/**
9158 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9159 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9160 */
9161TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9162 addConfigurationProperty("touch.deviceType", "touchScreen");
9163 prepareDisplay(DISPLAY_ORIENTATION_0);
9164 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9165 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9166
9167 NotifyMotionArgs motionArgs;
9168
9169 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9170 // First finger down.
9171 processId(mapper, FIRST_TRACKING_ID);
9172 processPosition(mapper, x1, y1);
9173 processSync(mapper);
9174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9175 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9177
9178 // Second finger down.
9179 processSlot(mapper, SECOND_SLOT);
9180 processId(mapper, SECOND_TRACKING_ID);
9181 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009182 processSync(mapper);
9183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009184 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9186
arthurhungcc7f9802020-04-30 17:55:40 +08009187 // If the tool type of the first finger changes to MT_TOOL_PALM,
9188 // we expect to receive ACTION_POINTER_UP with cancel flag.
9189 processSlot(mapper, FIRST_SLOT);
9190 processId(mapper, FIRST_TRACKING_ID);
9191 processToolType(mapper, MT_TOOL_PALM);
9192 processSync(mapper);
9193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009194 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009195 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9196
9197 // Second finger keeps moving.
9198 processSlot(mapper, SECOND_SLOT);
9199 processId(mapper, SECOND_TRACKING_ID);
9200 processPosition(mapper, x2 + 1, y2 + 1);
9201 processSync(mapper);
9202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9204
9205 // second finger becomes palm, receive cancel due to only 1 finger is active.
9206 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009207 processToolType(mapper, MT_TOOL_PALM);
9208 processSync(mapper);
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9210 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9211
arthurhungcc7f9802020-04-30 17:55:40 +08009212 // third finger down.
9213 processSlot(mapper, THIRD_SLOT);
9214 processId(mapper, THIRD_TRACKING_ID);
9215 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009216 processPosition(mapper, x3, y3);
9217 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9219 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009221 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9222
9223 // third finger move
9224 processId(mapper, THIRD_TRACKING_ID);
9225 processPosition(mapper, x3 + 1, y3 + 1);
9226 processSync(mapper);
9227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9229
9230 // first finger up, third finger receive move.
9231 processSlot(mapper, FIRST_SLOT);
9232 processId(mapper, INVALID_TRACKING_ID);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9236 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9237
9238 // second finger up, third finger receive move.
9239 processSlot(mapper, SECOND_SLOT);
9240 processId(mapper, INVALID_TRACKING_ID);
9241 processSync(mapper);
9242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9244 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9245
9246 // third finger up.
9247 processSlot(mapper, THIRD_SLOT);
9248 processId(mapper, INVALID_TRACKING_ID);
9249 processSync(mapper);
9250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9251 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9252 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9253}
9254
9255/**
9256 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9257 * and the active finger could still be allowed to receive the events
9258 */
9259TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9260 addConfigurationProperty("touch.deviceType", "touchScreen");
9261 prepareDisplay(DISPLAY_ORIENTATION_0);
9262 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9263 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9264
9265 NotifyMotionArgs motionArgs;
9266
9267 // default tool type is finger
9268 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9269 processId(mapper, FIRST_TRACKING_ID);
9270 processPosition(mapper, x1, y1);
9271 processSync(mapper);
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9273 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9275
9276 // Second finger down.
9277 processSlot(mapper, SECOND_SLOT);
9278 processId(mapper, SECOND_TRACKING_ID);
9279 processPosition(mapper, x2, y2);
9280 processSync(mapper);
9281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009282 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9284
9285 // If the tool type of the second finger changes to MT_TOOL_PALM,
9286 // we expect to receive ACTION_POINTER_UP with cancel flag.
9287 processId(mapper, SECOND_TRACKING_ID);
9288 processToolType(mapper, MT_TOOL_PALM);
9289 processSync(mapper);
9290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009291 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009292 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9293
9294 // The following MOVE event should be processed.
9295 processSlot(mapper, FIRST_SLOT);
9296 processId(mapper, FIRST_TRACKING_ID);
9297 processPosition(mapper, x1 + 1, y1 + 1);
9298 processSync(mapper);
9299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9301 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9302
9303 // second finger up.
9304 processSlot(mapper, SECOND_SLOT);
9305 processId(mapper, INVALID_TRACKING_ID);
9306 processSync(mapper);
9307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9309
9310 // first finger keep moving
9311 processSlot(mapper, FIRST_SLOT);
9312 processId(mapper, FIRST_TRACKING_ID);
9313 processPosition(mapper, x1 + 2, y1 + 2);
9314 processSync(mapper);
9315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9317
9318 // first finger up.
9319 processId(mapper, INVALID_TRACKING_ID);
9320 processSync(mapper);
9321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9322 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9323 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009324}
9325
Arthur Hung9ad18942021-06-19 02:04:46 +00009326/**
9327 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9328 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9329 * cause slot be valid again.
9330 */
9331TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9332 addConfigurationProperty("touch.deviceType", "touchScreen");
9333 prepareDisplay(DISPLAY_ORIENTATION_0);
9334 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9335 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9336
9337 NotifyMotionArgs motionArgs;
9338
9339 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9340 // First finger down.
9341 processId(mapper, FIRST_TRACKING_ID);
9342 processPosition(mapper, x1, y1);
9343 processPressure(mapper, RAW_PRESSURE_MAX);
9344 processSync(mapper);
9345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9346 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9347 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9348
9349 // First finger move.
9350 processId(mapper, FIRST_TRACKING_ID);
9351 processPosition(mapper, x1 + 1, y1 + 1);
9352 processPressure(mapper, RAW_PRESSURE_MAX);
9353 processSync(mapper);
9354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9356 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9357
9358 // Second finger down.
9359 processSlot(mapper, SECOND_SLOT);
9360 processId(mapper, SECOND_TRACKING_ID);
9361 processPosition(mapper, x2, y2);
9362 processPressure(mapper, RAW_PRESSURE_MAX);
9363 processSync(mapper);
9364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009365 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009366 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9367
9368 // second finger up with some unexpected data.
9369 processSlot(mapper, SECOND_SLOT);
9370 processId(mapper, INVALID_TRACKING_ID);
9371 processPosition(mapper, x2, y2);
9372 processSync(mapper);
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009374 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009375 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9376
9377 // first finger up with some unexpected data.
9378 processSlot(mapper, FIRST_SLOT);
9379 processId(mapper, INVALID_TRACKING_ID);
9380 processPosition(mapper, x2, y2);
9381 processPressure(mapper, RAW_PRESSURE_MAX);
9382 processSync(mapper);
9383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9384 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9385 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9386}
9387
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009388// --- MultiTouchInputMapperTest_ExternalDevice ---
9389
9390class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9391protected:
Chris Yea52ade12020-08-27 16:49:20 -07009392 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009393};
9394
9395/**
9396 * Expect fallback to internal viewport if device is external and external viewport is not present.
9397 */
9398TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9399 prepareAxes(POSITION);
9400 addConfigurationProperty("touch.deviceType", "touchScreen");
9401 prepareDisplay(DISPLAY_ORIENTATION_0);
9402 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9403
9404 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9405
9406 NotifyMotionArgs motionArgs;
9407
9408 // Expect the event to be sent to the internal viewport,
9409 // because an external viewport is not present.
9410 processPosition(mapper, 100, 100);
9411 processSync(mapper);
9412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9413 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9414
9415 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009416 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009417 processPosition(mapper, 100, 100);
9418 processSync(mapper);
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9420 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9421}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009422
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009423TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9424 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9425 std::shared_ptr<FakePointerController> fakePointerController =
9426 std::make_shared<FakePointerController>();
9427 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9428 fakePointerController->setPosition(0, 0);
9429 fakePointerController->setButtonState(0);
9430
9431 // prepare device and capture
9432 prepareDisplay(DISPLAY_ORIENTATION_0);
9433 prepareAxes(POSITION | ID | SLOT);
9434 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9435 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9436 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009437 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009438 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9439
9440 // captured touchpad should be a touchpad source
9441 NotifyDeviceResetArgs resetArgs;
9442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9443 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9444
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009445 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009446
9447 const InputDeviceInfo::MotionRange* relRangeX =
9448 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9449 ASSERT_NE(relRangeX, nullptr);
9450 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9451 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9452 const InputDeviceInfo::MotionRange* relRangeY =
9453 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9454 ASSERT_NE(relRangeY, nullptr);
9455 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9456 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9457
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009458 // run captured pointer tests - note that this is unscaled, so input listener events should be
9459 // identical to what the hardware sends (accounting for any
9460 // calibration).
9461 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009462 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009463 processId(mapper, 1);
9464 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9465 processKey(mapper, BTN_TOUCH, 1);
9466 processSync(mapper);
9467
9468 // expect coord[0] to contain initial location of touch 0
9469 NotifyMotionArgs args;
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9471 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9472 ASSERT_EQ(1U, args.pointerCount);
9473 ASSERT_EQ(0, args.pointerProperties[0].id);
9474 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9475 ASSERT_NO_FATAL_FAILURE(
9476 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9477
9478 // FINGER 1 DOWN
9479 processSlot(mapper, 1);
9480 processId(mapper, 2);
9481 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9482 processSync(mapper);
9483
9484 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009486 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009487 ASSERT_EQ(2U, args.pointerCount);
9488 ASSERT_EQ(0, args.pointerProperties[0].id);
9489 ASSERT_EQ(1, args.pointerProperties[1].id);
9490 ASSERT_NO_FATAL_FAILURE(
9491 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9492 ASSERT_NO_FATAL_FAILURE(
9493 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9494
9495 // FINGER 1 MOVE
9496 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9497 processSync(mapper);
9498
9499 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9500 // from move
9501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9503 ASSERT_NO_FATAL_FAILURE(
9504 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9505 ASSERT_NO_FATAL_FAILURE(
9506 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9507
9508 // FINGER 0 MOVE
9509 processSlot(mapper, 0);
9510 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9511 processSync(mapper);
9512
9513 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9516 ASSERT_NO_FATAL_FAILURE(
9517 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9518 ASSERT_NO_FATAL_FAILURE(
9519 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9520
9521 // BUTTON DOWN
9522 processKey(mapper, BTN_LEFT, 1);
9523 processSync(mapper);
9524
9525 // touchinputmapper design sends a move before button press
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9529 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9530
9531 // BUTTON UP
9532 processKey(mapper, BTN_LEFT, 0);
9533 processSync(mapper);
9534
9535 // touchinputmapper design sends a move after button release
9536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9540
9541 // FINGER 0 UP
9542 processId(mapper, -1);
9543 processSync(mapper);
9544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9545 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9546
9547 // FINGER 1 MOVE
9548 processSlot(mapper, 1);
9549 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9550 processSync(mapper);
9551
9552 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9555 ASSERT_EQ(1U, args.pointerCount);
9556 ASSERT_EQ(1, args.pointerProperties[0].id);
9557 ASSERT_NO_FATAL_FAILURE(
9558 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9559
9560 // FINGER 1 UP
9561 processId(mapper, -1);
9562 processKey(mapper, BTN_TOUCH, 0);
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9565 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9566
9567 // non captured touchpad should be a mouse source
9568 mFakePolicy->setPointerCapture(false);
9569 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9571 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9572}
9573
9574TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9575 std::shared_ptr<FakePointerController> fakePointerController =
9576 std::make_shared<FakePointerController>();
9577 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9578 fakePointerController->setPosition(0, 0);
9579 fakePointerController->setButtonState(0);
9580
9581 // prepare device and capture
9582 prepareDisplay(DISPLAY_ORIENTATION_0);
9583 prepareAxes(POSITION | ID | SLOT);
9584 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9585 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009586 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009587 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9588 // run uncaptured pointer tests - pushes out generic events
9589 // FINGER 0 DOWN
9590 processId(mapper, 3);
9591 processPosition(mapper, 100, 100);
9592 processKey(mapper, BTN_TOUCH, 1);
9593 processSync(mapper);
9594
9595 // start at (100,100), cursor should be at (0,0) * scale
9596 NotifyMotionArgs args;
9597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9599 ASSERT_NO_FATAL_FAILURE(
9600 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9601
9602 // FINGER 0 MOVE
9603 processPosition(mapper, 200, 200);
9604 processSync(mapper);
9605
9606 // compute scaling to help with touch position checking
9607 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9608 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9609 float scale =
9610 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9611
9612 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9614 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9616 0, 0, 0, 0, 0, 0, 0));
9617}
9618
9619TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9620 std::shared_ptr<FakePointerController> fakePointerController =
9621 std::make_shared<FakePointerController>();
9622
9623 prepareDisplay(DISPLAY_ORIENTATION_0);
9624 prepareAxes(POSITION | ID | SLOT);
9625 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009626 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009627 mFakePolicy->setPointerCapture(false);
9628 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9629
9630 // uncaptured touchpad should be a pointer device
9631 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9632
9633 // captured touchpad should be a touchpad device
9634 mFakePolicy->setPointerCapture(true);
9635 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9636 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9637}
9638
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009639// --- JoystickInputMapperTest ---
9640
9641class JoystickInputMapperTest : public InputMapperTest {
9642protected:
9643 static const int32_t RAW_X_MIN;
9644 static const int32_t RAW_X_MAX;
9645 static const int32_t RAW_Y_MIN;
9646 static const int32_t RAW_Y_MAX;
9647
9648 void SetUp() override {
9649 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9650 }
9651 void prepareAxes() {
9652 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9653 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9654 }
9655
9656 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9658 }
9659
9660 void processSync(JoystickInputMapper& mapper) {
9661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9662 }
9663
9664 void prepareVirtualDisplay(int32_t orientation) {
9665 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9666 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9667 NO_PORT, ViewportType::VIRTUAL);
9668 }
9669};
9670
9671const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9672const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9673const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9674const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9675
9676TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9677 prepareAxes();
9678 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9679
9680 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9681
9682 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9683
9684 // Send an axis event
9685 processAxis(mapper, ABS_X, 100);
9686 processSync(mapper);
9687
9688 NotifyMotionArgs args;
9689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9690 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9691
9692 // Send another axis event
9693 processAxis(mapper, ABS_Y, 100);
9694 processSync(mapper);
9695
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9697 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9698}
9699
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009700// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009701
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009702class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009703protected:
9704 static const char* DEVICE_NAME;
9705 static const char* DEVICE_LOCATION;
9706 static const int32_t DEVICE_ID;
9707 static const int32_t DEVICE_GENERATION;
9708 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009709 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009710 static const int32_t EVENTHUB_ID;
9711
9712 std::shared_ptr<FakeEventHub> mFakeEventHub;
9713 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009714 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009715 std::unique_ptr<InstrumentedInputReader> mReader;
9716 std::shared_ptr<InputDevice> mDevice;
9717
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009718 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009719 mFakeEventHub = std::make_unique<FakeEventHub>();
9720 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009721 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009722 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009723 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009724 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9725 }
9726
9727 void SetUp() override { SetUp(DEVICE_CLASSES); }
9728
9729 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009730 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009731 mFakePolicy.clear();
9732 }
9733
9734 void configureDevice(uint32_t changes) {
9735 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9736 mReader->requestRefreshConfiguration(changes);
9737 mReader->loopOnce();
9738 }
9739 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9740 }
9741
9742 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9743 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009744 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009745 InputDeviceIdentifier identifier;
9746 identifier.name = name;
9747 identifier.location = location;
9748 std::shared_ptr<InputDevice> device =
9749 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9750 identifier);
9751 mReader->pushNextDevice(device);
9752 mFakeEventHub->addDevice(eventHubId, name, classes);
9753 mReader->loopOnce();
9754 return device;
9755 }
9756
9757 template <class T, typename... Args>
9758 T& addControllerAndConfigure(Args... args) {
9759 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9760
9761 return controller;
9762 }
9763};
9764
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009765const char* PeripheralControllerTest::DEVICE_NAME = "device";
9766const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9767const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9768const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9769const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009770const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9771 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009772const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009773
9774// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009775class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009776protected:
9777 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009778 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009779 }
9780};
9781
9782TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009783 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009784
9785 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9786 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9787}
9788
9789TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009790 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009791
9792 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9793 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9794}
9795
9796// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009797class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009798protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009799 void SetUp() override {
9800 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9801 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009802};
9803
Chris Ye85758332021-05-16 23:05:17 -07009804TEST_F(LightControllerTest, MonoLight) {
9805 RawLightInfo infoMono = {.id = 1,
9806 .name = "Mono",
9807 .maxBrightness = 255,
9808 .flags = InputLightClass::BRIGHTNESS,
9809 .path = ""};
9810 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009811
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009812 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009813 InputDeviceInfo info;
9814 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009815 std::vector<InputDeviceLightInfo> lights = info.getLights();
9816 ASSERT_EQ(1U, lights.size());
9817 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009818
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009819 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9820 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009821}
9822
9823TEST_F(LightControllerTest, RGBLight) {
9824 RawLightInfo infoRed = {.id = 1,
9825 .name = "red",
9826 .maxBrightness = 255,
9827 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9828 .path = ""};
9829 RawLightInfo infoGreen = {.id = 2,
9830 .name = "green",
9831 .maxBrightness = 255,
9832 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9833 .path = ""};
9834 RawLightInfo infoBlue = {.id = 3,
9835 .name = "blue",
9836 .maxBrightness = 255,
9837 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9838 .path = ""};
9839 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9840 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9841 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9842
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009843 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009844 InputDeviceInfo info;
9845 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009846 std::vector<InputDeviceLightInfo> lights = info.getLights();
9847 ASSERT_EQ(1U, lights.size());
9848 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009849
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009850 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9851 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009852}
9853
9854TEST_F(LightControllerTest, MultiColorRGBLight) {
9855 RawLightInfo infoColor = {.id = 1,
9856 .name = "red",
9857 .maxBrightness = 255,
9858 .flags = InputLightClass::BRIGHTNESS |
9859 InputLightClass::MULTI_INTENSITY |
9860 InputLightClass::MULTI_INDEX,
9861 .path = ""};
9862
9863 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9864
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009865 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009866 InputDeviceInfo info;
9867 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009868 std::vector<InputDeviceLightInfo> lights = info.getLights();
9869 ASSERT_EQ(1U, lights.size());
9870 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009871
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009872 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9873 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009874}
9875
9876TEST_F(LightControllerTest, PlayerIdLight) {
9877 RawLightInfo info1 = {.id = 1,
9878 .name = "player1",
9879 .maxBrightness = 255,
9880 .flags = InputLightClass::BRIGHTNESS,
9881 .path = ""};
9882 RawLightInfo info2 = {.id = 2,
9883 .name = "player2",
9884 .maxBrightness = 255,
9885 .flags = InputLightClass::BRIGHTNESS,
9886 .path = ""};
9887 RawLightInfo info3 = {.id = 3,
9888 .name = "player3",
9889 .maxBrightness = 255,
9890 .flags = InputLightClass::BRIGHTNESS,
9891 .path = ""};
9892 RawLightInfo info4 = {.id = 4,
9893 .name = "player4",
9894 .maxBrightness = 255,
9895 .flags = InputLightClass::BRIGHTNESS,
9896 .path = ""};
9897 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9898 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9899 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9900 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9901
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009902 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009903 InputDeviceInfo info;
9904 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009905 std::vector<InputDeviceLightInfo> lights = info.getLights();
9906 ASSERT_EQ(1U, lights.size());
9907 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009908
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009909 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9910 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9911 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009912}
9913
Michael Wrightd02c5b62014-02-10 15:10:22 -08009914} // namespace android