blob: 575d950c9b826ce1148348d6c88a6f6e5af2e547 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000059static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080060static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000061static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_WIDTH = 480;
63static constexpr int32_t DISPLAY_HEIGHT = 800;
64static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
65static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
66static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070067static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070068static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080069
arthurhungcc7f9802020-04-30 17:55:40 +080070static constexpr int32_t FIRST_SLOT = 0;
71static constexpr int32_t SECOND_SLOT = 1;
72static constexpr int32_t THIRD_SLOT = 2;
73static constexpr int32_t INVALID_TRACKING_ID = -1;
74static constexpr int32_t FIRST_TRACKING_ID = 0;
75static constexpr int32_t SECOND_TRACKING_ID = 1;
76static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080077static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080078static constexpr int32_t BATTERY_STATUS = 4;
79static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080080static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
81static constexpr int32_t LIGHT_COLOR = 0x7F448866;
82static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080083
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080084static constexpr int32_t ACTION_POINTER_0_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_DOWN =
89 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92
Michael Wrightd02c5b62014-02-10 15:10:22 -080093// Error tolerance for floating point assertions.
94static const float EPSILON = 0.001f;
95
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000096using ::testing::AllOf;
97
98MATCHER_P(WithAction, action, "InputEvent with specified action") {
99 return arg.action == action;
100}
101
102MATCHER_P(WithSource, source, "InputEvent with specified source") {
103 return arg.source == source;
104}
105
106MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
107 return arg.displayId == displayId;
108}
109
110MATCHER_P2(WithCoords, x, y, "MotionEvent with specified action") {
111 return arg.pointerCoords[0].getX() == x && arg.pointerCoords[0].getY();
112}
113
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114template<typename T>
115static inline T min(T a, T b) {
116 return a < b ? a : b;
117}
118
119static inline float avg(float x, float y) {
120 return (x + y) / 2;
121}
122
Chris Ye3fdbfef2021-01-06 18:45:18 -0800123// Mapping for light color name and the light color
124const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
125 {"green", LightColor::GREEN},
126 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700128static int32_t getInverseRotation(int32_t orientation) {
129 switch (orientation) {
130 case DISPLAY_ORIENTATION_90:
131 return DISPLAY_ORIENTATION_270;
132 case DISPLAY_ORIENTATION_270:
133 return DISPLAY_ORIENTATION_90;
134 default:
135 return orientation;
136 }
137}
138
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800139static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
140 InputDeviceInfo info;
141 mapper.populateDeviceInfo(&info);
142
143 const InputDeviceInfo::MotionRange* motionRange =
144 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
145 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
146}
147
148static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
149 InputDeviceInfo info;
150 mapper.populateDeviceInfo(&info);
151
152 const InputDeviceInfo::MotionRange* motionRange =
153 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
154 ASSERT_EQ(nullptr, motionRange);
155}
156
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157// --- FakePointerController ---
158
159class FakePointerController : public PointerControllerInterface {
160 bool mHaveBounds;
161 float mMinX, mMinY, mMaxX, mMaxY;
162 float mX, mY;
163 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800164 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166public:
167 FakePointerController() :
168 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800169 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 }
171
Michael Wright17db18e2020-06-26 20:51:44 +0100172 virtual ~FakePointerController() {}
173
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174 void setBounds(float minX, float minY, float maxX, float maxY) {
175 mHaveBounds = true;
176 mMinX = minX;
177 mMinY = minY;
178 mMaxX = maxX;
179 mMaxY = maxY;
180 }
181
Chris Yea52ade12020-08-27 16:49:20 -0700182 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183 mX = x;
184 mY = y;
185 }
186
Chris Yea52ade12020-08-27 16:49:20 -0700187 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
Chris Yea52ade12020-08-27 16:49:20 -0700191 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 *outX = mX;
193 *outY = mY;
194 }
195
Chris Yea52ade12020-08-27 16:49:20 -0700196 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800197
Chris Yea52ade12020-08-27 16:49:20 -0700198 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800199 mDisplayId = viewport.displayId;
200 }
201
Arthur Hung7c645402019-01-25 17:45:42 +0800202 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
203 return mSpotsByDisplay;
204 }
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206private:
Chris Yea52ade12020-08-27 16:49:20 -0700207 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 *outMinX = mMinX;
209 *outMinY = mMinY;
210 *outMaxX = mMaxX;
211 *outMaxY = mMaxY;
212 return mHaveBounds;
213 }
214
Chris Yea52ade12020-08-27 16:49:20 -0700215 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 mX += deltaX;
217 if (mX < mMinX) mX = mMinX;
218 if (mX > mMaxX) mX = mMaxX;
219 mY += deltaY;
220 if (mY < mMinY) mY = mMinY;
221 if (mY > mMaxY) mY = mMaxY;
222 }
223
Chris Yea52ade12020-08-27 16:49:20 -0700224 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225
Chris Yea52ade12020-08-27 16:49:20 -0700226 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
Chris Yea52ade12020-08-27 16:49:20 -0700228 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229
Chris Yea52ade12020-08-27 16:49:20 -0700230 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
231 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800232 std::vector<int32_t> newSpots;
233 // Add spots for fingers that are down.
234 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
235 uint32_t id = idBits.clearFirstMarkedBit();
236 newSpots.push_back(id);
237 }
238
239 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240 }
241
Prabir Pradhan197e0862022-07-01 14:28:00 +0000242 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800243
244 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245};
246
247
248// --- FakeInputReaderPolicy ---
249
250class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700251 std::mutex mLock;
252 std::condition_variable mDevicesChangedCondition;
253
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000255 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700256 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
257 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100258 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700259 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260
261protected:
Chris Yea52ade12020-08-27 16:49:20 -0700262 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263
264public:
265 FakeInputReaderPolicy() {
266 }
267
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700268 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800269 waitForInputDevices([](bool devicesChanged) {
270 if (!devicesChanged) {
271 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
272 }
273 });
274 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700275
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800276 void assertInputDevicesNotChanged() {
277 waitForInputDevices([](bool devicesChanged) {
278 if (devicesChanged) {
279 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
280 }
281 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700282 }
283
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700284 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100285 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100286 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700287 }
288
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700289 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
290 return mConfig.getDisplayViewportByUniqueId(uniqueId);
291 }
292 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
293 return mConfig.getDisplayViewportByType(type);
294 }
295
296 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
297 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700298 }
299
Prabir Pradhan5632d622021-09-06 07:57:20 -0700300 void addDisplayViewport(DisplayViewport viewport) {
301 mViewports.push_back(std::move(viewport));
302 mConfig.setDisplayViewports(mViewports);
303 }
304
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700305 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000306 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700307 std::optional<uint8_t> physicalPort, ViewportType type) {
308 const bool isRotated =
309 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
310 DisplayViewport v;
311 v.displayId = displayId;
312 v.orientation = orientation;
313 v.logicalLeft = 0;
314 v.logicalTop = 0;
315 v.logicalRight = isRotated ? height : width;
316 v.logicalBottom = isRotated ? width : height;
317 v.physicalLeft = 0;
318 v.physicalTop = 0;
319 v.physicalRight = isRotated ? height : width;
320 v.physicalBottom = isRotated ? width : height;
321 v.deviceWidth = isRotated ? height : width;
322 v.deviceHeight = isRotated ? width : height;
323 v.isActive = isActive;
324 v.uniqueId = uniqueId;
325 v.physicalPort = physicalPort;
326 v.type = type;
327
328 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 }
330
Arthur Hung6cd19a42019-08-30 19:04:12 +0800331 bool updateViewport(const DisplayViewport& viewport) {
332 size_t count = mViewports.size();
333 for (size_t i = 0; i < count; i++) {
334 const DisplayViewport& currentViewport = mViewports[i];
335 if (currentViewport.displayId == viewport.displayId) {
336 mViewports[i] = viewport;
337 mConfig.setDisplayViewports(mViewports);
338 return true;
339 }
340 }
341 // no viewport found.
342 return false;
343 }
344
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100345 void addExcludedDeviceName(const std::string& deviceName) {
346 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700349 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
350 mConfig.portAssociations.insert({inputPort, displayPort});
351 }
352
Christine Franks1ba71cc2021-04-07 14:37:42 -0700353 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
354 const std::string& displayUniqueId) {
355 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
356 }
357
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000358 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700359
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000360 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700361
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000362 void setPointerController(std::shared_ptr<FakePointerController> controller) {
363 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 }
365
366 const InputReaderConfiguration* getReaderConfiguration() const {
367 return &mConfig;
368 }
369
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800370 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 return mInputDevices;
372 }
373
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100374 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700375 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700376 return transform;
377 }
378
379 void setTouchAffineTransformation(const TouchAffineTransformation t) {
380 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800381 }
382
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000383 PointerCaptureRequest setPointerCapture(bool enabled) {
384 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
385 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800386 }
387
Arthur Hung7c645402019-01-25 17:45:42 +0800388 void setShowTouches(bool enabled) {
389 mConfig.showTouches = enabled;
390 }
391
Garfield Tan888a6a42020-01-09 11:39:16 -0800392 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
393 mConfig.defaultPointerDisplayId = pointerDisplayId;
394 }
395
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800396 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
397
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000398 void setVelocityControlParams(const VelocityControlParameters& params) {
399 mConfig.pointerVelocityControlParameters = params;
400 mConfig.wheelVelocityControlParameters = params;
401 }
402
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000404 uint32_t mNextPointerCaptureSequenceNumber = 0;
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 *outConfig = mConfig;
408 }
409
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000410 std::shared_ptr<PointerControllerInterface> obtainPointerController(
411 int32_t /*deviceId*/) override {
412 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413 }
414
Chris Yea52ade12020-08-27 16:49:20 -0700415 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700416 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700418 mInputDevicesChanged = true;
419 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 }
421
Chris Yea52ade12020-08-27 16:49:20 -0700422 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
423 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700424 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425 }
426
Chris Yea52ade12020-08-27 16:49:20 -0700427 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800428
429 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
430 std::unique_lock<std::mutex> lock(mLock);
431 base::ScopedLockAssertion assumeLocked(mLock);
432
433 const bool devicesChanged =
434 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
435 return mInputDevicesChanged;
436 });
437 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
438 mInputDevicesChanged = false;
439 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440};
441
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442// --- FakeEventHub ---
443
444class FakeEventHub : public EventHubInterface {
445 struct KeyInfo {
446 int32_t keyCode;
447 uint32_t flags;
448 };
449
Chris Yef59a2f42020-10-16 12:55:26 -0700450 struct SensorInfo {
451 InputDeviceSensorType sensorType;
452 int32_t sensorDataIndex;
453 };
454
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 struct Device {
456 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700457 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 PropertyMap configuration;
459 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
460 KeyedVector<int, bool> relativeAxes;
461 KeyedVector<int32_t, int32_t> keyCodeStates;
462 KeyedVector<int32_t, int32_t> scanCodeStates;
463 KeyedVector<int32_t, int32_t> switchStates;
464 KeyedVector<int32_t, int32_t> absoluteAxisValue;
465 KeyedVector<int32_t, KeyInfo> keysByScanCode;
466 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
467 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100468 // fake mapping which would normally come from keyCharacterMap
469 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700470 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
471 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800472 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700473 bool enabled;
474
475 status_t enable() {
476 enabled = true;
477 return OK;
478 }
479
480 status_t disable() {
481 enabled = false;
482 return OK;
483 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700485 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486 };
487
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700488 std::mutex mLock;
489 std::condition_variable mEventsCondition;
490
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100492 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000493 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600494 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000495 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800496 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
497 // Simulates a device light brightness, from light id to light brightness.
498 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
499 // Simulates a device light intensities, from light id to light intensities map.
500 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
501 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700503public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 virtual ~FakeEventHub() {
505 for (size_t i = 0; i < mDevices.size(); i++) {
506 delete mDevices.valueAt(i);
507 }
508 }
509
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 FakeEventHub() { }
511
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700512 void addDevice(int32_t deviceId, const std::string& name,
513 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 Device* device = new Device(classes);
515 device->identifier.name = name;
516 mDevices.add(deviceId, device);
517
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000518 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 }
520
521 void removeDevice(int32_t deviceId) {
522 delete mDevices.valueFor(deviceId);
523 mDevices.removeItem(deviceId);
524
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000525 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526 }
527
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700528 bool isDeviceEnabled(int32_t deviceId) {
529 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700530 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700531 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
532 return false;
533 }
534 return device->enabled;
535 }
536
537 status_t enableDevice(int32_t deviceId) {
538 status_t result;
539 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700540 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700541 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
542 return BAD_VALUE;
543 }
544 if (device->enabled) {
545 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
546 return OK;
547 }
548 result = device->enable();
549 return result;
550 }
551
552 status_t disableDevice(int32_t deviceId) {
553 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700554 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700555 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
556 return BAD_VALUE;
557 }
558 if (!device->enabled) {
559 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
560 return OK;
561 }
562 return device->disable();
563 }
564
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000566 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 }
568
569 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
570 Device* device = getDevice(deviceId);
571 device->configuration.addProperty(key, value);
572 }
573
574 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
575 Device* device = getDevice(deviceId);
576 device->configuration.addAll(configuration);
577 }
578
579 void addAbsoluteAxis(int32_t deviceId, int axis,
580 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
581 Device* device = getDevice(deviceId);
582
583 RawAbsoluteAxisInfo info;
584 info.valid = true;
585 info.minValue = minValue;
586 info.maxValue = maxValue;
587 info.flat = flat;
588 info.fuzz = fuzz;
589 info.resolution = resolution;
590 device->absoluteAxes.add(axis, info);
591 }
592
593 void addRelativeAxis(int32_t deviceId, int32_t axis) {
594 Device* device = getDevice(deviceId);
595 device->relativeAxes.add(axis, true);
596 }
597
598 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
599 Device* device = getDevice(deviceId);
600 device->keyCodeStates.replaceValueFor(keyCode, state);
601 }
602
603 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
604 Device* device = getDevice(deviceId);
605 device->scanCodeStates.replaceValueFor(scanCode, state);
606 }
607
608 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
609 Device* device = getDevice(deviceId);
610 device->switchStates.replaceValueFor(switchCode, state);
611 }
612
613 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
614 Device* device = getDevice(deviceId);
615 device->absoluteAxisValue.replaceValueFor(axis, value);
616 }
617
618 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
619 int32_t keyCode, uint32_t flags) {
620 Device* device = getDevice(deviceId);
621 KeyInfo info;
622 info.keyCode = keyCode;
623 info.flags = flags;
624 if (scanCode) {
625 device->keysByScanCode.add(scanCode, info);
626 }
627 if (usageCode) {
628 device->keysByUsageCode.add(usageCode, info);
629 }
630 }
631
Philip Junker4af3b3d2021-12-14 10:36:55 +0100632 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
633 Device* device = getDevice(deviceId);
634 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
635 }
636
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 void addLed(int32_t deviceId, int32_t led, bool initialState) {
638 Device* device = getDevice(deviceId);
639 device->leds.add(led, initialState);
640 }
641
Chris Yef59a2f42020-10-16 12:55:26 -0700642 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
643 int32_t sensorDataIndex) {
644 Device* device = getDevice(deviceId);
645 SensorInfo info;
646 info.sensorType = sensorType;
647 info.sensorDataIndex = sensorDataIndex;
648 device->sensorsByAbsCode.emplace(absCode, info);
649 }
650
651 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
652 Device* device = getDevice(deviceId);
653 typename BitArray<MSC_MAX>::Buffer buffer;
654 buffer[mscEvent / 32] = 1 << mscEvent % 32;
655 device->mscBitmask.loadFromBuffer(buffer);
656 }
657
Chris Ye3fdbfef2021-01-06 18:45:18 -0800658 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
659 mRawLightInfos.emplace(rawId, std::move(info));
660 }
661
662 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
663 mLightBrightness.emplace(rawId, brightness);
664 }
665
666 void fakeLightIntensities(int32_t rawId,
667 const std::unordered_map<LightColor, int32_t> intensities) {
668 mLightIntensities.emplace(rawId, std::move(intensities));
669 }
670
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 bool getLedState(int32_t deviceId, int32_t led) {
672 Device* device = getDevice(deviceId);
673 return device->leds.valueFor(led);
674 }
675
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100676 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 return mExcludedDevices;
678 }
679
680 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
681 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800682 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 }
684
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000685 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
686 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700687 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 RawEvent event;
689 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000690 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 event.deviceId = deviceId;
692 event.type = type;
693 event.code = code;
694 event.value = value;
695 mEvents.push_back(event);
696
697 if (type == EV_ABS) {
698 setAbsoluteAxisValue(deviceId, code, value);
699 }
700 }
701
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600702 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
703 std::vector<TouchVideoFrame>> videoFrames) {
704 mVideoFrames = std::move(videoFrames);
705 }
706
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700708 std::unique_lock<std::mutex> lock(mLock);
709 base::ScopedLockAssertion assumeLocked(mLock);
710 const bool queueIsEmpty =
711 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
712 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
713 if (!queueIsEmpty) {
714 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
715 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 }
717
718private:
719 Device* getDevice(int32_t deviceId) const {
720 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100721 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 }
723
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700724 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700726 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 }
728
Chris Yea52ade12020-08-27 16:49:20 -0700729 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 Device* device = getDevice(deviceId);
731 return device ? device->identifier : InputDeviceIdentifier();
732 }
733
Chris Yea52ade12020-08-27 16:49:20 -0700734 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735
Chris Yea52ade12020-08-27 16:49:20 -0700736 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 Device* device = getDevice(deviceId);
738 if (device) {
739 *outConfiguration = device->configuration;
740 }
741 }
742
Chris Yea52ade12020-08-27 16:49:20 -0700743 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
744 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800746 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 ssize_t index = device->absoluteAxes.indexOfKey(axis);
748 if (index >= 0) {
749 *outAxisInfo = device->absoluteAxes.valueAt(index);
750 return OK;
751 }
752 }
753 outAxisInfo->clear();
754 return -1;
755 }
756
Chris Yea52ade12020-08-27 16:49:20 -0700757 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 Device* device = getDevice(deviceId);
759 if (device) {
760 return device->relativeAxes.indexOfKey(axis) >= 0;
761 }
762 return false;
763 }
764
Chris Yea52ade12020-08-27 16:49:20 -0700765 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766
Chris Yef59a2f42020-10-16 12:55:26 -0700767 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
768 Device* device = getDevice(deviceId);
769 if (device) {
770 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
771 }
772 return false;
773 }
774
Chris Yea52ade12020-08-27 16:49:20 -0700775 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
776 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 Device* device = getDevice(deviceId);
778 if (device) {
779 const KeyInfo* key = getKey(device, scanCode, usageCode);
780 if (key) {
781 if (outKeycode) {
782 *outKeycode = key->keyCode;
783 }
784 if (outFlags) {
785 *outFlags = key->flags;
786 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700787 if (outMetaState) {
788 *outMetaState = metaState;
789 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 return OK;
791 }
792 }
793 return NAME_NOT_FOUND;
794 }
795
796 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
797 if (usageCode) {
798 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
799 if (index >= 0) {
800 return &device->keysByUsageCode.valueAt(index);
801 }
802 }
803 if (scanCode) {
804 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
805 if (index >= 0) {
806 return &device->keysByScanCode.valueAt(index);
807 }
808 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700809 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
Chris Yea52ade12020-08-27 16:49:20 -0700812 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813
Chris Yef59a2f42020-10-16 12:55:26 -0700814 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
815 int32_t absCode) {
816 Device* device = getDevice(deviceId);
817 if (!device) {
818 return Errorf("Sensor device not found.");
819 }
820 auto it = device->sensorsByAbsCode.find(absCode);
821 if (it == device->sensorsByAbsCode.end()) {
822 return Errorf("Sensor map not found.");
823 }
824 const SensorInfo& info = it->second;
825 return std::make_pair(info.sensorType, info.sensorDataIndex);
826 }
827
Chris Yea52ade12020-08-27 16:49:20 -0700828 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 mExcludedDevices = devices;
830 }
831
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000832 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
833 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000835 const size_t filledSize = std::min(mEvents.size(), bufferSize);
836 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
837
838 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700839 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000840 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800841 }
842
Chris Yea52ade12020-08-27 16:49:20 -0700843 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600844 auto it = mVideoFrames.find(deviceId);
845 if (it != mVideoFrames.end()) {
846 std::vector<TouchVideoFrame> frames = std::move(it->second);
847 mVideoFrames.erase(deviceId);
848 return frames;
849 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800850 return {};
851 }
852
Chris Yea52ade12020-08-27 16:49:20 -0700853 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 Device* device = getDevice(deviceId);
855 if (device) {
856 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
857 if (index >= 0) {
858 return device->scanCodeStates.valueAt(index);
859 }
860 }
861 return AKEY_STATE_UNKNOWN;
862 }
863
Chris Yea52ade12020-08-27 16:49:20 -0700864 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865 Device* device = getDevice(deviceId);
866 if (device) {
867 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
868 if (index >= 0) {
869 return device->keyCodeStates.valueAt(index);
870 }
871 }
872 return AKEY_STATE_UNKNOWN;
873 }
874
Chris Yea52ade12020-08-27 16:49:20 -0700875 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 Device* device = getDevice(deviceId);
877 if (device) {
878 ssize_t index = device->switchStates.indexOfKey(sw);
879 if (index >= 0) {
880 return device->switchStates.valueAt(index);
881 }
882 }
883 return AKEY_STATE_UNKNOWN;
884 }
885
Chris Yea52ade12020-08-27 16:49:20 -0700886 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
887 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800888 Device* device = getDevice(deviceId);
889 if (device) {
890 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
891 if (index >= 0) {
892 *outValue = device->absoluteAxisValue.valueAt(index);
893 return OK;
894 }
895 }
896 *outValue = 0;
897 return -1;
898 }
899
Philip Junker4af3b3d2021-12-14 10:36:55 +0100900 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
901 Device* device = getDevice(deviceId);
902 if (!device) {
903 return AKEYCODE_UNKNOWN;
904 }
905 auto it = device->keyCodeMapping.find(locationKeyCode);
906 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
907 }
908
Chris Yea52ade12020-08-27 16:49:20 -0700909 // Return true if the device has non-empty key layout.
910 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
911 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 bool result = false;
913 Device* device = getDevice(deviceId);
914 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700915 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 for (size_t i = 0; i < numCodes; i++) {
917 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
918 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
919 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920 }
921 }
922 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
923 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
924 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 }
926 }
927 }
928 }
929 return result;
930 }
931
Chris Yea52ade12020-08-27 16:49:20 -0700932 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 Device* device = getDevice(deviceId);
934 if (device) {
935 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
936 return index >= 0;
937 }
938 return false;
939 }
940
Arthur Hungcb40a002021-08-03 14:31:01 +0000941 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
942 Device* device = getDevice(deviceId);
943 if (!device) {
944 return false;
945 }
946 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
947 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
948 return true;
949 }
950 }
951 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
952 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
953 return true;
954 }
955 }
956 return false;
957 }
958
Chris Yea52ade12020-08-27 16:49:20 -0700959 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 Device* device = getDevice(deviceId);
961 return device && device->leds.indexOfKey(led) >= 0;
962 }
963
Chris Yea52ade12020-08-27 16:49:20 -0700964 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 Device* device = getDevice(deviceId);
966 if (device) {
967 ssize_t index = device->leds.indexOfKey(led);
968 if (index >= 0) {
969 device->leds.replaceValueAt(led, on);
970 } else {
971 ADD_FAILURE()
972 << "Attempted to set the state of an LED that the EventHub declared "
973 "was not present. led=" << led;
974 }
975 }
976 }
977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void getVirtualKeyDefinitions(
979 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 outVirtualKeys.clear();
981
982 Device* device = getDevice(deviceId);
983 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800984 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 }
986 }
987
Chris Yea52ade12020-08-27 16:49:20 -0700988 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700989 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 return false;
994 }
995
Chris Yea52ade12020-08-27 16:49:20 -0700996 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997
Chris Yea52ade12020-08-27 16:49:20 -0700998 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999
Chris Ye87143712020-11-10 05:05:58 +00001000 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
1001
Chris Yee2b1e5c2021-03-10 22:45:12 -08001002 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1003 return BATTERY_CAPACITY;
1004 }
Kim Low03ea0352020-11-06 12:45:07 -08001005
Chris Yee2b1e5c2021-03-10 22:45:12 -08001006 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1007 return BATTERY_STATUS;
1008 }
1009
Andy Chen22c330c2022-08-29 20:07:10 -04001010 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) override {
1011 return {DEFAULT_BATTERY};
1012 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001013
1014 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
1015 return std::nullopt;
1016 }
Kim Low03ea0352020-11-06 12:45:07 -08001017
Chris Ye3fdbfef2021-01-06 18:45:18 -08001018 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1019 std::vector<int32_t> ids;
1020 for (const auto& [rawId, info] : mRawLightInfos) {
1021 ids.push_back(rawId);
1022 }
1023 return ids;
1024 }
1025
1026 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1027 auto it = mRawLightInfos.find(lightId);
1028 if (it == mRawLightInfos.end()) {
1029 return std::nullopt;
1030 }
1031 return it->second;
1032 }
1033
1034 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1035 mLightBrightness.emplace(lightId, brightness);
1036 }
1037
1038 void setLightIntensities(int32_t deviceId, int32_t lightId,
1039 std::unordered_map<LightColor, int32_t> intensities) override {
1040 mLightIntensities.emplace(lightId, intensities);
1041 };
1042
1043 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1044 auto lightIt = mLightBrightness.find(lightId);
1045 if (lightIt == mLightBrightness.end()) {
1046 return std::nullopt;
1047 }
1048 return lightIt->second;
1049 }
1050
1051 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1052 int32_t deviceId, int32_t lightId) override {
1053 auto lightIt = mLightIntensities.find(lightId);
1054 if (lightIt == mLightIntensities.end()) {
1055 return std::nullopt;
1056 }
1057 return lightIt->second;
1058 };
1059
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001060 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061 return false;
1062 }
1063
Chris Yea52ade12020-08-27 16:49:20 -07001064 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Chris Yea52ade12020-08-27 16:49:20 -07001066 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067
Chris Yea52ade12020-08-27 16:49:20 -07001068 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069
Chris Yea52ade12020-08-27 16:49:20 -07001070 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071};
1072
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073// --- FakeInputMapper ---
1074
1075class FakeInputMapper : public InputMapper {
1076 uint32_t mSources;
1077 int32_t mKeyboardType;
1078 int32_t mMetaState;
1079 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1080 KeyedVector<int32_t, int32_t> mScanCodeStates;
1081 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001082 // fake mapping which would normally come from keyCharacterMap
1083 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001084 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001086 std::mutex mLock;
1087 std::condition_variable mStateChangedCondition;
1088 bool mConfigureWasCalled GUARDED_BY(mLock);
1089 bool mResetWasCalled GUARDED_BY(mLock);
1090 bool mProcessWasCalled GUARDED_BY(mLock);
1091 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092
Arthur Hungc23540e2018-11-29 20:42:11 +08001093 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001095 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1096 : InputMapper(deviceContext),
1097 mSources(sources),
1098 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001100 mConfigureWasCalled(false),
1101 mResetWasCalled(false),
1102 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103
Chris Yea52ade12020-08-27 16:49:20 -07001104 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105
1106 void setKeyboardType(int32_t keyboardType) {
1107 mKeyboardType = keyboardType;
1108 }
1109
1110 void setMetaState(int32_t metaState) {
1111 mMetaState = metaState;
1112 }
1113
1114 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001115 std::unique_lock<std::mutex> lock(mLock);
1116 base::ScopedLockAssertion assumeLocked(mLock);
1117 const bool configureCalled =
1118 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1119 return mConfigureWasCalled;
1120 });
1121 if (!configureCalled) {
1122 FAIL() << "Expected configure() to have been called.";
1123 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001124 mConfigureWasCalled = false;
1125 }
1126
1127 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001128 std::unique_lock<std::mutex> lock(mLock);
1129 base::ScopedLockAssertion assumeLocked(mLock);
1130 const bool resetCalled =
1131 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1132 return mResetWasCalled;
1133 });
1134 if (!resetCalled) {
1135 FAIL() << "Expected reset() to have been called.";
1136 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137 mResetWasCalled = false;
1138 }
1139
Yi Kong9b14ac62018-07-17 13:48:38 -07001140 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001141 std::unique_lock<std::mutex> lock(mLock);
1142 base::ScopedLockAssertion assumeLocked(mLock);
1143 const bool processCalled =
1144 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1145 return mProcessWasCalled;
1146 });
1147 if (!processCalled) {
1148 FAIL() << "Expected process() to have been called.";
1149 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150 if (outLastEvent) {
1151 *outLastEvent = mLastEvent;
1152 }
1153 mProcessWasCalled = false;
1154 }
1155
1156 void setKeyCodeState(int32_t keyCode, int32_t state) {
1157 mKeyCodeStates.replaceValueFor(keyCode, state);
1158 }
1159
1160 void setScanCodeState(int32_t scanCode, int32_t state) {
1161 mScanCodeStates.replaceValueFor(scanCode, state);
1162 }
1163
1164 void setSwitchState(int32_t switchCode, int32_t state) {
1165 mSwitchStates.replaceValueFor(switchCode, state);
1166 }
1167
1168 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001169 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001170 }
1171
Philip Junker4af3b3d2021-12-14 10:36:55 +01001172 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1173 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1174 }
1175
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001177 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178
Chris Yea52ade12020-08-27 16:49:20 -07001179 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 InputMapper::populateDeviceInfo(deviceInfo);
1181
1182 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1183 deviceInfo->setKeyboardType(mKeyboardType);
1184 }
1185 }
1186
Chris Yea52ade12020-08-27 16:49:20 -07001187 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001188 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001190
1191 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001192 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001193 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1194 mViewport = config->getDisplayViewportByPort(*displayPort);
1195 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001196
1197 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001198 }
1199
Chris Yea52ade12020-08-27 16:49:20 -07001200 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001201 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001203 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 }
1205
Chris Yea52ade12020-08-27 16:49:20 -07001206 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001207 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 mLastEvent = *rawEvent;
1209 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001210 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 }
1212
Chris Yea52ade12020-08-27 16:49:20 -07001213 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1215 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1216 }
1217
Philip Junker4af3b3d2021-12-14 10:36:55 +01001218 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1219 auto it = mKeyCodeMapping.find(locationKeyCode);
1220 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1221 }
1222
Chris Yea52ade12020-08-27 16:49:20 -07001223 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1225 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1226 }
1227
Chris Yea52ade12020-08-27 16:49:20 -07001228 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001229 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1230 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1231 }
1232
Chris Yea52ade12020-08-27 16:49:20 -07001233 // Return true if the device has non-empty key layout.
1234 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1235 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001236 for (size_t i = 0; i < numCodes; i++) {
1237 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1238 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1239 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001240 }
1241 }
1242 }
Chris Yea52ade12020-08-27 16:49:20 -07001243 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244 return result;
1245 }
1246
1247 virtual int32_t getMetaState() {
1248 return mMetaState;
1249 }
1250
1251 virtual void fadePointer() {
1252 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001253
1254 virtual std::optional<int32_t> getAssociatedDisplay() {
1255 if (mViewport) {
1256 return std::make_optional(mViewport->displayId);
1257 }
1258 return std::nullopt;
1259 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001260};
1261
1262
1263// --- InstrumentedInputReader ---
1264
1265class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001266 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267
1268public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001269 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1270 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001271 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001272 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001274 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001276 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001278 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001279 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280 InputDeviceIdentifier identifier;
1281 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001282 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001284 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001285 }
1286
Prabir Pradhan28efc192019-11-05 01:10:04 +00001287 // Make the protected loopOnce method accessible to tests.
1288 using InputReader::loopOnce;
1289
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001291 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1292 const InputDeviceIdentifier& identifier)
1293 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001294 if (!mNextDevices.empty()) {
1295 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1296 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001297 return device;
1298 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001299 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001300 }
1301
arthurhungdcef2dc2020-08-11 14:47:50 +08001302 // --- FakeInputReaderContext ---
1303 class FakeInputReaderContext : public ContextImpl {
1304 int32_t mGlobalMetaState;
1305 bool mUpdateGlobalMetaStateWasCalled;
1306 int32_t mGeneration;
1307
1308 public:
1309 FakeInputReaderContext(InputReader* reader)
1310 : ContextImpl(reader),
1311 mGlobalMetaState(0),
1312 mUpdateGlobalMetaStateWasCalled(false),
1313 mGeneration(1) {}
1314
1315 virtual ~FakeInputReaderContext() {}
1316
1317 void assertUpdateGlobalMetaStateWasCalled() {
1318 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1319 << "Expected updateGlobalMetaState() to have been called.";
1320 mUpdateGlobalMetaStateWasCalled = false;
1321 }
1322
1323 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1324
1325 uint32_t getGeneration() { return mGeneration; }
1326
1327 void updateGlobalMetaState() override {
1328 mUpdateGlobalMetaStateWasCalled = true;
1329 ContextImpl::updateGlobalMetaState();
1330 }
1331
1332 int32_t getGlobalMetaState() override {
1333 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1334 }
1335
1336 int32_t bumpGeneration() override {
1337 mGeneration = ContextImpl::bumpGeneration();
1338 return mGeneration;
1339 }
1340 } mFakeContext;
1341
Michael Wrightd02c5b62014-02-10 15:10:22 -08001342 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001343
1344public:
1345 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001346};
1347
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348// --- InputReaderPolicyTest ---
1349class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001350protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001351 sp<FakeInputReaderPolicy> mFakePolicy;
1352
Chris Yea52ade12020-08-27 16:49:20 -07001353 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1354 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355};
1356
1357/**
1358 * Check that empty set of viewports is an acceptable configuration.
1359 * Also try to get internal viewport two different ways - by type and by uniqueId.
1360 *
1361 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1362 * Such configuration is not currently allowed.
1363 */
1364TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001365 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366
1367 // We didn't add any viewports yet, so there shouldn't be any.
1368 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001369 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 ASSERT_FALSE(internalViewport);
1371
1372 // Add an internal viewport, then clear it
1373 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001374 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001375 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001376
1377 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001378 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001380 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001381
1382 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001383 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001385 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386
1387 mFakePolicy->clearViewports();
1388 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001389 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001391 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001392 ASSERT_FALSE(internalViewport);
1393}
1394
1395TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1396 const std::string internalUniqueId = "local:0";
1397 const std::string externalUniqueId = "local:1";
1398 const std::string virtualUniqueId1 = "virtual:2";
1399 const std::string virtualUniqueId2 = "virtual:3";
1400 constexpr int32_t virtualDisplayId1 = 2;
1401 constexpr int32_t virtualDisplayId2 = 3;
1402
1403 // Add an internal viewport
1404 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001405 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1406 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 // Add an external viewport
1408 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001409 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1410 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411 // Add an virtual viewport
1412 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001413 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1414 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 // Add another virtual viewport
1416 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001417 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1418 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419
1420 // Check matching by type for internal
1421 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001422 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001423 ASSERT_TRUE(internalViewport);
1424 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1425
1426 // Check matching by type for external
1427 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001428 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001429 ASSERT_TRUE(externalViewport);
1430 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1431
1432 // Check matching by uniqueId for virtual viewport #1
1433 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001434 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001436 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001437 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1438 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1439
1440 // Check matching by uniqueId for virtual viewport #2
1441 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001442 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001444 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1446 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1447}
1448
1449
1450/**
1451 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1452 * that lookup works by checking display id.
1453 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1454 */
1455TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1456 const std::string uniqueId1 = "uniqueId1";
1457 const std::string uniqueId2 = "uniqueId2";
1458 constexpr int32_t displayId1 = 2;
1459 constexpr int32_t displayId2 = 3;
1460
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001461 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1462 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001463 for (const ViewportType& type : types) {
1464 mFakePolicy->clearViewports();
1465 // Add a viewport
1466 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001467 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1468 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001469 // Add another viewport
1470 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001471 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1472 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001473
1474 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001475 std::optional<DisplayViewport> viewport1 =
1476 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001477 ASSERT_TRUE(viewport1);
1478 ASSERT_EQ(displayId1, viewport1->displayId);
1479 ASSERT_EQ(type, viewport1->type);
1480
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001481 std::optional<DisplayViewport> viewport2 =
1482 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001483 ASSERT_TRUE(viewport2);
1484 ASSERT_EQ(displayId2, viewport2->displayId);
1485 ASSERT_EQ(type, viewport2->type);
1486
1487 // When there are multiple viewports of the same kind, and uniqueId is not specified
1488 // in the call to getDisplayViewport, then that situation is not supported.
1489 // The viewports can be stored in any order, so we cannot rely on the order, since that
1490 // is just implementation detail.
1491 // However, we can check that it still returns *a* viewport, we just cannot assert
1492 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001493 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001494 ASSERT_TRUE(someViewport);
1495 }
1496}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001498/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001499 * When we have multiple internal displays make sure we always return the default display when
1500 * querying by type.
1501 */
1502TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1503 const std::string uniqueId1 = "uniqueId1";
1504 const std::string uniqueId2 = "uniqueId2";
1505 constexpr int32_t nonDefaultDisplayId = 2;
1506 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1507 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1508
1509 // Add the default display first and ensure it gets returned.
1510 mFakePolicy->clearViewports();
1511 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001512 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001513 ViewportType::INTERNAL);
1514 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001515 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001516 ViewportType::INTERNAL);
1517
1518 std::optional<DisplayViewport> viewport =
1519 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1520 ASSERT_TRUE(viewport);
1521 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1522 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1523
1524 // Add the default display second to make sure order doesn't matter.
1525 mFakePolicy->clearViewports();
1526 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001527 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001528 ViewportType::INTERNAL);
1529 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001530 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001531 ViewportType::INTERNAL);
1532
1533 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1534 ASSERT_TRUE(viewport);
1535 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1536 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1537}
1538
1539/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001540 * Check getDisplayViewportByPort
1541 */
1542TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001543 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001544 const std::string uniqueId1 = "uniqueId1";
1545 const std::string uniqueId2 = "uniqueId2";
1546 constexpr int32_t displayId1 = 1;
1547 constexpr int32_t displayId2 = 2;
1548 const uint8_t hdmi1 = 0;
1549 const uint8_t hdmi2 = 1;
1550 const uint8_t hdmi3 = 2;
1551
1552 mFakePolicy->clearViewports();
1553 // Add a viewport that's associated with some display port that's not of interest.
1554 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001555 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1556 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001557 // Add another viewport, connected to HDMI1 port
1558 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001559 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1560 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001561
1562 // Check that correct display viewport was returned by comparing the display ports.
1563 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1564 ASSERT_TRUE(hdmi1Viewport);
1565 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1566 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1567
1568 // Check that we can still get the same viewport using the uniqueId
1569 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1570 ASSERT_TRUE(hdmi1Viewport);
1571 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1572 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1573 ASSERT_EQ(type, hdmi1Viewport->type);
1574
1575 // Check that we cannot find a port with "HDMI2", because we never added one
1576 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1577 ASSERT_FALSE(hdmi2Viewport);
1578}
1579
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580// --- InputReaderTest ---
1581
1582class InputReaderTest : public testing::Test {
1583protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001584 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001586 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001587 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588
Chris Yea52ade12020-08-27 16:49:20 -07001589 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001590 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001592 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593
Prabir Pradhan28efc192019-11-05 01:10:04 +00001594 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001595 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 }
1597
Chris Yea52ade12020-08-27 16:49:20 -07001598 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001599 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001600 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601 }
1602
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001603 void addDevice(int32_t eventHubId, const std::string& name,
1604 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606
1607 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001608 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001609 }
1610 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001611 mReader->loopOnce();
1612 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001613 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1614 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615 }
1616
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001617 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001618 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001619 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001620 }
1621
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001622 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001624 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001625 }
1626
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001628 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001629 ftl::Flags<InputDeviceClass> classes,
1630 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001631 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1633 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001634 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001635 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001636 return mapper;
1637 }
1638};
1639
Chris Ye98d3f532020-10-01 21:48:59 -07001640TEST_F(InputReaderTest, PolicyGetInputDevices) {
1641 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001642 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001643 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644
1645 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001646 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001647 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001649 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1651 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001652 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001653}
1654
Chris Yee7310032020-09-22 15:36:28 -07001655TEST_F(InputReaderTest, GetMergedInputDevices) {
1656 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1657 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1658 // Add two subdevices to device
1659 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1660 // Must add at least one mapper or the device will be ignored!
1661 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1662 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1663
1664 // Push same device instance for next device to be added, so they'll have same identifier.
1665 mReader->pushNextDevice(device);
1666 mReader->pushNextDevice(device);
1667 ASSERT_NO_FATAL_FAILURE(
1668 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1669 ASSERT_NO_FATAL_FAILURE(
1670 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1671
1672 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001673 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001674}
1675
Chris Yee14523a2020-12-19 13:46:00 -08001676TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1677 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1678 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1679 // Add two subdevices to device
1680 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1681 // Must add at least one mapper or the device will be ignored!
1682 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1683 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1684
1685 // Push same device instance for next device to be added, so they'll have same identifier.
1686 mReader->pushNextDevice(device);
1687 mReader->pushNextDevice(device);
1688 // Sensor device is initially disabled
1689 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1690 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1691 nullptr));
1692 // Device is disabled because the only sub device is a sensor device and disabled initially.
1693 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1694 ASSERT_FALSE(device->isEnabled());
1695 ASSERT_NO_FATAL_FAILURE(
1696 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1697 // The merged device is enabled if any sub device is enabled
1698 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1699 ASSERT_TRUE(device->isEnabled());
1700}
1701
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001702TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001703 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001704 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001705 constexpr int32_t eventHubId = 1;
1706 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001707 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001709 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001711
Yi Kong9b14ac62018-07-17 13:48:38 -07001712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001713
1714 NotifyDeviceResetArgs resetArgs;
1715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001716 ASSERT_EQ(deviceId, resetArgs.deviceId);
1717
1718 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001719 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001720 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001721
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001723 ASSERT_EQ(deviceId, resetArgs.deviceId);
1724 ASSERT_EQ(device->isEnabled(), false);
1725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001726 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001727 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001730 ASSERT_EQ(device->isEnabled(), false);
1731
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001732 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001733 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001735 ASSERT_EQ(deviceId, resetArgs.deviceId);
1736 ASSERT_EQ(device->isEnabled(), true);
1737}
1738
Michael Wrightd02c5b62014-02-10 15:10:22 -08001739TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001741 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001743 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001744 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001745 AINPUT_SOURCE_KEYBOARD, nullptr);
1746 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001747
1748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1749 AINPUT_SOURCE_ANY, AKEYCODE_A))
1750 << "Should return unknown when the device id is >= 0 but unknown.";
1751
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001752 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1753 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1754 << "Should return unknown when the device id is valid but the sources are not "
1755 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001756
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001757 ASSERT_EQ(AKEY_STATE_DOWN,
1758 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1759 AKEYCODE_A))
1760 << "Should return value provided by mapper when device id is valid and the device "
1761 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001762
1763 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1764 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1765 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1766
1767 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1768 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1769 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1770}
1771
Philip Junker4af3b3d2021-12-14 10:36:55 +01001772TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1773 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1774 constexpr int32_t eventHubId = 1;
1775 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1776 InputDeviceClass::KEYBOARD,
1777 AINPUT_SOURCE_KEYBOARD, nullptr);
1778 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1779
1780 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1781 << "Should return unknown when the device with the specified id is not found.";
1782
1783 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1784 << "Should return correct mapping when device id is valid and mapping exists.";
1785
1786 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1787 << "Should return the location key code when device id is valid and there's no "
1788 "mapping.";
1789}
1790
1791TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1792 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1793 constexpr int32_t eventHubId = 1;
1794 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1795 InputDeviceClass::JOYSTICK,
1796 AINPUT_SOURCE_GAMEPAD, nullptr);
1797 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1798
1799 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1800 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1801}
1802
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001805 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001809 AINPUT_SOURCE_KEYBOARD, nullptr);
1810 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811
1812 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1813 AINPUT_SOURCE_ANY, KEY_A))
1814 << "Should return unknown when the device id is >= 0 but unknown.";
1815
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1817 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1818 << "Should return unknown when the device id is valid but the sources are not "
1819 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 ASSERT_EQ(AKEY_STATE_DOWN,
1822 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1823 KEY_A))
1824 << "Should return value provided by mapper when device id is valid and the device "
1825 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1828 AINPUT_SOURCE_TRACKBALL, KEY_A))
1829 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1830
1831 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1832 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1833 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1834}
1835
1836TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001838 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001839 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001840 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001841 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001842 AINPUT_SOURCE_KEYBOARD, nullptr);
1843 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001844
1845 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1846 AINPUT_SOURCE_ANY, SW_LID))
1847 << "Should return unknown when the device id is >= 0 but unknown.";
1848
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1850 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1851 << "Should return unknown when the device id is valid but the sources are not "
1852 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001853
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 ASSERT_EQ(AKEY_STATE_DOWN,
1855 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1856 SW_LID))
1857 << "Should return value provided by mapper when device id is valid and the device "
1858 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859
1860 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1861 AINPUT_SOURCE_TRACKBALL, SW_LID))
1862 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1863
1864 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1865 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1866 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1867}
1868
1869TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001871 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001874 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001875 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001877 mapper.addSupportedKeyCode(AKEYCODE_A);
1878 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001879
1880 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1881 uint8_t flags[4] = { 0, 0, 0, 1 };
1882
1883 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1884 << "Should return false when device id is >= 0 but unknown.";
1885 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1886
1887 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001888 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1889 << "Should return false when device id is valid but the sources are not supported by "
1890 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001891 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1892
1893 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001894 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1895 keyCodes, flags))
1896 << "Should return value provided by mapper when device id is valid and the device "
1897 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001898 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1899
1900 flags[3] = 1;
1901 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1902 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1903 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1904
1905 flags[3] = 1;
1906 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1907 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1908 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1909}
1910
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001911TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001912 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001913 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914
1915 NotifyConfigurationChangedArgs args;
1916
1917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1918 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1919}
1920
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001921TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001922 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001923 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001924 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001925 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001926 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001927 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001928 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001929 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001931 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001932 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001933 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1934
1935 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001936 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001937 ASSERT_EQ(when, event.when);
1938 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001939 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001940 ASSERT_EQ(EV_KEY, event.type);
1941 ASSERT_EQ(KEY_A, event.code);
1942 ASSERT_EQ(1, event.value);
1943}
1944
Garfield Tan1c7bc862020-01-28 13:24:04 -08001945TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001946 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001947 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001948 constexpr int32_t eventHubId = 1;
1949 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001950 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001951 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001952 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001954
1955 NotifyDeviceResetArgs resetArgs;
1956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001957 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001958
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001959 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001960 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001962 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001963 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001965 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001966 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001968 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001969 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001970
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001971 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001972 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001974 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001975 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001976}
1977
Garfield Tan1c7bc862020-01-28 13:24:04 -08001978TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1979 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001980 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001981 constexpr int32_t eventHubId = 1;
1982 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1983 // Must add at least one mapper or the device will be ignored!
1984 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001985 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001986 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1987
1988 NotifyDeviceResetArgs resetArgs;
1989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1990 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1991}
1992
Arthur Hungc23540e2018-11-29 20:42:11 +08001993TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001994 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001995 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001996 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001997 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001998 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1999 FakeInputMapper& mapper =
2000 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002001 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002002
2003 const uint8_t hdmi1 = 1;
2004
2005 // Associated touch screen with second display.
2006 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2007
2008 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002009 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002010 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002011 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002012 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002013 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002014 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002015 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002016 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002017 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002018
2019 // Add the device, and make sure all of the callbacks are triggered.
2020 // The device is added after the input port associations are processed since
2021 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002022 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002025 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002026
Arthur Hung2c9a3342019-07-23 14:18:59 +08002027 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002028 ASSERT_EQ(deviceId, device->getId());
2029 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2030 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002031
2032 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002033 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002034 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002035 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002036}
2037
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002038TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2039 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002040 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002041 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2042 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2043 // Must add at least one mapper or the device will be ignored!
2044 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2045 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2046 mReader->pushNextDevice(device);
2047 mReader->pushNextDevice(device);
2048 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2049 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2050
2051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2052
2053 NotifyDeviceResetArgs resetArgs;
2054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2055 ASSERT_EQ(deviceId, resetArgs.deviceId);
2056 ASSERT_TRUE(device->isEnabled());
2057 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2058 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2059
2060 disableDevice(deviceId);
2061 mReader->loopOnce();
2062
2063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2064 ASSERT_EQ(deviceId, resetArgs.deviceId);
2065 ASSERT_FALSE(device->isEnabled());
2066 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2067 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2068
2069 enableDevice(deviceId);
2070 mReader->loopOnce();
2071
2072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2073 ASSERT_EQ(deviceId, resetArgs.deviceId);
2074 ASSERT_TRUE(device->isEnabled());
2075 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2076 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2077}
2078
2079TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2080 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002081 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002082 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2083 // Add two subdevices to device
2084 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2085 FakeInputMapper& mapperDevice1 =
2086 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2087 FakeInputMapper& mapperDevice2 =
2088 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2089 mReader->pushNextDevice(device);
2090 mReader->pushNextDevice(device);
2091 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2092 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2093
2094 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2095 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2096
2097 ASSERT_EQ(AKEY_STATE_DOWN,
2098 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2099 ASSERT_EQ(AKEY_STATE_DOWN,
2100 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2101 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2102 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2103}
2104
Prabir Pradhan7e186182020-11-10 13:56:45 -08002105TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2106 NotifyPointerCaptureChangedArgs args;
2107
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002108 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002109 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2110 mReader->loopOnce();
2111 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002112 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2113 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002114
2115 mFakePolicy->setPointerCapture(false);
2116 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2117 mReader->loopOnce();
2118 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002119 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002120
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002121 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002122 // does not change.
2123 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2124 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002125 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002126}
2127
Chris Ye87143712020-11-10 05:05:58 +00002128class FakeVibratorInputMapper : public FakeInputMapper {
2129public:
2130 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2131 : FakeInputMapper(deviceContext, sources) {}
2132
2133 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2134};
2135
2136TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2137 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002138 ftl::Flags<InputDeviceClass> deviceClass =
2139 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002140 constexpr int32_t eventHubId = 1;
2141 const char* DEVICE_LOCATION = "BLUETOOTH";
2142 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2143 FakeVibratorInputMapper& mapper =
2144 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2145 mReader->pushNextDevice(device);
2146
2147 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2148 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2149
2150 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2151 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2152}
2153
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002154// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002155
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002156class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002157public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002158 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002159
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002160 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002161
Andy Chen22c330c2022-08-29 20:07:10 -04002162 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2163
Chris Yee2b1e5c2021-03-10 22:45:12 -08002164 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2165
2166 void dump(std::string& dump) override {}
2167
2168 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2169 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002170 }
2171
Chris Yee2b1e5c2021-03-10 22:45:12 -08002172 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2173 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002174 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002175
2176 bool setLightColor(int32_t lightId, int32_t color) override {
2177 getDeviceContext().setLightBrightness(lightId, color >> 24);
2178 return true;
2179 }
2180
2181 std::optional<int32_t> getLightColor(int32_t lightId) override {
2182 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2183 if (!result.has_value()) {
2184 return std::nullopt;
2185 }
2186 return result.value() << 24;
2187 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002188
2189 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2190
2191 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2192
2193private:
2194 InputDeviceContext& mDeviceContext;
2195 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2196 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chen22c330c2022-08-29 20:07:10 -04002197 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002198};
2199
Chris Yee2b1e5c2021-03-10 22:45:12 -08002200TEST_F(InputReaderTest, BatteryGetCapacity) {
2201 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002202 ftl::Flags<InputDeviceClass> deviceClass =
2203 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002204 constexpr int32_t eventHubId = 1;
2205 const char* DEVICE_LOCATION = "BLUETOOTH";
2206 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002207 FakePeripheralController& controller =
2208 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002209 mReader->pushNextDevice(device);
2210
2211 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2212
2213 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2214 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2215}
2216
2217TEST_F(InputReaderTest, BatteryGetStatus) {
2218 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002219 ftl::Flags<InputDeviceClass> deviceClass =
2220 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002221 constexpr int32_t eventHubId = 1;
2222 const char* DEVICE_LOCATION = "BLUETOOTH";
2223 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002224 FakePeripheralController& controller =
2225 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002226 mReader->pushNextDevice(device);
2227
2228 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2229
2230 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2231 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2232}
2233
Chris Ye3fdbfef2021-01-06 18:45:18 -08002234TEST_F(InputReaderTest, LightGetColor) {
2235 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002236 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002237 constexpr int32_t eventHubId = 1;
2238 const char* DEVICE_LOCATION = "BLUETOOTH";
2239 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002240 FakePeripheralController& controller =
2241 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002242 mReader->pushNextDevice(device);
2243 RawLightInfo info = {.id = 1,
2244 .name = "Mono",
2245 .maxBrightness = 255,
2246 .flags = InputLightClass::BRIGHTNESS,
2247 .path = ""};
2248 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2249 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2250
2251 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002252
Chris Yee2b1e5c2021-03-10 22:45:12 -08002253 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2254 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002255 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2256 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2257}
2258
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002259// --- InputReaderIntegrationTest ---
2260
2261// These tests create and interact with the InputReader only through its interface.
2262// The InputReader is started during SetUp(), which starts its processing in its own
2263// thread. The tests use linux uinput to emulate input devices.
2264// NOTE: Interacting with the physical device while these tests are running may cause
2265// the tests to fail.
2266class InputReaderIntegrationTest : public testing::Test {
2267protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002268 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002269 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002270 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002271
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002272 std::shared_ptr<FakePointerController> mFakePointerController;
2273
Chris Yea52ade12020-08-27 16:49:20 -07002274 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002275 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002276 mFakePointerController = std::make_shared<FakePointerController>();
2277 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002278 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2279 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002280
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002281 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2282 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002283 ASSERT_EQ(mReader->start(), OK);
2284
2285 // Since this test is run on a real device, all the input devices connected
2286 // to the test device will show up in mReader. We wait for those input devices to
2287 // show up before beginning the tests.
2288 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2289 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2290 }
2291
Chris Yea52ade12020-08-27 16:49:20 -07002292 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002293 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002294 mReader.reset();
2295 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002296 mFakePolicy.clear();
2297 }
2298};
2299
2300TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2301 // An invalid input device that is only used for this test.
2302 class InvalidUinputDevice : public UinputDevice {
2303 public:
2304 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2305
2306 private:
2307 void configureDevice(int fd, uinput_user_dev* device) override {}
2308 };
2309
2310 const size_t numDevices = mFakePolicy->getInputDevices().size();
2311
2312 // UinputDevice does not set any event or key bits, so InputReader should not
2313 // consider it as a valid device.
2314 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2315 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2316 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2317 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2318
2319 invalidDevice.reset();
2320 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2321 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2322 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2323}
2324
2325TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2326 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2327
2328 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2329 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2331 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2332
2333 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002334 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002335 const auto& it =
2336 std::find_if(inputDevices.begin(), inputDevices.end(),
2337 [&keyboard](const InputDeviceInfo& info) {
2338 return info.getIdentifier().name == keyboard->getName();
2339 });
2340
2341 ASSERT_NE(it, inputDevices.end());
2342 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2343 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2344 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002345
2346 keyboard.reset();
2347 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2349 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2350}
2351
2352TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2353 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2355
2356 NotifyConfigurationChangedArgs configChangedArgs;
2357 ASSERT_NO_FATAL_FAILURE(
2358 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002359 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002360 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2361
2362 NotifyKeyArgs keyArgs;
2363 keyboard->pressAndReleaseHomeKey();
2364 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2365 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002366 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002367 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002368 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002369 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002370 prevTimestamp = keyArgs.eventTime;
2371
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2373 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002374 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002375 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002376 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002377}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002379/**
2380 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2381 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2382 * are passed to the listener.
2383 */
2384static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2385TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2386 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2387 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2388 NotifyKeyArgs keyArgs;
2389
2390 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2392 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2393 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2394
2395 controller->pressAndReleaseKey(BTN_GEAR_UP);
2396 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2398 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2399}
2400
Arthur Hungaab25622020-01-16 11:22:11 +08002401// --- TouchProcessTest ---
2402class TouchIntegrationTest : public InputReaderIntegrationTest {
2403protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002404 const std::string UNIQUE_ID = "local:0";
2405
Chris Yea52ade12020-08-27 16:49:20 -07002406 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002407 InputReaderIntegrationTest::SetUp();
2408 // At least add an internal display.
2409 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2410 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002411 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002412
2413 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2414 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2416 }
2417
2418 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2419 int32_t orientation, const std::string& uniqueId,
2420 std::optional<uint8_t> physicalPort,
2421 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002422 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2423 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002424 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2425 }
2426
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002427 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2428 NotifyMotionArgs args;
2429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2430 EXPECT_EQ(action, args.action);
2431 ASSERT_EQ(points.size(), args.pointerCount);
2432 for (size_t i = 0; i < args.pointerCount; i++) {
2433 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2434 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2435 }
2436 }
2437
Arthur Hungaab25622020-01-16 11:22:11 +08002438 std::unique_ptr<UinputTouchScreen> mDevice;
2439};
2440
2441TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2442 NotifyMotionArgs args;
2443 const Point centerPoint = mDevice->getCenterPoint();
2444
2445 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002446 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002447 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002448 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002449 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2451
2452 // ACTION_MOVE
2453 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002454 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002455 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2457
2458 // ACTION_UP
2459 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002460 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2462 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2463}
2464
2465TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2466 NotifyMotionArgs args;
2467 const Point centerPoint = mDevice->getCenterPoint();
2468
2469 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002470 mDevice->sendSlot(FIRST_SLOT);
2471 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002472 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002473 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2476
2477 // ACTION_POINTER_DOWN (Second slot)
2478 const Point secondPoint = centerPoint + Point(100, 100);
2479 mDevice->sendSlot(SECOND_SLOT);
2480 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002481 mDevice->sendDown(secondPoint);
2482 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002483 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002484 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002485
2486 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002487 mDevice->sendMove(secondPoint + Point(1, 1));
2488 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2491
2492 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002493 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002494 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002495 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002496 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002497
2498 // ACTION_UP
2499 mDevice->sendSlot(FIRST_SLOT);
2500 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002501 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002502 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2503 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2504}
2505
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002506/**
2507 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2508 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2509 * data?
2510 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2511 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2512 * for Pointer 0 only is generated after.
2513 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2514 * events, we will not miss any information.
2515 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2516 * event generated afterwards that contains the newest movement of pointer 0.
2517 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2518 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2519 * losing information about non-palm pointers.
2520 */
2521TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2522 NotifyMotionArgs args;
2523 const Point centerPoint = mDevice->getCenterPoint();
2524
2525 // ACTION_DOWN
2526 mDevice->sendSlot(FIRST_SLOT);
2527 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2528 mDevice->sendDown(centerPoint);
2529 mDevice->sendSync();
2530 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2531
2532 // ACTION_POINTER_DOWN (Second slot)
2533 const Point secondPoint = centerPoint + Point(100, 100);
2534 mDevice->sendSlot(SECOND_SLOT);
2535 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2536 mDevice->sendDown(secondPoint);
2537 mDevice->sendSync();
2538 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2539
2540 // ACTION_MOVE (First slot)
2541 mDevice->sendSlot(FIRST_SLOT);
2542 mDevice->sendMove(centerPoint + Point(5, 5));
2543 // ACTION_POINTER_UP (Second slot)
2544 mDevice->sendSlot(SECOND_SLOT);
2545 mDevice->sendPointerUp();
2546 // Send a single sync for the above 2 pointer updates
2547 mDevice->sendSync();
2548
2549 // First, we should get POINTER_UP for the second pointer
2550 assertReceivedMotion(ACTION_POINTER_1_UP,
2551 {/*first pointer */ centerPoint + Point(5, 5),
2552 /*second pointer*/ secondPoint});
2553
2554 // Next, the MOVE event for the first pointer
2555 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2556}
2557
2558/**
2559 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2560 * move, and then it will go up, all in the same frame.
2561 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2562 * gets sent to the listener.
2563 */
2564TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2565 NotifyMotionArgs args;
2566 const Point centerPoint = mDevice->getCenterPoint();
2567
2568 // ACTION_DOWN
2569 mDevice->sendSlot(FIRST_SLOT);
2570 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2571 mDevice->sendDown(centerPoint);
2572 mDevice->sendSync();
2573 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2574
2575 // ACTION_POINTER_DOWN (Second slot)
2576 const Point secondPoint = centerPoint + Point(100, 100);
2577 mDevice->sendSlot(SECOND_SLOT);
2578 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2579 mDevice->sendDown(secondPoint);
2580 mDevice->sendSync();
2581 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2582
2583 // ACTION_MOVE (First slot)
2584 mDevice->sendSlot(FIRST_SLOT);
2585 mDevice->sendMove(centerPoint + Point(5, 5));
2586 // ACTION_POINTER_UP (Second slot)
2587 mDevice->sendSlot(SECOND_SLOT);
2588 mDevice->sendMove(secondPoint + Point(6, 6));
2589 mDevice->sendPointerUp();
2590 // Send a single sync for the above 2 pointer updates
2591 mDevice->sendSync();
2592
2593 // First, we should get POINTER_UP for the second pointer
2594 // The movement of the second pointer during the liftoff frame is ignored.
2595 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2596 assertReceivedMotion(ACTION_POINTER_1_UP,
2597 {/*first pointer */ centerPoint + Point(5, 5),
2598 /*second pointer*/ secondPoint});
2599
2600 // Next, the MOVE event for the first pointer
2601 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2602}
2603
Arthur Hungaab25622020-01-16 11:22:11 +08002604TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2605 NotifyMotionArgs args;
2606 const Point centerPoint = mDevice->getCenterPoint();
2607
2608 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002609 mDevice->sendSlot(FIRST_SLOT);
2610 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002611 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002612 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002613 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2614 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2615
arthurhungcc7f9802020-04-30 17:55:40 +08002616 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002617 const Point secondPoint = centerPoint + Point(100, 100);
2618 mDevice->sendSlot(SECOND_SLOT);
2619 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2620 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002621 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002622 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002623 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002624
arthurhungcc7f9802020-04-30 17:55:40 +08002625 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002626 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002627 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002628 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2630
arthurhungcc7f9802020-04-30 17:55:40 +08002631 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2632 // a palm event.
2633 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002634 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002635 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002636 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002637 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002638 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002639
arthurhungcc7f9802020-04-30 17:55:40 +08002640 // Send up to second slot, expect first slot send moving.
2641 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002642 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002643 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002645
arthurhungcc7f9802020-04-30 17:55:40 +08002646 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002647 mDevice->sendSlot(FIRST_SLOT);
2648 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002649 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002650
arthurhungcc7f9802020-04-30 17:55:40 +08002651 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2652 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002653}
2654
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656class InputDeviceTest : public testing::Test {
2657protected:
2658 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002659 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 static const int32_t DEVICE_ID;
2661 static const int32_t DEVICE_GENERATION;
2662 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002663 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002664 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002666 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002668 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002669 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002670 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671
Chris Yea52ade12020-08-27 16:49:20 -07002672 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002673 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002675 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002676 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002677 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678 InputDeviceIdentifier identifier;
2679 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002680 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002681 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002682 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002683 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002684 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002685 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686 }
2687
Chris Yea52ade12020-08-27 16:49:20 -07002688 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002689 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 }
2692};
2693
2694const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002695const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002696const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2698const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002699const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002700 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002701const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702
2703TEST_F(InputDeviceTest, ImmutableProperties) {
2704 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002705 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002706 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707}
2708
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002709TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2710 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002711}
2712
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2714 // Configuration.
2715 InputReaderConfiguration config;
2716 mDevice->configure(ARBITRARY_TIME, &config, 0);
2717
2718 // Reset.
2719 mDevice->reset(ARBITRARY_TIME);
2720
2721 NotifyDeviceResetArgs resetArgs;
2722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2723 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2724 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2725
2726 // Metadata.
2727 ASSERT_TRUE(mDevice->isIgnored());
2728 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2729
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002730 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002732 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002733 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2734 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2735
2736 // State queries.
2737 ASSERT_EQ(0, mDevice->getMetaState());
2738
2739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2740 << "Ignored device should return unknown key code state.";
2741 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2742 << "Ignored device should return unknown scan code state.";
2743 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2744 << "Ignored device should return unknown switch state.";
2745
2746 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2747 uint8_t flags[2] = { 0, 1 };
2748 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2749 << "Ignored device should never mark any key codes.";
2750 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2751 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2752}
2753
2754TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2755 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002756 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002758 FakeInputMapper& mapper1 =
2759 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002760 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2761 mapper1.setMetaState(AMETA_ALT_ON);
2762 mapper1.addSupportedKeyCode(AKEYCODE_A);
2763 mapper1.addSupportedKeyCode(AKEYCODE_B);
2764 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2765 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2766 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2767 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2768 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002770 FakeInputMapper& mapper2 =
2771 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002772 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002773
2774 InputReaderConfiguration config;
2775 mDevice->configure(ARBITRARY_TIME, &config, 0);
2776
2777 String8 propertyValue;
2778 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2779 << "Device should have read configuration during configuration phase.";
2780 ASSERT_STREQ("value", propertyValue.string());
2781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2783 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
2785 // Reset
2786 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2788 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789
2790 NotifyDeviceResetArgs resetArgs;
2791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2792 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2793 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2794
2795 // Metadata.
2796 ASSERT_FALSE(mDevice->isIgnored());
2797 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2798
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002799 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002801 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2803 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2804
2805 // State queries.
2806 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2807 << "Should query mappers and combine meta states.";
2808
2809 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2810 << "Should return unknown key code state when source not supported.";
2811 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2812 << "Should return unknown scan code state when source not supported.";
2813 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2814 << "Should return unknown switch state when source not supported.";
2815
2816 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2817 << "Should query mapper when source is supported.";
2818 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2819 << "Should query mapper when source is supported.";
2820 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2821 << "Should query mapper when source is supported.";
2822
2823 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2824 uint8_t flags[4] = { 0, 0, 0, 1 };
2825 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2826 << "Should do nothing when source is unsupported.";
2827 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2828 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2829 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2830 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2831
2832 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2833 << "Should query mapper when source is supported.";
2834 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2835 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2836 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2837 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2838
2839 // Event handling.
2840 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002841 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842 mDevice->process(&event, 1);
2843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002844 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2845 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846}
2847
Arthur Hung2c9a3342019-07-23 14:18:59 +08002848// A single input device is associated with a specific display. Check that:
2849// 1. Device is disabled if the viewport corresponding to the associated display is not found
2850// 2. Device is disabled when setEnabled API is called
2851TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002852 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002853
2854 // First Configuration.
2855 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2856
2857 // Device should be enabled by default.
2858 ASSERT_TRUE(mDevice->isEnabled());
2859
2860 // Prepare associated info.
2861 constexpr uint8_t hdmi = 1;
2862 const std::string UNIQUE_ID = "local:1";
2863
2864 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2865 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2866 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2867 // Device should be disabled because it is associated with a specific display via
2868 // input port <-> display port association, but the corresponding display is not found
2869 ASSERT_FALSE(mDevice->isEnabled());
2870
2871 // Prepare displays.
2872 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002873 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2874 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002875 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2876 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2877 ASSERT_TRUE(mDevice->isEnabled());
2878
2879 // Device should be disabled after set disable.
2880 mFakePolicy->addDisabledDevice(mDevice->getId());
2881 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2882 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2883 ASSERT_FALSE(mDevice->isEnabled());
2884
2885 // Device should still be disabled even found the associated display.
2886 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2887 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2888 ASSERT_FALSE(mDevice->isEnabled());
2889}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002890
Christine Franks1ba71cc2021-04-07 14:37:42 -07002891TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2892 // Device should be enabled by default.
2893 mFakePolicy->clearViewports();
2894 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2895 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2896 ASSERT_TRUE(mDevice->isEnabled());
2897
2898 // Device should be disabled because it is associated with a specific display, but the
2899 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002900 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002901 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2902 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2903 ASSERT_FALSE(mDevice->isEnabled());
2904
2905 // Device should be enabled when a display is found.
2906 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2907 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2908 NO_PORT, ViewportType::INTERNAL);
2909 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2910 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2911 ASSERT_TRUE(mDevice->isEnabled());
2912
2913 // Device should be disabled after set disable.
2914 mFakePolicy->addDisabledDevice(mDevice->getId());
2915 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2916 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2917 ASSERT_FALSE(mDevice->isEnabled());
2918
2919 // Device should still be disabled even found the associated display.
2920 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2921 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2922 ASSERT_FALSE(mDevice->isEnabled());
2923}
2924
Christine Franks2a2293c2022-01-18 11:51:16 -08002925TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2926 mFakePolicy->clearViewports();
2927 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2928 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2929
Christine Franks2a2293c2022-01-18 11:51:16 -08002930 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2931 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2932 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2933 NO_PORT, ViewportType::INTERNAL);
2934 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2935 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2936 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2937}
2938
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939// --- InputMapperTest ---
2940
2941class InputMapperTest : public testing::Test {
2942protected:
2943 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002944 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945 static const int32_t DEVICE_ID;
2946 static const int32_t DEVICE_GENERATION;
2947 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002948 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002949 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002951 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002953 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002954 std::unique_ptr<InstrumentedInputReader> mReader;
2955 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002957 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002958 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002959 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002960 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002961 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002962 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002963 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002964 // Consume the device reset notification generated when adding a new device.
2965 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002966 }
2967
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002968 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002969 SetUp(DEVICE_CLASSES);
2970 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002971
Chris Yea52ade12020-08-27 16:49:20 -07002972 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002973 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975 }
2976
2977 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002978 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979 }
2980
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002981 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002982 if (!changes ||
2983 (changes &
2984 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2985 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002986 mReader->requestRefreshConfiguration(changes);
2987 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002988 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002989 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002990 // Loop the reader to flush the input listener queue.
2991 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002992 }
2993
arthurhungdcef2dc2020-08-11 14:47:50 +08002994 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2995 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002996 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002997 InputDeviceIdentifier identifier;
2998 identifier.name = name;
2999 identifier.location = location;
3000 std::shared_ptr<InputDevice> device =
3001 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3002 identifier);
3003 mReader->pushNextDevice(device);
3004 mFakeEventHub->addDevice(eventHubId, name, classes);
3005 mReader->loopOnce();
3006 return device;
3007 }
3008
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003009 template <class T, typename... Args>
3010 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003011 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003012 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003013 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003014 mapper.reset(ARBITRARY_TIME);
Prabir Pradhan36690412022-08-05 22:26:56 +00003015 // Loop the reader to flush the input listener queue.
3016 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003017 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 }
3019
3020 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003021 int32_t orientation, const std::string& uniqueId,
3022 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003023 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3024 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003025 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3026 }
3027
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003028 void clearViewports() {
3029 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003030 }
3031
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003032 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3033 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 RawEvent event;
3035 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003036 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003037 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003038 event.type = type;
3039 event.code = code;
3040 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003041 mapper.process(&event);
Prabir Pradhan36690412022-08-05 22:26:56 +00003042 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003043 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 }
3045
3046 static void assertMotionRange(const InputDeviceInfo& info,
3047 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3048 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003049 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3051 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3052 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3053 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3054 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3055 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3056 }
3057
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003058 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3059 float size, float touchMajor, float touchMinor, float toolMajor,
3060 float toolMinor, float orientation, float distance,
3061 float scaledAxisEpsilon = 1.f) {
3062 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3063 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3065 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003066 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3067 scaledAxisEpsilon);
3068 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3069 scaledAxisEpsilon);
3070 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3071 scaledAxisEpsilon);
3072 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3073 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3075 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3076 }
3077
Michael Wright17db18e2020-06-26 20:51:44 +01003078 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003080 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 ASSERT_NEAR(x, actualX, 1);
3082 ASSERT_NEAR(y, actualY, 1);
3083 }
3084};
3085
3086const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003087const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003088const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3090const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003091const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3092 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003093const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
3095// --- SwitchInputMapperTest ---
3096
3097class SwitchInputMapperTest : public InputMapperTest {
3098protected:
3099};
3100
3101TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003102 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003104 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105}
3106
3107TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003108 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003110 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003111 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003113 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003114 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115}
3116
3117TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003118 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124
3125 NotifySwitchArgs args;
3126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3127 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003128 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3129 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130 args.switchMask);
3131 ASSERT_EQ(uint32_t(0), args.policyFlags);
3132}
3133
Chris Ye87143712020-11-10 05:05:58 +00003134// --- VibratorInputMapperTest ---
3135class VibratorInputMapperTest : public InputMapperTest {
3136protected:
3137 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3138};
3139
3140TEST_F(VibratorInputMapperTest, GetSources) {
3141 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3142
3143 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3144}
3145
3146TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3147 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3148
3149 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3150}
3151
3152TEST_F(VibratorInputMapperTest, Vibrate) {
3153 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003154 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003155 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3156
3157 VibrationElement pattern(2);
3158 VibrationSequence sequence(2);
3159 pattern.duration = std::chrono::milliseconds(200);
3160 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3161 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3162 sequence.addElement(pattern);
3163 pattern.duration = std::chrono::milliseconds(500);
3164 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3165 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3166 sequence.addElement(pattern);
3167
3168 std::vector<int64_t> timings = {0, 1};
3169 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3170
3171 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003172 // Start vibrating
3173 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003174 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003175 // Verify vibrator state listener was notified.
3176 mReader->loopOnce();
3177 NotifyVibratorStateArgs args;
3178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3179 ASSERT_EQ(DEVICE_ID, args.deviceId);
3180 ASSERT_TRUE(args.isOn);
3181 // Stop vibrating
3182 mapper.cancelVibrate(VIBRATION_TOKEN);
3183 ASSERT_FALSE(mapper.isVibrating());
3184 // Verify vibrator state listener was notified.
3185 mReader->loopOnce();
3186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3187 ASSERT_EQ(DEVICE_ID, args.deviceId);
3188 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003189}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190
Chris Yef59a2f42020-10-16 12:55:26 -07003191// --- SensorInputMapperTest ---
3192
3193class SensorInputMapperTest : public InputMapperTest {
3194protected:
3195 static const int32_t ACCEL_RAW_MIN;
3196 static const int32_t ACCEL_RAW_MAX;
3197 static const int32_t ACCEL_RAW_FUZZ;
3198 static const int32_t ACCEL_RAW_FLAT;
3199 static const int32_t ACCEL_RAW_RESOLUTION;
3200
3201 static const int32_t GYRO_RAW_MIN;
3202 static const int32_t GYRO_RAW_MAX;
3203 static const int32_t GYRO_RAW_FUZZ;
3204 static const int32_t GYRO_RAW_FLAT;
3205 static const int32_t GYRO_RAW_RESOLUTION;
3206
3207 static const float GRAVITY_MS2_UNIT;
3208 static const float DEGREE_RADIAN_UNIT;
3209
3210 void prepareAccelAxes();
3211 void prepareGyroAxes();
3212 void setAccelProperties();
3213 void setGyroProperties();
3214 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3215};
3216
3217const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3218const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3219const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3220const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3221const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3222
3223const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3224const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3225const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3226const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3227const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3228
3229const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3230const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3231
3232void SensorInputMapperTest::prepareAccelAxes() {
3233 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3234 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3235 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3236 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3237 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3238 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3239}
3240
3241void SensorInputMapperTest::prepareGyroAxes() {
3242 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3243 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3244 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3245 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3246 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3247 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3248}
3249
3250void SensorInputMapperTest::setAccelProperties() {
3251 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3252 /* sensorDataIndex */ 0);
3253 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3254 /* sensorDataIndex */ 1);
3255 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3256 /* sensorDataIndex */ 2);
3257 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3258 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3259 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3260 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3261 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3262}
3263
3264void SensorInputMapperTest::setGyroProperties() {
3265 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3266 /* sensorDataIndex */ 0);
3267 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3268 /* sensorDataIndex */ 1);
3269 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3270 /* sensorDataIndex */ 2);
3271 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3272 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3273 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3274 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3275 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3276}
3277
3278TEST_F(SensorInputMapperTest, GetSources) {
3279 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3280
3281 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3282}
3283
3284TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3285 setAccelProperties();
3286 prepareAccelAxes();
3287 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3288
3289 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3290 std::chrono::microseconds(10000),
3291 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003292 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003298
3299 NotifySensorArgs args;
3300 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3301 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3302 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3303
3304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3305 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3306 ASSERT_EQ(args.deviceId, DEVICE_ID);
3307 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3308 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3309 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3310 ASSERT_EQ(args.values, values);
3311 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3312}
3313
3314TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3315 setGyroProperties();
3316 prepareGyroAxes();
3317 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3318
3319 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3320 std::chrono::microseconds(10000),
3321 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003322 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003328
3329 NotifySensorArgs args;
3330 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3331 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3332 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3333
3334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3335 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3336 ASSERT_EQ(args.deviceId, DEVICE_ID);
3337 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3338 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3339 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3340 ASSERT_EQ(args.values, values);
3341 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3342}
3343
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344// --- KeyboardInputMapperTest ---
3345
3346class KeyboardInputMapperTest : public InputMapperTest {
3347protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003348 const std::string UNIQUE_ID = "local:0";
3349
3350 void prepareDisplay(int32_t orientation);
3351
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003352 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003353 int32_t originalKeyCode, int32_t rotatedKeyCode,
3354 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355};
3356
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003357/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3358 * orientation.
3359 */
3360void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003361 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3362 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003363}
3364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003365void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003366 int32_t originalScanCode, int32_t originalKeyCode,
3367 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 NotifyKeyArgs args;
3369
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3372 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3373 ASSERT_EQ(originalScanCode, args.scanCode);
3374 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003375 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3379 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3380 ASSERT_EQ(originalScanCode, args.scanCode);
3381 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003382 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383}
3384
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003386 KeyboardInputMapper& mapper =
3387 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3388 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003390 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391}
3392
3393TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3394 const int32_t USAGE_A = 0x070004;
3395 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003396 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3397 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003398 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3399 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3400 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003402 KeyboardInputMapper& mapper =
3403 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3404 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003405 // Initial metastate is AMETA_NONE.
3406 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407
3408 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 NotifyKeyArgs args;
3411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3412 ASSERT_EQ(DEVICE_ID, args.deviceId);
3413 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3414 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3415 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3416 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3417 ASSERT_EQ(KEY_HOME, args.scanCode);
3418 ASSERT_EQ(AMETA_NONE, args.metaState);
3419 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3420 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3421 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3422
3423 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003424 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3426 ASSERT_EQ(DEVICE_ID, args.deviceId);
3427 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3428 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3429 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3430 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3431 ASSERT_EQ(KEY_HOME, args.scanCode);
3432 ASSERT_EQ(AMETA_NONE, args.metaState);
3433 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3434 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3435 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3436
3437 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(DEVICE_ID, args.deviceId);
3442 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3443 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3445 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3446 ASSERT_EQ(0, args.scanCode);
3447 ASSERT_EQ(AMETA_NONE, args.metaState);
3448 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3449 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3450 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3451
3452 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3454 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3456 ASSERT_EQ(DEVICE_ID, args.deviceId);
3457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3458 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3460 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3461 ASSERT_EQ(0, args.scanCode);
3462 ASSERT_EQ(AMETA_NONE, args.metaState);
3463 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3464 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3465 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3466
3467 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3471 ASSERT_EQ(DEVICE_ID, args.deviceId);
3472 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3473 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3474 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3475 ASSERT_EQ(0, args.keyCode);
3476 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3477 ASSERT_EQ(AMETA_NONE, args.metaState);
3478 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3479 ASSERT_EQ(0U, args.policyFlags);
3480 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3481
3482 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3484 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3486 ASSERT_EQ(DEVICE_ID, args.deviceId);
3487 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3488 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3489 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3490 ASSERT_EQ(0, args.keyCode);
3491 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3492 ASSERT_EQ(AMETA_NONE, args.metaState);
3493 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3494 ASSERT_EQ(0U, args.policyFlags);
3495 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3496}
3497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003498/**
3499 * Ensure that the readTime is set to the time when the EV_KEY is received.
3500 */
3501TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3502 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3503
3504 KeyboardInputMapper& mapper =
3505 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3506 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3507 NotifyKeyArgs args;
3508
3509 // Key down
3510 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3512 ASSERT_EQ(12, args.readTime);
3513
3514 // Key up
3515 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3517 ASSERT_EQ(15, args.readTime);
3518}
3519
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003521 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3522 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003523 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3524 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3525 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003527 KeyboardInputMapper& mapper =
3528 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3529 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530
Arthur Hung95f68612022-04-07 14:08:22 +08003531 // Initial metastate is AMETA_NONE.
3532 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533
3534 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536 NotifyKeyArgs args;
3537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3538 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003539 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003540 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541
3542 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003543 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3545 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003546 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547
3548 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003549 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 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());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553
3554 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003555 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3557 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003558 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003559 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560}
3561
3562TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003563 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3564 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3565 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3566 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003568 KeyboardInputMapper& mapper =
3569 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3570 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003571
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003572 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3574 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3575 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3576 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3577 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3578 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3579 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3580 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3581}
3582
3583TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003584 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3585 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3586 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3587 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003590 KeyboardInputMapper& mapper =
3591 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3592 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003594 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003595 ASSERT_NO_FATAL_FAILURE(
3596 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3597 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3598 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3599 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3600 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3601 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3602 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003604 clearViewports();
3605 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003606 ASSERT_NO_FATAL_FAILURE(
3607 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3608 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3609 AKEYCODE_DPAD_UP, DISPLAY_ID));
3610 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3611 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3613 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003614
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003615 clearViewports();
3616 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003617 ASSERT_NO_FATAL_FAILURE(
3618 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3619 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3620 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3622 AKEYCODE_DPAD_UP, DISPLAY_ID));
3623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3624 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003626 clearViewports();
3627 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003628 ASSERT_NO_FATAL_FAILURE(
3629 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3630 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3631 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3633 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3635 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636
3637 // Special case: if orientation changes while key is down, we still emit the same keycode
3638 // in the key up as we did in the key down.
3639 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003640 clearViewports();
3641 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3644 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3645 ASSERT_EQ(KEY_UP, args.scanCode);
3646 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3647
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003648 clearViewports();
3649 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3652 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3653 ASSERT_EQ(KEY_UP, args.scanCode);
3654 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3655}
3656
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003657TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3658 // If the keyboard is not orientation aware,
3659 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003660 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003661
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003662 KeyboardInputMapper& mapper =
3663 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3664 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003665 NotifyKeyArgs args;
3666
3667 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3672 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3673
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003674 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3679 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3680}
3681
3682TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3683 // If the keyboard is orientation aware,
3684 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003685 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003686
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003687 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003688 KeyboardInputMapper& mapper =
3689 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3690 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003691 NotifyKeyArgs args;
3692
3693 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3694 // ^--- already checked by the previous test
3695
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003696 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003697 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003698 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3702 ASSERT_EQ(DISPLAY_ID, args.displayId);
3703
3704 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003705 clearViewports();
3706 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003707 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3712 ASSERT_EQ(newDisplayId, args.displayId);
3713}
3714
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 KeyboardInputMapper& mapper =
3717 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3718 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003720 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003721 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003723 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003724 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725}
3726
Philip Junker4af3b3d2021-12-14 10:36:55 +01003727TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3728 KeyboardInputMapper& mapper =
3729 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3730 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3731
3732 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3733 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3734 << "If a mapping is available, the result is equal to the mapping";
3735
3736 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3737 << "If no mapping is available, the result is the key location";
3738}
3739
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003741 KeyboardInputMapper& mapper =
3742 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3743 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003745 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003746 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003749 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750}
3751
3752TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003753 KeyboardInputMapper& mapper =
3754 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3755 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003757 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758
3759 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3760 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762 ASSERT_TRUE(flags[0]);
3763 ASSERT_FALSE(flags[1]);
3764}
3765
3766TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003767 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3768 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3769 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3771 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3772 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 KeyboardInputMapper& mapper =
3775 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3776 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003777 // Initial metastate is AMETA_NONE.
3778 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003779
3780 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003781 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3782 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3783 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784
3785 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003788 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3789 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3790 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003791 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792
3793 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003796 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3797 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3798 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003799 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800
3801 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003804 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3805 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3806 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
3809 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003812 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3813 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3814 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003815 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816
3817 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003820 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3821 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3822 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003823 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824
3825 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003828 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3829 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3830 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003831 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832}
3833
Chris Yea52ade12020-08-27 16:49:20 -07003834TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3835 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3836 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3837 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3838 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3839
3840 KeyboardInputMapper& mapper =
3841 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3842 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3843
Chris Yea52ade12020-08-27 16:49:20 -07003844 // Meta state should be AMETA_NONE after reset
3845 mapper.reset(ARBITRARY_TIME);
3846 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3847 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3848 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3849 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3850
3851 NotifyKeyArgs args;
3852 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3855 ASSERT_EQ(AMETA_NONE, args.metaState);
3856 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3857 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3858 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3859
3860 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003861 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3863 ASSERT_EQ(AMETA_NONE, args.metaState);
3864 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3865 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3866 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3867}
3868
Arthur Hung2c9a3342019-07-23 14:18:59 +08003869TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3870 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003871 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3872 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3873 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3874 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003875
3876 // keyboard 2.
3877 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003878 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003879 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003880 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003881 std::shared_ptr<InputDevice> device2 =
3882 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003883 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003884
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003885 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3886 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3887 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3888 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003889
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003890 KeyboardInputMapper& mapper =
3891 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3892 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003893
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003894 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003895 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003896 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003897 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3898 device2->reset(ARBITRARY_TIME);
3899
3900 // Prepared displays and associated info.
3901 constexpr uint8_t hdmi1 = 0;
3902 constexpr uint8_t hdmi2 = 1;
3903 const std::string SECONDARY_UNIQUE_ID = "local:1";
3904
3905 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3906 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3907
3908 // No associated display viewport found, should disable the device.
3909 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3910 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3911 ASSERT_FALSE(device2->isEnabled());
3912
3913 // Prepare second display.
3914 constexpr int32_t newDisplayId = 2;
3915 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003916 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003917 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003918 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003919 // Default device will reconfigure above, need additional reconfiguration for another device.
3920 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3921 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3922
3923 // Device should be enabled after the associated display is found.
3924 ASSERT_TRUE(mDevice->isEnabled());
3925 ASSERT_TRUE(device2->isEnabled());
3926
3927 // Test pad key events
3928 ASSERT_NO_FATAL_FAILURE(
3929 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3930 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3931 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3932 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3933 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3934 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3935 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3936
3937 ASSERT_NO_FATAL_FAILURE(
3938 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3939 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3940 AKEYCODE_DPAD_RIGHT, newDisplayId));
3941 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3942 AKEYCODE_DPAD_DOWN, newDisplayId));
3943 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3944 AKEYCODE_DPAD_LEFT, newDisplayId));
3945}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946
arthurhungc903df12020-08-11 15:08:42 +08003947TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3948 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3949 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3950 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3951 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3952 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3953 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3954
3955 KeyboardInputMapper& mapper =
3956 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3957 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003958 // Initial metastate is AMETA_NONE.
3959 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003960
3961 // Initialization should have turned all of the lights off.
3962 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3963 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3964 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3965
3966 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003969 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3970 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3971
3972 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003975 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3976 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3977
3978 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003981 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3982 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3983
3984 mFakeEventHub->removeDevice(EVENTHUB_ID);
3985 mReader->loopOnce();
3986
3987 // keyboard 2 should default toggle keys.
3988 const std::string USB2 = "USB2";
3989 const std::string DEVICE_NAME2 = "KEYBOARD2";
3990 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3991 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3992 std::shared_ptr<InputDevice> device2 =
3993 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003994 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003995 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3996 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3997 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3998 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3999 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4000 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4001
arthurhung6fe95782020-10-05 22:41:16 +08004002 KeyboardInputMapper& mapper2 =
4003 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4004 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004005 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4006 device2->reset(ARBITRARY_TIME);
4007
4008 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4009 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4010 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004011 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4012 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004013}
4014
Arthur Hungcb40a002021-08-03 14:31:01 +00004015TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4016 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4017 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4018 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4019
4020 // Suppose we have two mappers. (DPAD + KEYBOARD)
4021 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4022 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4023 KeyboardInputMapper& mapper =
4024 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4025 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004026 // Initial metastate is AMETA_NONE.
4027 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004028
4029 mReader->toggleCapsLockState(DEVICE_ID);
4030 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4031}
4032
Arthur Hungfb3cc112022-04-13 07:39:50 +00004033TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4034 // keyboard 1.
4035 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4036 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4037 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4038 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4039 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4040 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4041
4042 KeyboardInputMapper& mapper1 =
4043 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4044 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4045
4046 // keyboard 2.
4047 const std::string USB2 = "USB2";
4048 const std::string DEVICE_NAME2 = "KEYBOARD2";
4049 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4050 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4051 std::shared_ptr<InputDevice> device2 =
4052 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4053 ftl::Flags<InputDeviceClass>(0));
4054 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4055 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4056 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4057 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4058 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4059 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4060
4061 KeyboardInputMapper& mapper2 =
4062 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4063 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4064 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4065 device2->reset(ARBITRARY_TIME);
4066
Arthur Hung95f68612022-04-07 14:08:22 +08004067 // Initial metastate is AMETA_NONE.
4068 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4069 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4070
4071 // Toggle num lock on and off.
4072 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4073 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004074 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4075 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4076 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4077
4078 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4079 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4080 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4081 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4082 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4083
4084 // Toggle caps lock on and off.
4085 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4086 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4087 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4088 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4089 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4090
4091 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4092 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4093 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4094 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4095 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4096
4097 // Toggle scroll lock on and off.
4098 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4099 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4100 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4101 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4102 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4103
4104 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4105 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4106 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4107 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4108 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4109}
4110
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004111// --- KeyboardInputMapperTest_ExternalDevice ---
4112
4113class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4114protected:
Chris Yea52ade12020-08-27 16:49:20 -07004115 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004116};
4117
4118TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004119 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4120 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004121
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004122 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4124 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4125 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004127 KeyboardInputMapper& mapper =
4128 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4129 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004130
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004132 NotifyKeyArgs args;
4133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4134 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4135
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004136 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4138 ASSERT_EQ(uint32_t(0), args.policyFlags);
4139
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4142 ASSERT_EQ(uint32_t(0), args.policyFlags);
4143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4146 ASSERT_EQ(uint32_t(0), args.policyFlags);
4147
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4150 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4151
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004152 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4154 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4155}
4156
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004157TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004158 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004159
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004160 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4161 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4162 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004163
Powei Fengd041c5d2019-05-03 17:11:33 -07004164 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004165 KeyboardInputMapper& mapper =
4166 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4167 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004168
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004170 NotifyKeyArgs args;
4171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4172 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4173
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004174 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4176 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4177
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4180 ASSERT_EQ(uint32_t(0), args.policyFlags);
4181
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004182 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4184 ASSERT_EQ(uint32_t(0), args.policyFlags);
4185
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4188 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4189
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004190 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4192 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4193}
4194
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195// --- CursorInputMapperTest ---
4196
4197class CursorInputMapperTest : public InputMapperTest {
4198protected:
4199 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4200
Michael Wright17db18e2020-06-26 20:51:44 +01004201 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202
Chris Yea52ade12020-08-27 16:49:20 -07004203 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204 InputMapperTest::SetUp();
4205
Michael Wright17db18e2020-06-26 20:51:44 +01004206 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004207 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 }
4209
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004210 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4211 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004212
4213 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004214 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4215 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4216 }
4217
4218 void prepareSecondaryDisplay() {
4219 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4220 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4221 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004222 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223
4224 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4225 float pressure) {
4226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4227 0.0f, 0.0f, 0.0f, EPSILON));
4228 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229};
4230
4231const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004233void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4234 int32_t originalY, int32_t rotatedX,
4235 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 NotifyMotionArgs args;
4237
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004243 ASSERT_NO_FATAL_FAILURE(
4244 assertCursorPointerCoords(args.pointerCoords[0],
4245 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4246 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247}
4248
4249TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004251 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004253 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254}
4255
4256TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004258 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004260 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261}
4262
4263TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004265 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
4267 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004268 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269
4270 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004271 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4272 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4274 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4275
4276 // When the bounds are set, then there should be a valid motion range.
4277 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4278
4279 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004280 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281
4282 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4283 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4284 1, 800 - 1, 0.0f, 0.0f));
4285 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4286 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4287 2, 480 - 1, 0.0f, 0.0f));
4288 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4289 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4290 0.0f, 1.0f, 0.0f, 0.0f));
4291}
4292
4293TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004295 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296
4297 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004298 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299
4300 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4301 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4302 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4303 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4304 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4305 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4306 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4307 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4308 0.0f, 1.0f, 0.0f, 0.0f));
4309}
4310
4311TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004313 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314
arthurhungdcef2dc2020-08-11 14:47:50 +08004315 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316
4317 NotifyMotionArgs args;
4318
4319 // Button press.
4320 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4324 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4325 ASSERT_EQ(DEVICE_ID, args.deviceId);
4326 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4327 ASSERT_EQ(uint32_t(0), args.policyFlags);
4328 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4329 ASSERT_EQ(0, args.flags);
4330 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4331 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4332 ASSERT_EQ(0, args.edgeFlags);
4333 ASSERT_EQ(uint32_t(1), args.pointerCount);
4334 ASSERT_EQ(0, args.pointerProperties[0].id);
4335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004336 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4338 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4339 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4340
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4342 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4343 ASSERT_EQ(DEVICE_ID, args.deviceId);
4344 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4345 ASSERT_EQ(uint32_t(0), args.policyFlags);
4346 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4347 ASSERT_EQ(0, args.flags);
4348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4349 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4350 ASSERT_EQ(0, args.edgeFlags);
4351 ASSERT_EQ(uint32_t(1), args.pointerCount);
4352 ASSERT_EQ(0, args.pointerProperties[0].id);
4353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004354 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4356 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4357 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4358
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004360 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4361 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4363 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4364 ASSERT_EQ(DEVICE_ID, args.deviceId);
4365 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4366 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4368 ASSERT_EQ(0, args.flags);
4369 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4370 ASSERT_EQ(0, args.buttonState);
4371 ASSERT_EQ(0, args.edgeFlags);
4372 ASSERT_EQ(uint32_t(1), args.pointerCount);
4373 ASSERT_EQ(0, args.pointerProperties[0].id);
4374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004375 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004376 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4377 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4378 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4379
4380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4381 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4382 ASSERT_EQ(DEVICE_ID, args.deviceId);
4383 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4384 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4386 ASSERT_EQ(0, args.flags);
4387 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4388 ASSERT_EQ(0, args.buttonState);
4389 ASSERT_EQ(0, args.edgeFlags);
4390 ASSERT_EQ(uint32_t(1), args.pointerCount);
4391 ASSERT_EQ(0, args.pointerProperties[0].id);
4392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004393 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4395 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4396 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4397}
4398
4399TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004401 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402
4403 NotifyMotionArgs args;
4404
4405 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004410 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4411 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4412 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413
4414 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004419 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4420 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421}
4422
4423TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004425 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426
4427 NotifyMotionArgs args;
4428
4429 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4433 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004434 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004438 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004439
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004444 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004445 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004446
4447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004449 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450}
4451
4452TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004454 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455
4456 NotifyMotionArgs args;
4457
4458 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004465 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4466 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4467 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004471 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4472 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4473 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004481 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4482 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4483 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484
4485 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004490 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004491
4492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004494 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495}
4496
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004497TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004498 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004500 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4501 // need to be rotated.
4502 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004503 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004505 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4514}
4515
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004516TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004517 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004519 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4520 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004521 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004523 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004524 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4533
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004534 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004535 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4537 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4539 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4540 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4541 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4542 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4543 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004545 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004546 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004547 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4548 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4553 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4554 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4555
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004556 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004557 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004558 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4559 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4561 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4564 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4565 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566}
4567
4568TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004570 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571
4572 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4573 mFakePointerController->setPosition(100, 200);
4574 mFakePointerController->setButtonState(0);
4575
4576 NotifyMotionArgs motionArgs;
4577 NotifyKeyArgs keyArgs;
4578
4579 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4583 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4584 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4585 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004586 ASSERT_NO_FATAL_FAILURE(
4587 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4591 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4592 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004593 ASSERT_NO_FATAL_FAILURE(
4594 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004595
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600 ASSERT_EQ(0, motionArgs.buttonState);
4601 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004602 ASSERT_NO_FATAL_FAILURE(
4603 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004606 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607 ASSERT_EQ(0, motionArgs.buttonState);
4608 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004609 ASSERT_NO_FATAL_FAILURE(
4610 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004611
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004614 ASSERT_EQ(0, motionArgs.buttonState);
4615 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004616 ASSERT_NO_FATAL_FAILURE(
4617 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618
4619 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4624 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4625 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4626 motionArgs.buttonState);
4627 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4628 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004629 ASSERT_NO_FATAL_FAILURE(
4630 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4634 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4635 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4636 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004637 ASSERT_NO_FATAL_FAILURE(
4638 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004639
4640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4641 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4642 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4643 motionArgs.buttonState);
4644 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4645 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004646 ASSERT_NO_FATAL_FAILURE(
4647 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4654 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004655 ASSERT_NO_FATAL_FAILURE(
4656 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004657
4658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004660 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4661 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004662 ASSERT_NO_FATAL_FAILURE(
4663 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004668 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4669 ASSERT_EQ(0, motionArgs.buttonState);
4670 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004671 ASSERT_NO_FATAL_FAILURE(
4672 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004675
4676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677 ASSERT_EQ(0, motionArgs.buttonState);
4678 ASSERT_EQ(0, mFakePointerController->getButtonState());
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004680 ASSERT_NO_FATAL_FAILURE(
4681 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004682
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4684 ASSERT_EQ(0, motionArgs.buttonState);
4685 ASSERT_EQ(0, mFakePointerController->getButtonState());
4686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004687 ASSERT_NO_FATAL_FAILURE(
4688 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689
4690 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4694 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4695 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004696
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004698 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4700 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004701 ASSERT_NO_FATAL_FAILURE(
4702 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004703
4704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4705 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4706 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4707 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004708 ASSERT_NO_FATAL_FAILURE(
4709 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 ASSERT_EQ(0, motionArgs.buttonState);
4716 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004717 ASSERT_NO_FATAL_FAILURE(
4718 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004719
4720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004722 ASSERT_EQ(0, motionArgs.buttonState);
4723 ASSERT_EQ(0, mFakePointerController->getButtonState());
4724
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004725 ASSERT_NO_FATAL_FAILURE(
4726 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4728 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4729 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4730
4731 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4735 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4736 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4741 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004742 ASSERT_NO_FATAL_FAILURE(
4743 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004744
4745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4746 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4747 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4748 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004749 ASSERT_NO_FATAL_FAILURE(
4750 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004755 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756 ASSERT_EQ(0, motionArgs.buttonState);
4757 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004758 ASSERT_NO_FATAL_FAILURE(
4759 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004760
4761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4762 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4763 ASSERT_EQ(0, motionArgs.buttonState);
4764 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004765 ASSERT_NO_FATAL_FAILURE(
4766 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004767
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4769 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4770 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4771
4772 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4776 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4777 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004778
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004780 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4782 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004783 ASSERT_NO_FATAL_FAILURE(
4784 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004785
4786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4787 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4788 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4789 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004790 ASSERT_NO_FATAL_FAILURE(
4791 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004796 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 ASSERT_EQ(0, motionArgs.buttonState);
4798 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004799 ASSERT_NO_FATAL_FAILURE(
4800 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004801
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4803 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4804 ASSERT_EQ(0, motionArgs.buttonState);
4805 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004806 ASSERT_NO_FATAL_FAILURE(
4807 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004808
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4810 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4811 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4812
4813 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4817 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4818 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004819
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004821 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4823 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004824 ASSERT_NO_FATAL_FAILURE(
4825 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004826
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4828 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4829 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4830 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004831 ASSERT_NO_FATAL_FAILURE(
4832 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004837 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838 ASSERT_EQ(0, motionArgs.buttonState);
4839 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004840 ASSERT_NO_FATAL_FAILURE(
4841 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004842
4843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4844 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4845 ASSERT_EQ(0, motionArgs.buttonState);
4846 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004847 ASSERT_NO_FATAL_FAILURE(
4848 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004849
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4851 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4852 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4853}
4854
4855TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004857 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858
4859 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4860 mFakePointerController->setPosition(100, 200);
4861 mFakePointerController->setButtonState(0);
4862
4863 NotifyMotionArgs args;
4864
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004869 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4872 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 +01004873 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004874}
4875
4876TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004877 addConfigurationProperty("cursor.mode", "pointer");
4878 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004879 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004880
4881 NotifyDeviceResetArgs resetArgs;
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4883 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4884 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4885
4886 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4887 mFakePointerController->setPosition(100, 200);
4888 mFakePointerController->setButtonState(0);
4889
4890 NotifyMotionArgs args;
4891
4892 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4897 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4900 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 +01004901 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004902
4903 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4907 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4908 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4910 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4912 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4915 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4916
4917 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004918 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4919 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4921 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4924 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4926 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4927 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4929 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4930
4931 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4936 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4939 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 +01004940 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004941
4942 // Disable pointer capture and check that the device generation got bumped
4943 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004944 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004945 mFakePolicy->setPointerCapture(false);
4946 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004947 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004948
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004950 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4951
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4956 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4959 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 +01004960 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961}
4962
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004963/**
4964 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4965 * pointer acceleration or speed processing should not be applied.
4966 */
4967TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4968 addConfigurationProperty("cursor.mode", "pointer");
4969 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4970 100.f /*high threshold*/, 10.f /*acceleration*/);
4971 mFakePolicy->setVelocityControlParams(testParams);
4972 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4973
4974 NotifyDeviceResetArgs resetArgs;
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4976 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4977 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4978
4979 NotifyMotionArgs args;
4980
4981 // Move and verify scale is applied.
4982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4986 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4987 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4988 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4989 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4990 ASSERT_GT(relX, 10);
4991 ASSERT_GT(relY, 20);
4992
4993 // Enable Pointer Capture
4994 mFakePolicy->setPointerCapture(true);
4995 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4996 NotifyPointerCaptureChangedArgs captureArgs;
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4998 ASSERT_TRUE(captureArgs.request.enable);
4999
5000 // Move and verify scale is not applied.
5001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5005 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5007 ASSERT_EQ(10, args.pointerCoords[0].getX());
5008 ASSERT_EQ(20, args.pointerCoords[0].getY());
5009}
5010
Prabir Pradhan258e2b92022-06-24 18:37:04 +00005011TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5012 addConfigurationProperty("cursor.mode", "pointer");
5013 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5014
5015 NotifyDeviceResetArgs resetArgs;
5016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5017 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5018 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5019
5020 // Ensure the display is rotated.
5021 prepareDisplay(DISPLAY_ORIENTATION_90);
5022
5023 NotifyMotionArgs args;
5024
5025 // Verify that the coordinates are rotated.
5026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5030 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5032 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5033 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5034
5035 // Enable Pointer Capture.
5036 mFakePolicy->setPointerCapture(true);
5037 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5038 NotifyPointerCaptureChangedArgs captureArgs;
5039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5040 ASSERT_TRUE(captureArgs.request.enable);
5041
5042 // Move and verify rotation is not applied.
5043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5047 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5049 ASSERT_EQ(10, args.pointerCoords[0].getX());
5050 ASSERT_EQ(20, args.pointerCoords[0].getY());
5051}
5052
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005053TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005054 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005055
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005056 // Set up the default display.
5057 prepareDisplay(DISPLAY_ORIENTATION_90);
5058
5059 // Set up the secondary display as the display on which the pointer should be shown.
5060 // The InputDevice is not associated with any display.
5061 prepareSecondaryDisplay();
5062 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005063 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5064
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005065 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005066 mFakePointerController->setPosition(100, 200);
5067 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005068
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005069 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5074 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5075 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005076 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005077}
5078
5079TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5080 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5081
5082 // Set up the default display.
5083 prepareDisplay(DISPLAY_ORIENTATION_90);
5084
5085 // Set up the secondary display as the display on which the pointer should be shown,
5086 // and associate the InputDevice with the secondary display.
5087 prepareSecondaryDisplay();
5088 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5089 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5090 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5091
5092 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5093 mFakePointerController->setPosition(100, 200);
5094 mFakePointerController->setButtonState(0);
5095
5096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5100 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5101 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
5102 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5103}
5104
5105TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5106 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5107
5108 // Set up the default display as the display on which the pointer should be shown.
5109 prepareDisplay(DISPLAY_ORIENTATION_90);
5110 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5111
5112 // Associate the InputDevice with the secondary display.
5113 prepareSecondaryDisplay();
5114 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5115 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5116
5117 // The mapper should not generate any events because it is associated with a display that is
5118 // different from the pointer display.
5119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005123}
5124
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125// --- TouchInputMapperTest ---
5126
5127class TouchInputMapperTest : public InputMapperTest {
5128protected:
5129 static const int32_t RAW_X_MIN;
5130 static const int32_t RAW_X_MAX;
5131 static const int32_t RAW_Y_MIN;
5132 static const int32_t RAW_Y_MAX;
5133 static const int32_t RAW_TOUCH_MIN;
5134 static const int32_t RAW_TOUCH_MAX;
5135 static const int32_t RAW_TOOL_MIN;
5136 static const int32_t RAW_TOOL_MAX;
5137 static const int32_t RAW_PRESSURE_MIN;
5138 static const int32_t RAW_PRESSURE_MAX;
5139 static const int32_t RAW_ORIENTATION_MIN;
5140 static const int32_t RAW_ORIENTATION_MAX;
5141 static const int32_t RAW_DISTANCE_MIN;
5142 static const int32_t RAW_DISTANCE_MAX;
5143 static const int32_t RAW_TILT_MIN;
5144 static const int32_t RAW_TILT_MAX;
5145 static const int32_t RAW_ID_MIN;
5146 static const int32_t RAW_ID_MAX;
5147 static const int32_t RAW_SLOT_MIN;
5148 static const int32_t RAW_SLOT_MAX;
5149 static const float X_PRECISION;
5150 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005151 static const float X_PRECISION_VIRTUAL;
5152 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153
5154 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005155 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156
5157 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5158
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005159 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005160 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005161
Michael Wrightd02c5b62014-02-10 15:10:22 -08005162 enum Axes {
5163 POSITION = 1 << 0,
5164 TOUCH = 1 << 1,
5165 TOOL = 1 << 2,
5166 PRESSURE = 1 << 3,
5167 ORIENTATION = 1 << 4,
5168 MINOR = 1 << 5,
5169 ID = 1 << 6,
5170 DISTANCE = 1 << 7,
5171 TILT = 1 << 8,
5172 SLOT = 1 << 9,
5173 TOOL_TYPE = 1 << 10,
5174 };
5175
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005176 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5177 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005178 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005179 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005180 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181 int32_t toRawX(float displayX);
5182 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005183 int32_t toRotatedRawX(float displayX);
5184 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005185 float toCookedX(float rawX, float rawY);
5186 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005187 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005188 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005190 float toDisplayY(int32_t rawY, int32_t displayHeight);
5191
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192};
5193
5194const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5195const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5196const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5197const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5198const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5199const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5200const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5201const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005202const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5203const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5205const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5206const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5207const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5208const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5209const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5210const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5211const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5212const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5213const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5214const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5215const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005216const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5217 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5218const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5219 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005220const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5221 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222
5223const float TouchInputMapperTest::GEOMETRIC_SCALE =
5224 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5225 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5226
5227const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5228 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5229 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5230};
5231
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005232void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005233 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5234 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005235}
5236
5237void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5238 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5239 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240}
5241
Santos Cordonfa5cf462017-04-05 10:37:00 -07005242void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005243 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5244 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5245 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005246}
5247
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005249 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5250 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5251 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5252 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253}
5254
Jason Gerecke489fda82012-09-07 17:19:40 -07005255void TouchInputMapperTest::prepareLocationCalibration() {
5256 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5257}
5258
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259int32_t TouchInputMapperTest::toRawX(float displayX) {
5260 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5261}
5262
5263int32_t TouchInputMapperTest::toRawY(float displayY) {
5264 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5265}
5266
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005267int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5268 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5269}
5270
5271int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5272 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5273}
5274
Jason Gerecke489fda82012-09-07 17:19:40 -07005275float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5276 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5277 return rawX;
5278}
5279
5280float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5281 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5282 return rawY;
5283}
5284
Michael Wrightd02c5b62014-02-10 15:10:22 -08005285float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005286 return toDisplayX(rawX, DISPLAY_WIDTH);
5287}
5288
5289float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5290 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291}
5292
5293float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005294 return toDisplayY(rawY, DISPLAY_HEIGHT);
5295}
5296
5297float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5298 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299}
5300
5301
5302// --- SingleTouchInputMapperTest ---
5303
5304class SingleTouchInputMapperTest : public TouchInputMapperTest {
5305protected:
5306 void prepareButtons();
5307 void prepareAxes(int axes);
5308
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005309 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5310 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5311 void processUp(SingleTouchInputMapper& mappery);
5312 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5313 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5314 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5315 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5316 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5317 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318};
5319
5320void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005321 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322}
5323
5324void SingleTouchInputMapperTest::prepareAxes(int axes) {
5325 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005326 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5327 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328 }
5329 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005330 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5331 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005332 }
5333 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005334 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5335 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336 }
5337 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005338 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5339 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 }
5341 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005342 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5343 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344 }
5345}
5346
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005347void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351}
5352
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005353void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356}
5357
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005358void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360}
5361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005362void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364}
5365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5367 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369}
5370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005371void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373}
5374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5376 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379}
5380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5382 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384}
5385
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005386void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388}
5389
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391 prepareButtons();
5392 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005395 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396}
5397
5398TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005399 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5400 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401 prepareButtons();
5402 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005403 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005405 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406}
5407
5408TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409 prepareButtons();
5410 prepareAxes(POSITION);
5411 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005412 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005414 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415}
5416
5417TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418 prepareButtons();
5419 prepareAxes(POSITION);
5420 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005421 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005423 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424}
5425
5426TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005427 addConfigurationProperty("touch.deviceType", "touchScreen");
5428 prepareDisplay(DISPLAY_ORIENTATION_0);
5429 prepareButtons();
5430 prepareAxes(POSITION);
5431 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005432 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433
5434 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005435 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436
5437 // Virtual key is down.
5438 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5439 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5440 processDown(mapper, x, y);
5441 processSync(mapper);
5442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5443
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005444 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445
5446 // Virtual key is up.
5447 processUp(mapper);
5448 processSync(mapper);
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5450
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005451 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452}
5453
5454TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455 addConfigurationProperty("touch.deviceType", "touchScreen");
5456 prepareDisplay(DISPLAY_ORIENTATION_0);
5457 prepareButtons();
5458 prepareAxes(POSITION);
5459 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005460 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461
5462 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005463 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464
5465 // Virtual key is down.
5466 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5467 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5468 processDown(mapper, x, y);
5469 processSync(mapper);
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005472 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473
5474 // Virtual key is up.
5475 processUp(mapper);
5476 processSync(mapper);
5477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005479 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480}
5481
5482TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483 addConfigurationProperty("touch.deviceType", "touchScreen");
5484 prepareDisplay(DISPLAY_ORIENTATION_0);
5485 prepareButtons();
5486 prepareAxes(POSITION);
5487 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005488 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489
5490 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5491 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493 ASSERT_TRUE(flags[0]);
5494 ASSERT_FALSE(flags[1]);
5495}
5496
5497TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 addConfigurationProperty("touch.deviceType", "touchScreen");
5499 prepareDisplay(DISPLAY_ORIENTATION_0);
5500 prepareButtons();
5501 prepareAxes(POSITION);
5502 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005503 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504
arthurhungdcef2dc2020-08-11 14:47:50 +08005505 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506
5507 NotifyKeyArgs args;
5508
5509 // Press virtual key.
5510 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5511 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5512 processDown(mapper, x, y);
5513 processSync(mapper);
5514
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5516 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5517 ASSERT_EQ(DEVICE_ID, args.deviceId);
5518 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5519 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5520 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5521 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5522 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5523 ASSERT_EQ(KEY_HOME, args.scanCode);
5524 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5525 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5526
5527 // Release virtual key.
5528 processUp(mapper);
5529 processSync(mapper);
5530
5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5532 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5533 ASSERT_EQ(DEVICE_ID, args.deviceId);
5534 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5535 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5536 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5537 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5538 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5539 ASSERT_EQ(KEY_HOME, args.scanCode);
5540 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5541 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5542
5543 // Should not have sent any motions.
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5545}
5546
5547TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548 addConfigurationProperty("touch.deviceType", "touchScreen");
5549 prepareDisplay(DISPLAY_ORIENTATION_0);
5550 prepareButtons();
5551 prepareAxes(POSITION);
5552 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005553 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554
arthurhungdcef2dc2020-08-11 14:47:50 +08005555 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556
5557 NotifyKeyArgs keyArgs;
5558
5559 // Press virtual key.
5560 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5561 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5562 processDown(mapper, x, y);
5563 processSync(mapper);
5564
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5566 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5567 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5568 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5569 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5570 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5571 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5572 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5573 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5575 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5576
5577 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5578 // into the display area.
5579 y -= 100;
5580 processMove(mapper, x, y);
5581 processSync(mapper);
5582
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5584 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5585 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5586 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5587 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5588 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5589 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5590 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5591 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5592 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5593 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5594 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5595
5596 NotifyMotionArgs motionArgs;
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5598 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5599 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5600 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5601 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5602 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5603 ASSERT_EQ(0, motionArgs.flags);
5604 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5605 ASSERT_EQ(0, motionArgs.buttonState);
5606 ASSERT_EQ(0, motionArgs.edgeFlags);
5607 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5608 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5611 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5612 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5613 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5614 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5615
5616 // Keep moving out of bounds. Should generate a pointer move.
5617 y -= 50;
5618 processMove(mapper, x, y);
5619 processSync(mapper);
5620
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5622 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5623 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5624 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5625 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5627 ASSERT_EQ(0, motionArgs.flags);
5628 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5629 ASSERT_EQ(0, motionArgs.buttonState);
5630 ASSERT_EQ(0, motionArgs.edgeFlags);
5631 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5632 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5635 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5636 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5637 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5639
5640 // Release out of bounds. Should generate a pointer up.
5641 processUp(mapper);
5642 processSync(mapper);
5643
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5645 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5646 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5647 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5648 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5649 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5650 ASSERT_EQ(0, motionArgs.flags);
5651 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5652 ASSERT_EQ(0, motionArgs.buttonState);
5653 ASSERT_EQ(0, motionArgs.edgeFlags);
5654 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5655 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5658 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5659 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5660 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5661 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5662
5663 // Should not have sent any more keys or motions.
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5666}
5667
5668TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669 addConfigurationProperty("touch.deviceType", "touchScreen");
5670 prepareDisplay(DISPLAY_ORIENTATION_0);
5671 prepareButtons();
5672 prepareAxes(POSITION);
5673 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005674 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675
arthurhungdcef2dc2020-08-11 14:47:50 +08005676 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677
5678 NotifyMotionArgs motionArgs;
5679
5680 // Initially go down out of bounds.
5681 int32_t x = -10;
5682 int32_t y = -10;
5683 processDown(mapper, x, y);
5684 processSync(mapper);
5685
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5687
5688 // Move into the display area. Should generate a pointer down.
5689 x = 50;
5690 y = 75;
5691 processMove(mapper, x, y);
5692 processSync(mapper);
5693
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5695 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5696 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5697 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5698 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5699 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5700 ASSERT_EQ(0, motionArgs.flags);
5701 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5702 ASSERT_EQ(0, motionArgs.buttonState);
5703 ASSERT_EQ(0, motionArgs.edgeFlags);
5704 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5705 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5708 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5709 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5710 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5711 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5712
5713 // Release. Should generate a pointer up.
5714 processUp(mapper);
5715 processSync(mapper);
5716
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5718 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5719 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5720 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5721 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5722 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5723 ASSERT_EQ(0, motionArgs.flags);
5724 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5725 ASSERT_EQ(0, motionArgs.buttonState);
5726 ASSERT_EQ(0, motionArgs.edgeFlags);
5727 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5728 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5731 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5732 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5733 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5734 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5735
5736 // Should not have sent any more keys or motions.
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5739}
5740
Santos Cordonfa5cf462017-04-05 10:37:00 -07005741TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005742 addConfigurationProperty("touch.deviceType", "touchScreen");
5743 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5744
5745 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5746 prepareButtons();
5747 prepareAxes(POSITION);
5748 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005749 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005750
arthurhungdcef2dc2020-08-11 14:47:50 +08005751 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005752
5753 NotifyMotionArgs motionArgs;
5754
5755 // Down.
5756 int32_t x = 100;
5757 int32_t y = 125;
5758 processDown(mapper, x, y);
5759 processSync(mapper);
5760
5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5762 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5763 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5764 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5765 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5766 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5767 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5768 ASSERT_EQ(0, motionArgs.flags);
5769 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5770 ASSERT_EQ(0, motionArgs.buttonState);
5771 ASSERT_EQ(0, motionArgs.edgeFlags);
5772 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5776 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5777 1, 0, 0, 0, 0, 0, 0, 0));
5778 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5779 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5780 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5781
5782 // Move.
5783 x += 50;
5784 y += 75;
5785 processMove(mapper, x, y);
5786 processSync(mapper);
5787
5788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5789 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5790 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5791 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5792 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5793 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5795 ASSERT_EQ(0, motionArgs.flags);
5796 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5797 ASSERT_EQ(0, motionArgs.buttonState);
5798 ASSERT_EQ(0, motionArgs.edgeFlags);
5799 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5800 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5803 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5804 1, 0, 0, 0, 0, 0, 0, 0));
5805 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5806 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5808
5809 // Up.
5810 processUp(mapper);
5811 processSync(mapper);
5812
5813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5814 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5815 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5816 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5817 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5818 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5819 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5820 ASSERT_EQ(0, motionArgs.flags);
5821 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5822 ASSERT_EQ(0, motionArgs.buttonState);
5823 ASSERT_EQ(0, motionArgs.edgeFlags);
5824 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5825 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5828 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5829 1, 0, 0, 0, 0, 0, 0, 0));
5830 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5831 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5832 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5833
5834 // Should not have sent any more keys or motions.
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5837}
5838
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005840 addConfigurationProperty("touch.deviceType", "touchScreen");
5841 prepareDisplay(DISPLAY_ORIENTATION_0);
5842 prepareButtons();
5843 prepareAxes(POSITION);
5844 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005845 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005846
arthurhungdcef2dc2020-08-11 14:47:50 +08005847 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848
5849 NotifyMotionArgs motionArgs;
5850
5851 // Down.
5852 int32_t x = 100;
5853 int32_t y = 125;
5854 processDown(mapper, x, y);
5855 processSync(mapper);
5856
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5858 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5859 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5860 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5861 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5863 ASSERT_EQ(0, motionArgs.flags);
5864 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5865 ASSERT_EQ(0, motionArgs.buttonState);
5866 ASSERT_EQ(0, motionArgs.edgeFlags);
5867 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5868 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5871 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5872 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5873 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5874 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5875
5876 // Move.
5877 x += 50;
5878 y += 75;
5879 processMove(mapper, x, y);
5880 processSync(mapper);
5881
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5883 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5884 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5885 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5886 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5888 ASSERT_EQ(0, motionArgs.flags);
5889 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5890 ASSERT_EQ(0, motionArgs.buttonState);
5891 ASSERT_EQ(0, motionArgs.edgeFlags);
5892 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5893 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5894 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5896 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5897 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5898 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5899 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5900
5901 // Up.
5902 processUp(mapper);
5903 processSync(mapper);
5904
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5906 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5907 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5908 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5909 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5911 ASSERT_EQ(0, motionArgs.flags);
5912 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5913 ASSERT_EQ(0, motionArgs.buttonState);
5914 ASSERT_EQ(0, motionArgs.edgeFlags);
5915 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5916 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5919 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5920 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5921 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5922 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5923
5924 // Should not have sent any more keys or motions.
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5927}
5928
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005929TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930 addConfigurationProperty("touch.deviceType", "touchScreen");
5931 prepareButtons();
5932 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005933 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5934 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005935 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936
5937 NotifyMotionArgs args;
5938
5939 // Rotation 90.
5940 prepareDisplay(DISPLAY_ORIENTATION_90);
5941 processDown(mapper, toRawX(50), toRawY(75));
5942 processSync(mapper);
5943
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5945 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5946 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5947
5948 processUp(mapper);
5949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5951}
5952
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005953TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954 addConfigurationProperty("touch.deviceType", "touchScreen");
5955 prepareButtons();
5956 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005957 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5958 // orientation-aware are affected by display rotation.
5959 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005960 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961
5962 NotifyMotionArgs args;
5963
5964 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005965 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966 prepareDisplay(DISPLAY_ORIENTATION_0);
5967 processDown(mapper, toRawX(50), toRawY(75));
5968 processSync(mapper);
5969
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5971 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5972 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5973
5974 processUp(mapper);
5975 processSync(mapper);
5976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5977
5978 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005979 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005981 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982 processSync(mapper);
5983
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5985 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5986 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5987
5988 processUp(mapper);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5991
5992 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005993 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 prepareDisplay(DISPLAY_ORIENTATION_180);
5995 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5996 processSync(mapper);
5997
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5999 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6000 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6001
6002 processUp(mapper);
6003 processSync(mapper);
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6005
6006 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006007 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006009 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010 processSync(mapper);
6011
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6013 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6014 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6015
6016 processUp(mapper);
6017 processSync(mapper);
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6019}
6020
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006021TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6022 addConfigurationProperty("touch.deviceType", "touchScreen");
6023 prepareButtons();
6024 prepareAxes(POSITION);
6025 addConfigurationProperty("touch.orientationAware", "1");
6026 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6027 clearViewports();
6028 prepareDisplay(DISPLAY_ORIENTATION_0);
6029 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6030 NotifyMotionArgs args;
6031
6032 // Orientation 0.
6033 processDown(mapper, toRawX(50), toRawY(75));
6034 processSync(mapper);
6035
6036 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6037 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6038 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6039
6040 processUp(mapper);
6041 processSync(mapper);
6042 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6043}
6044
6045TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6046 addConfigurationProperty("touch.deviceType", "touchScreen");
6047 prepareButtons();
6048 prepareAxes(POSITION);
6049 addConfigurationProperty("touch.orientationAware", "1");
6050 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6051 clearViewports();
6052 prepareDisplay(DISPLAY_ORIENTATION_0);
6053 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6054 NotifyMotionArgs args;
6055
6056 // Orientation 90.
6057 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6058 processSync(mapper);
6059
6060 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6061 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6062 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6063
6064 processUp(mapper);
6065 processSync(mapper);
6066 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6067}
6068
6069TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6070 addConfigurationProperty("touch.deviceType", "touchScreen");
6071 prepareButtons();
6072 prepareAxes(POSITION);
6073 addConfigurationProperty("touch.orientationAware", "1");
6074 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6075 clearViewports();
6076 prepareDisplay(DISPLAY_ORIENTATION_0);
6077 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6078 NotifyMotionArgs args;
6079
6080 // Orientation 180.
6081 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6082 processSync(mapper);
6083
6084 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6085 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6086 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6087
6088 processUp(mapper);
6089 processSync(mapper);
6090 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6091}
6092
6093TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6094 addConfigurationProperty("touch.deviceType", "touchScreen");
6095 prepareButtons();
6096 prepareAxes(POSITION);
6097 addConfigurationProperty("touch.orientationAware", "1");
6098 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6099 clearViewports();
6100 prepareDisplay(DISPLAY_ORIENTATION_0);
6101 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6102 NotifyMotionArgs args;
6103
6104 // Orientation 270.
6105 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6106 processSync(mapper);
6107
6108 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6109 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6110 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6111
6112 processUp(mapper);
6113 processSync(mapper);
6114 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6115}
6116
6117TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6118 addConfigurationProperty("touch.deviceType", "touchScreen");
6119 prepareButtons();
6120 prepareAxes(POSITION);
6121 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6122 // orientation-aware are affected by display rotation.
6123 addConfigurationProperty("touch.orientationAware", "0");
6124 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6125 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6126
6127 NotifyMotionArgs args;
6128
6129 // Orientation 90, Rotation 0.
6130 clearViewports();
6131 prepareDisplay(DISPLAY_ORIENTATION_0);
6132 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6133 processSync(mapper);
6134
6135 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6136 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6137 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6138
6139 processUp(mapper);
6140 processSync(mapper);
6141 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6142
6143 // Orientation 90, Rotation 90.
6144 clearViewports();
6145 prepareDisplay(DISPLAY_ORIENTATION_90);
6146 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6147 processSync(mapper);
6148
6149 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6150 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6151 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6152
6153 processUp(mapper);
6154 processSync(mapper);
6155 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6156
6157 // Orientation 90, Rotation 180.
6158 clearViewports();
6159 prepareDisplay(DISPLAY_ORIENTATION_180);
6160 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6161 processSync(mapper);
6162
6163 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6164 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6165 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6166
6167 processUp(mapper);
6168 processSync(mapper);
6169 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6170
6171 // Orientation 90, Rotation 270.
6172 clearViewports();
6173 prepareDisplay(DISPLAY_ORIENTATION_270);
6174 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6175 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6176 processSync(mapper);
6177
6178 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6179 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6180 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6181
6182 processUp(mapper);
6183 processSync(mapper);
6184 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6185}
6186
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188 addConfigurationProperty("touch.deviceType", "touchScreen");
6189 prepareDisplay(DISPLAY_ORIENTATION_0);
6190 prepareButtons();
6191 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006192 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193
6194 // These calculations are based on the input device calibration documentation.
6195 int32_t rawX = 100;
6196 int32_t rawY = 200;
6197 int32_t rawPressure = 10;
6198 int32_t rawToolMajor = 12;
6199 int32_t rawDistance = 2;
6200 int32_t rawTiltX = 30;
6201 int32_t rawTiltY = 110;
6202
6203 float x = toDisplayX(rawX);
6204 float y = toDisplayY(rawY);
6205 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6206 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6207 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6208 float distance = float(rawDistance);
6209
6210 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6211 float tiltScale = M_PI / 180;
6212 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6213 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6214 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6215 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6216
6217 processDown(mapper, rawX, rawY);
6218 processPressure(mapper, rawPressure);
6219 processToolMajor(mapper, rawToolMajor);
6220 processDistance(mapper, rawDistance);
6221 processTilt(mapper, rawTiltX, rawTiltY);
6222 processSync(mapper);
6223
6224 NotifyMotionArgs args;
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6227 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6228 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6229}
6230
Jason Gerecke489fda82012-09-07 17:19:40 -07006231TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006232 addConfigurationProperty("touch.deviceType", "touchScreen");
6233 prepareDisplay(DISPLAY_ORIENTATION_0);
6234 prepareLocationCalibration();
6235 prepareButtons();
6236 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006237 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006238
6239 int32_t rawX = 100;
6240 int32_t rawY = 200;
6241
6242 float x = toDisplayX(toCookedX(rawX, rawY));
6243 float y = toDisplayY(toCookedY(rawX, rawY));
6244
6245 processDown(mapper, rawX, rawY);
6246 processSync(mapper);
6247
6248 NotifyMotionArgs args;
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6251 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6252}
6253
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255 addConfigurationProperty("touch.deviceType", "touchScreen");
6256 prepareDisplay(DISPLAY_ORIENTATION_0);
6257 prepareButtons();
6258 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006259 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260
6261 NotifyMotionArgs motionArgs;
6262 NotifyKeyArgs keyArgs;
6263
6264 processDown(mapper, 100, 200);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6268 ASSERT_EQ(0, motionArgs.buttonState);
6269
6270 // press BTN_LEFT, release BTN_LEFT
6271 processKey(mapper, BTN_LEFT, 1);
6272 processSync(mapper);
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6275 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6276
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6279 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6280
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281 processKey(mapper, BTN_LEFT, 0);
6282 processSync(mapper);
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006286
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006289 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290
6291 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6292 processKey(mapper, BTN_RIGHT, 1);
6293 processKey(mapper, BTN_MIDDLE, 1);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6298 motionArgs.buttonState);
6299
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6302 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6303
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6306 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6307 motionArgs.buttonState);
6308
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309 processKey(mapper, BTN_RIGHT, 0);
6310 processSync(mapper);
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006314
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318
6319 processKey(mapper, BTN_MIDDLE, 0);
6320 processSync(mapper);
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006324
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006327 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328
6329 // press BTN_BACK, release BTN_BACK
6330 processKey(mapper, BTN_BACK, 1);
6331 processSync(mapper);
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6333 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6334 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006338 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6339
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6342 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343
6344 processKey(mapper, BTN_BACK, 0);
6345 processSync(mapper);
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006352 ASSERT_EQ(0, motionArgs.buttonState);
6353
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6355 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6356 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6357
6358 // press BTN_SIDE, release BTN_SIDE
6359 processKey(mapper, BTN_SIDE, 1);
6360 processSync(mapper);
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6362 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6363 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006364
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006367 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6368
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6371 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372
6373 processKey(mapper, BTN_SIDE, 0);
6374 processSync(mapper);
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006378
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006381 ASSERT_EQ(0, motionArgs.buttonState);
6382
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6384 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6385 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6386
6387 // press BTN_FORWARD, release BTN_FORWARD
6388 processKey(mapper, BTN_FORWARD, 1);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6391 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6392 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006393
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006396 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6397
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6400 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401
6402 processKey(mapper, BTN_FORWARD, 0);
6403 processSync(mapper);
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006405 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006407
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006410 ASSERT_EQ(0, motionArgs.buttonState);
6411
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6413 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6414 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6415
6416 // press BTN_EXTRA, release BTN_EXTRA
6417 processKey(mapper, BTN_EXTRA, 1);
6418 processSync(mapper);
6419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6420 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6421 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006422
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006425 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6426
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6429 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006430
6431 processKey(mapper, BTN_EXTRA, 0);
6432 processSync(mapper);
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006434 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006435 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006436
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439 ASSERT_EQ(0, motionArgs.buttonState);
6440
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6442 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6443 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6444
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6446
Michael Wrightd02c5b62014-02-10 15:10:22 -08006447 // press BTN_STYLUS, release BTN_STYLUS
6448 processKey(mapper, BTN_STYLUS, 1);
6449 processSync(mapper);
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006452 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6453
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6456 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457
6458 processKey(mapper, BTN_STYLUS, 0);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006461 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006463
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006466 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006467
6468 // press BTN_STYLUS2, release BTN_STYLUS2
6469 processKey(mapper, BTN_STYLUS2, 1);
6470 processSync(mapper);
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006473 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6474
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6477 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478
6479 processKey(mapper, BTN_STYLUS2, 0);
6480 processSync(mapper);
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006482 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006484
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006487 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006488
6489 // release touch
6490 processUp(mapper);
6491 processSync(mapper);
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6493 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6494 ASSERT_EQ(0, motionArgs.buttonState);
6495}
6496
6497TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498 addConfigurationProperty("touch.deviceType", "touchScreen");
6499 prepareDisplay(DISPLAY_ORIENTATION_0);
6500 prepareButtons();
6501 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006502 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006503
6504 NotifyMotionArgs motionArgs;
6505
6506 // default tool type is finger
6507 processDown(mapper, 100, 200);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6512
6513 // eraser
6514 processKey(mapper, BTN_TOOL_RUBBER, 1);
6515 processSync(mapper);
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6519
6520 // stylus
6521 processKey(mapper, BTN_TOOL_RUBBER, 0);
6522 processKey(mapper, BTN_TOOL_PEN, 1);
6523 processSync(mapper);
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6527
6528 // brush
6529 processKey(mapper, BTN_TOOL_PEN, 0);
6530 processKey(mapper, BTN_TOOL_BRUSH, 1);
6531 processSync(mapper);
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6535
6536 // pencil
6537 processKey(mapper, BTN_TOOL_BRUSH, 0);
6538 processKey(mapper, BTN_TOOL_PENCIL, 1);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6543
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006544 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006545 processKey(mapper, BTN_TOOL_PENCIL, 0);
6546 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6551
6552 // mouse
6553 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6554 processKey(mapper, BTN_TOOL_MOUSE, 1);
6555 processSync(mapper);
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6559
6560 // lens
6561 processKey(mapper, BTN_TOOL_MOUSE, 0);
6562 processKey(mapper, BTN_TOOL_LENS, 1);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6567
6568 // double-tap
6569 processKey(mapper, BTN_TOOL_LENS, 0);
6570 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6575
6576 // triple-tap
6577 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6578 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6579 processSync(mapper);
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6583
6584 // quad-tap
6585 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6586 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6587 processSync(mapper);
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6591
6592 // finger
6593 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6594 processKey(mapper, BTN_TOOL_FINGER, 1);
6595 processSync(mapper);
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6599
6600 // stylus trumps finger
6601 processKey(mapper, BTN_TOOL_PEN, 1);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6606
6607 // eraser trumps stylus
6608 processKey(mapper, BTN_TOOL_RUBBER, 1);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6613
6614 // mouse trumps eraser
6615 processKey(mapper, BTN_TOOL_MOUSE, 1);
6616 processSync(mapper);
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6620
6621 // back to default tool type
6622 processKey(mapper, BTN_TOOL_MOUSE, 0);
6623 processKey(mapper, BTN_TOOL_RUBBER, 0);
6624 processKey(mapper, BTN_TOOL_PEN, 0);
6625 processKey(mapper, BTN_TOOL_FINGER, 0);
6626 processSync(mapper);
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6630}
6631
6632TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006633 addConfigurationProperty("touch.deviceType", "touchScreen");
6634 prepareDisplay(DISPLAY_ORIENTATION_0);
6635 prepareButtons();
6636 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006637 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006638 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006639
6640 NotifyMotionArgs motionArgs;
6641
6642 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6643 processKey(mapper, BTN_TOOL_FINGER, 1);
6644 processMove(mapper, 100, 200);
6645 processSync(mapper);
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6649 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6650
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6654 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6655
6656 // move a little
6657 processMove(mapper, 150, 250);
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6662 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6663
6664 // down when BTN_TOUCH is pressed, pressure defaults to 1
6665 processKey(mapper, BTN_TOUCH, 1);
6666 processSync(mapper);
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6671
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6673 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6675 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6676
6677 // up when BTN_TOUCH is released, hover restored
6678 processKey(mapper, BTN_TOUCH, 0);
6679 processSync(mapper);
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6683 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6684
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6688 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6689
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6691 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6693 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6694
6695 // exit hover when pointer goes away
6696 processKey(mapper, BTN_TOOL_FINGER, 0);
6697 processSync(mapper);
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
6704TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705 addConfigurationProperty("touch.deviceType", "touchScreen");
6706 prepareDisplay(DISPLAY_ORIENTATION_0);
6707 prepareButtons();
6708 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006709 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006710
6711 NotifyMotionArgs motionArgs;
6712
6713 // initially hovering because pressure is 0
6714 processDown(mapper, 100, 200);
6715 processPressure(mapper, 0);
6716 processSync(mapper);
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6718 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6720 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6721
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6723 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6725 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6726
6727 // move a little
6728 processMove(mapper, 150, 250);
6729 processSync(mapper);
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6733 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6734
6735 // down when pressure is non-zero
6736 processPressure(mapper, RAW_PRESSURE_MAX);
6737 processSync(mapper);
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6742
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6744 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6746 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6747
6748 // up when pressure becomes 0, hover restored
6749 processPressure(mapper, 0);
6750 processSync(mapper);
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6754 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6755
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6759 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6760
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6762 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6764 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6765
6766 // exit hover when pointer goes away
6767 processUp(mapper);
6768 processSync(mapper);
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
lilinnan687e58f2022-07-19 16:00:50 +08006775TEST_F(SingleTouchInputMapperTest,
6776 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6777 addConfigurationProperty("touch.deviceType", "touchScreen");
6778 prepareDisplay(DISPLAY_ORIENTATION_0);
6779 prepareButtons();
6780 prepareAxes(POSITION);
6781 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6782 NotifyMotionArgs motionArgs;
6783
6784 // Down.
6785 int32_t x = 100;
6786 int32_t y = 200;
6787 processDown(mapper, x, y);
6788 processSync(mapper);
6789
6790 // We should receive a down event
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6793
6794 // Change display id
6795 clearViewports();
6796 prepareSecondaryDisplay(ViewportType::INTERNAL);
6797
6798 // We should receive a cancel event
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6801 // Then receive reset called
6802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6803}
6804
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006805TEST_F(SingleTouchInputMapperTest,
6806 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6807 addConfigurationProperty("touch.deviceType", "touchScreen");
6808 prepareDisplay(DISPLAY_ORIENTATION_0);
6809 prepareButtons();
6810 prepareAxes(POSITION);
6811 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6813 NotifyMotionArgs motionArgs;
6814
6815 // Start a new gesture.
6816 processDown(mapper, 100, 200);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6819 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6820
6821 // Make the viewport inactive. This will put the device in disabled mode.
6822 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6823 viewport->isActive = false;
6824 mFakePolicy->updateViewport(*viewport);
6825 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6826
6827 // We should receive a cancel event for the ongoing gesture.
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6829 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6830 // Then we should be notified that the device was reset.
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6832
6833 // No events are generated while the viewport is inactive.
6834 processMove(mapper, 101, 201);
6835 processSync(mapper);
6836 processDown(mapper, 102, 202);
6837 processSync(mapper);
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6839
6840 // Make the viewport active again. The device should resume processing events.
6841 viewport->isActive = true;
6842 mFakePolicy->updateViewport(*viewport);
6843 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6844
6845 // The device is reset because it changes back to direct mode, without generating any events.
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6848
6849 // Start a new gesture.
6850 processDown(mapper, 100, 200);
6851 processSync(mapper);
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6853 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6854
6855 // No more events.
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6858}
6859
Prabir Pradhan5632d622021-09-06 07:57:20 -07006860// --- TouchDisplayProjectionTest ---
6861
6862class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6863public:
6864 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6865 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6866 // rotated equivalent of the given un-rotated physical display bounds.
6867 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6868 uint32_t inverseRotationFlags;
6869 auto width = DISPLAY_WIDTH;
6870 auto height = DISPLAY_HEIGHT;
6871 switch (orientation) {
6872 case DISPLAY_ORIENTATION_90:
6873 inverseRotationFlags = ui::Transform::ROT_270;
6874 std::swap(width, height);
6875 break;
6876 case DISPLAY_ORIENTATION_180:
6877 inverseRotationFlags = ui::Transform::ROT_180;
6878 break;
6879 case DISPLAY_ORIENTATION_270:
6880 inverseRotationFlags = ui::Transform::ROT_90;
6881 std::swap(width, height);
6882 break;
6883 case DISPLAY_ORIENTATION_0:
6884 inverseRotationFlags = ui::Transform::ROT_0;
6885 break;
6886 default:
6887 FAIL() << "Invalid orientation: " << orientation;
6888 }
6889
6890 const ui::Transform rotation(inverseRotationFlags, width, height);
6891 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6892
6893 std::optional<DisplayViewport> internalViewport =
6894 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6895 DisplayViewport& v = *internalViewport;
6896 v.displayId = DISPLAY_ID;
6897 v.orientation = orientation;
6898
6899 v.logicalLeft = 0;
6900 v.logicalTop = 0;
6901 v.logicalRight = 100;
6902 v.logicalBottom = 100;
6903
6904 v.physicalLeft = rotatedPhysicalDisplay.left;
6905 v.physicalTop = rotatedPhysicalDisplay.top;
6906 v.physicalRight = rotatedPhysicalDisplay.right;
6907 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6908
6909 v.deviceWidth = width;
6910 v.deviceHeight = height;
6911
6912 v.isActive = true;
6913 v.uniqueId = UNIQUE_ID;
6914 v.type = ViewportType::INTERNAL;
6915 mFakePolicy->updateViewport(v);
6916 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6917 }
6918
6919 void assertReceivedMove(const Point& point) {
6920 NotifyMotionArgs motionArgs;
6921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6923 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6925 1, 0, 0, 0, 0, 0, 0, 0));
6926 }
6927};
6928
6929TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6930 addConfigurationProperty("touch.deviceType", "touchScreen");
6931 prepareDisplay(DISPLAY_ORIENTATION_0);
6932
6933 prepareButtons();
6934 prepareAxes(POSITION);
6935 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6936
6937 NotifyMotionArgs motionArgs;
6938
6939 // Configure the DisplayViewport such that the logical display maps to a subsection of
6940 // the display panel called the physical display. Here, the physical display is bounded by the
6941 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6942 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6943 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6944 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6945
6946 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6947 DISPLAY_ORIENTATION_270}) {
6948 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6949
6950 // Touches outside the physical display should be ignored, and should not generate any
6951 // events. Ensure touches at the following points that lie outside of the physical display
6952 // area do not generate any events.
6953 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6954 processDown(mapper, toRawX(point.x), toRawY(point.y));
6955 processSync(mapper);
6956 processUp(mapper);
6957 processSync(mapper);
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6959 << "Unexpected event generated for touch outside physical display at point: "
6960 << point.x << ", " << point.y;
6961 }
6962 }
6963}
6964
6965TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6966 addConfigurationProperty("touch.deviceType", "touchScreen");
6967 prepareDisplay(DISPLAY_ORIENTATION_0);
6968
6969 prepareButtons();
6970 prepareAxes(POSITION);
6971 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6972
6973 NotifyMotionArgs motionArgs;
6974
6975 // Configure the DisplayViewport such that the logical display maps to a subsection of
6976 // the display panel called the physical display. Here, the physical display is bounded by the
6977 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6978 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6979
6980 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6981 DISPLAY_ORIENTATION_270}) {
6982 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6983
6984 // Touches that start outside the physical display should be ignored until it enters the
6985 // physical display bounds, at which point it should generate a down event. Start a touch at
6986 // the point (5, 100), which is outside the physical display bounds.
6987 static const Point kOutsidePoint{5, 100};
6988 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6989 processSync(mapper);
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6991
6992 // Move the touch into the physical display area. This should generate a pointer down.
6993 processMove(mapper, toRawX(11), toRawY(21));
6994 processSync(mapper);
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6996 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6997 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6998 ASSERT_NO_FATAL_FAILURE(
6999 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7000
7001 // Move the touch inside the physical display area. This should generate a pointer move.
7002 processMove(mapper, toRawX(69), toRawY(159));
7003 processSync(mapper);
7004 assertReceivedMove({69, 159});
7005
7006 // Move outside the physical display area. Since the pointer is already down, this should
7007 // now continue generating events.
7008 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7009 processSync(mapper);
7010 assertReceivedMove(kOutsidePoint);
7011
7012 // Release. This should generate a pointer up.
7013 processUp(mapper);
7014 processSync(mapper);
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7016 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7018 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7019
7020 // Ensure no more events were generated.
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7023 }
7024}
7025
Michael Wrightd02c5b62014-02-10 15:10:22 -08007026// --- MultiTouchInputMapperTest ---
7027
7028class MultiTouchInputMapperTest : public TouchInputMapperTest {
7029protected:
7030 void prepareAxes(int axes);
7031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007032 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7033 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7034 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7035 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7036 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7037 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7038 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7039 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7040 void processId(MultiTouchInputMapper& mapper, int32_t id);
7041 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7042 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7043 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7044 void processMTSync(MultiTouchInputMapper& mapper);
7045 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007046};
7047
7048void MultiTouchInputMapperTest::prepareAxes(int axes) {
7049 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007050 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7051 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007052 }
7053 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007054 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7055 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007056 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007057 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7058 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007059 }
7060 }
7061 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007062 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7063 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007064 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007065 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007066 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 }
7068 }
7069 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7071 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007072 }
7073 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007074 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7075 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007076 }
7077 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007078 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7079 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007080 }
7081 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007082 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7083 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084 }
7085 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007086 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7087 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007088 }
7089 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007090 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091 }
7092}
7093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007094void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7095 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098}
7099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007100void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7101 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103}
7104
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007105void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7106 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108}
7109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007110void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112}
7113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007114void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116}
7117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007118void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7119 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007121}
7122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007123void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007125}
7126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007127void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129}
7130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007131void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007133}
7134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007135void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137}
7138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007139void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141}
7142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007143void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7144 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146}
7147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007148void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150}
7151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007152void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154}
7155
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157 addConfigurationProperty("touch.deviceType", "touchScreen");
7158 prepareDisplay(DISPLAY_ORIENTATION_0);
7159 prepareAxes(POSITION);
7160 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007161 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162
arthurhungdcef2dc2020-08-11 14:47:50 +08007163 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007164
7165 NotifyMotionArgs motionArgs;
7166
7167 // Two fingers down at once.
7168 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7169 processPosition(mapper, x1, y1);
7170 processMTSync(mapper);
7171 processPosition(mapper, x2, y2);
7172 processMTSync(mapper);
7173 processSync(mapper);
7174
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7176 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7177 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7178 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7179 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7180 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7181 ASSERT_EQ(0, motionArgs.flags);
7182 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7183 ASSERT_EQ(0, motionArgs.buttonState);
7184 ASSERT_EQ(0, motionArgs.edgeFlags);
7185 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7186 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7189 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7190 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7191 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7192 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7193
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7195 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7196 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7197 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7198 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007199 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200 ASSERT_EQ(0, motionArgs.flags);
7201 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7202 ASSERT_EQ(0, motionArgs.buttonState);
7203 ASSERT_EQ(0, motionArgs.edgeFlags);
7204 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7205 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7207 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7210 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7212 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7213 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7214 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7215 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7216
7217 // Move.
7218 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7219 processPosition(mapper, x1, y1);
7220 processMTSync(mapper);
7221 processPosition(mapper, x2, y2);
7222 processMTSync(mapper);
7223 processSync(mapper);
7224
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7226 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7227 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7228 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7229 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7231 ASSERT_EQ(0, motionArgs.flags);
7232 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7233 ASSERT_EQ(0, motionArgs.buttonState);
7234 ASSERT_EQ(0, motionArgs.edgeFlags);
7235 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7236 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7238 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7241 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7243 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7244 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7245 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7247
7248 // First finger up.
7249 x2 += 15; y2 -= 20;
7250 processPosition(mapper, x2, y2);
7251 processMTSync(mapper);
7252 processSync(mapper);
7253
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7255 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7256 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7257 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7258 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007259 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007260 ASSERT_EQ(0, motionArgs.flags);
7261 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7262 ASSERT_EQ(0, motionArgs.buttonState);
7263 ASSERT_EQ(0, motionArgs.edgeFlags);
7264 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7265 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7267 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7270 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7272 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7273 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7274 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7275 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7276
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7278 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7279 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7280 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7281 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7283 ASSERT_EQ(0, motionArgs.flags);
7284 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7285 ASSERT_EQ(0, motionArgs.buttonState);
7286 ASSERT_EQ(0, motionArgs.edgeFlags);
7287 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7288 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7291 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7292 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7293 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7295
7296 // Move.
7297 x2 += 20; y2 -= 25;
7298 processPosition(mapper, x2, y2);
7299 processMTSync(mapper);
7300 processSync(mapper);
7301
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7303 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7304 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7305 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7306 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7308 ASSERT_EQ(0, motionArgs.flags);
7309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7310 ASSERT_EQ(0, motionArgs.buttonState);
7311 ASSERT_EQ(0, motionArgs.edgeFlags);
7312 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7313 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7316 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7317 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7318 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7320
7321 // New finger down.
7322 int32_t x3 = 700, y3 = 300;
7323 processPosition(mapper, x2, y2);
7324 processMTSync(mapper);
7325 processPosition(mapper, x3, y3);
7326 processMTSync(mapper);
7327 processSync(mapper);
7328
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7330 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7331 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7332 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7333 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007334 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007335 ASSERT_EQ(0, motionArgs.flags);
7336 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7337 ASSERT_EQ(0, motionArgs.buttonState);
7338 ASSERT_EQ(0, motionArgs.edgeFlags);
7339 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7342 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7345 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7347 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7348 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7349 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7351
7352 // Second finger up.
7353 x3 += 30; y3 -= 20;
7354 processPosition(mapper, x3, y3);
7355 processMTSync(mapper);
7356 processSync(mapper);
7357
7358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7359 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7360 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7361 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7362 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007363 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007364 ASSERT_EQ(0, motionArgs.flags);
7365 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7366 ASSERT_EQ(0, motionArgs.buttonState);
7367 ASSERT_EQ(0, motionArgs.edgeFlags);
7368 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7369 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7371 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7374 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7376 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7377 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7378 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7380
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7383 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7384 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7385 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7387 ASSERT_EQ(0, motionArgs.flags);
7388 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7389 ASSERT_EQ(0, motionArgs.buttonState);
7390 ASSERT_EQ(0, motionArgs.edgeFlags);
7391 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7392 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7395 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7396 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7397 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7399
7400 // Last finger up.
7401 processMTSync(mapper);
7402 processSync(mapper);
7403
7404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7406 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7407 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7408 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7409 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7410 ASSERT_EQ(0, motionArgs.flags);
7411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7412 ASSERT_EQ(0, motionArgs.buttonState);
7413 ASSERT_EQ(0, motionArgs.edgeFlags);
7414 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7415 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7418 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7419 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7420 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7421 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7422
7423 // Should not have sent any more keys or motions.
7424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7426}
7427
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007428TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7429 addConfigurationProperty("touch.deviceType", "touchScreen");
7430 prepareDisplay(DISPLAY_ORIENTATION_0);
7431
7432 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7433 /*fuzz*/ 0, /*resolution*/ 10);
7434 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7435 /*fuzz*/ 0, /*resolution*/ 11);
7436 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7437 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7438 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7439 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7440 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7441 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7442 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7443 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7444
7445 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7446
7447 // X and Y axes
7448 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7449 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7450 // Touch major and minor
7451 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7452 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7453 // Tool major and minor
7454 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7455 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7456}
7457
7458TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7459 addConfigurationProperty("touch.deviceType", "touchScreen");
7460 prepareDisplay(DISPLAY_ORIENTATION_0);
7461
7462 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7463 /*fuzz*/ 0, /*resolution*/ 10);
7464 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7465 /*fuzz*/ 0, /*resolution*/ 11);
7466
7467 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7468
7469 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7470
7471 // Touch major and minor
7472 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7473 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7474 // Tool major and minor
7475 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7476 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7477}
7478
Michael Wrightd02c5b62014-02-10 15:10:22 -08007479TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007480 addConfigurationProperty("touch.deviceType", "touchScreen");
7481 prepareDisplay(DISPLAY_ORIENTATION_0);
7482 prepareAxes(POSITION | ID);
7483 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007484 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007485
arthurhungdcef2dc2020-08-11 14:47:50 +08007486 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007487
7488 NotifyMotionArgs motionArgs;
7489
7490 // Two fingers down at once.
7491 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7492 processPosition(mapper, x1, y1);
7493 processId(mapper, 1);
7494 processMTSync(mapper);
7495 processPosition(mapper, x2, y2);
7496 processId(mapper, 2);
7497 processMTSync(mapper);
7498 processSync(mapper);
7499
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7501 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7502 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7503 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7506 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7507
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007509 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007510 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7511 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7513 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7516 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7518 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7519
7520 // Move.
7521 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7522 processPosition(mapper, x1, y1);
7523 processId(mapper, 1);
7524 processMTSync(mapper);
7525 processPosition(mapper, x2, y2);
7526 processId(mapper, 2);
7527 processMTSync(mapper);
7528 processSync(mapper);
7529
7530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7532 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7535 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7538 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7540 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7541
7542 // First finger up.
7543 x2 += 15; y2 -= 20;
7544 processPosition(mapper, x2, y2);
7545 processId(mapper, 2);
7546 processMTSync(mapper);
7547 processSync(mapper);
7548
7549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007550 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007551 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7552 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7554 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7557 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7559 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7560
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7563 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7564 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7567 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7568
7569 // Move.
7570 x2 += 20; y2 -= 25;
7571 processPosition(mapper, x2, y2);
7572 processId(mapper, 2);
7573 processMTSync(mapper);
7574 processSync(mapper);
7575
7576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7578 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7579 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7582 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7583
7584 // New finger down.
7585 int32_t x3 = 700, y3 = 300;
7586 processPosition(mapper, x2, y2);
7587 processId(mapper, 2);
7588 processMTSync(mapper);
7589 processPosition(mapper, x3, y3);
7590 processId(mapper, 3);
7591 processMTSync(mapper);
7592 processSync(mapper);
7593
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007595 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007596 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7597 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7599 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7602 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7604 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7605
7606 // Second finger up.
7607 x3 += 30; y3 -= 20;
7608 processPosition(mapper, x3, y3);
7609 processId(mapper, 3);
7610 processMTSync(mapper);
7611 processSync(mapper);
7612
7613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007614 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007615 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7616 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7618 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7621 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7623 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7624
7625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7627 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7628 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7631 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7632
7633 // Last finger up.
7634 processMTSync(mapper);
7635 processSync(mapper);
7636
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7638 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7639 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7640 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7643 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7644
7645 // Should not have sent any more keys or motions.
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7648}
7649
7650TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651 addConfigurationProperty("touch.deviceType", "touchScreen");
7652 prepareDisplay(DISPLAY_ORIENTATION_0);
7653 prepareAxes(POSITION | ID | SLOT);
7654 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007655 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007656
arthurhungdcef2dc2020-08-11 14:47:50 +08007657 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007658
7659 NotifyMotionArgs motionArgs;
7660
7661 // Two fingers down at once.
7662 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7663 processPosition(mapper, x1, y1);
7664 processId(mapper, 1);
7665 processSlot(mapper, 1);
7666 processPosition(mapper, x2, y2);
7667 processId(mapper, 2);
7668 processSync(mapper);
7669
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7671 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7672 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7676 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7677
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007679 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7681 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7683 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7686 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7688 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7689
7690 // Move.
7691 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7692 processSlot(mapper, 0);
7693 processPosition(mapper, x1, y1);
7694 processSlot(mapper, 1);
7695 processPosition(mapper, x2, y2);
7696 processSync(mapper);
7697
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7700 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7701 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7703 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7706 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7708 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7709
7710 // First finger up.
7711 x2 += 15; y2 -= 20;
7712 processSlot(mapper, 0);
7713 processId(mapper, -1);
7714 processSlot(mapper, 1);
7715 processPosition(mapper, x2, y2);
7716 processSync(mapper);
7717
7718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007719 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7723 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7726 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7728 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7729
7730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7732 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7733 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7736 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7737
7738 // Move.
7739 x2 += 20; y2 -= 25;
7740 processPosition(mapper, x2, y2);
7741 processSync(mapper);
7742
7743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7745 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7746 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7749 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7750
7751 // New finger down.
7752 int32_t x3 = 700, y3 = 300;
7753 processPosition(mapper, x2, y2);
7754 processSlot(mapper, 0);
7755 processId(mapper, 3);
7756 processPosition(mapper, x3, y3);
7757 processSync(mapper);
7758
7759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007760 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007761 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7762 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7764 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7767 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7769 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7770
7771 // Second finger up.
7772 x3 += 30; y3 -= 20;
7773 processSlot(mapper, 1);
7774 processId(mapper, -1);
7775 processSlot(mapper, 0);
7776 processPosition(mapper, x3, y3);
7777 processSync(mapper);
7778
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007780 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007781 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7782 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7784 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7787 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7789 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7790
7791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7793 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7794 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7797 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7798
7799 // Last finger up.
7800 processId(mapper, -1);
7801 processSync(mapper);
7802
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7804 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7805 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7806 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7809 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7810
7811 // Should not have sent any more keys or motions.
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7814}
7815
7816TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817 addConfigurationProperty("touch.deviceType", "touchScreen");
7818 prepareDisplay(DISPLAY_ORIENTATION_0);
7819 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007820 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007821
7822 // These calculations are based on the input device calibration documentation.
7823 int32_t rawX = 100;
7824 int32_t rawY = 200;
7825 int32_t rawTouchMajor = 7;
7826 int32_t rawTouchMinor = 6;
7827 int32_t rawToolMajor = 9;
7828 int32_t rawToolMinor = 8;
7829 int32_t rawPressure = 11;
7830 int32_t rawDistance = 0;
7831 int32_t rawOrientation = 3;
7832 int32_t id = 5;
7833
7834 float x = toDisplayX(rawX);
7835 float y = toDisplayY(rawY);
7836 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7837 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7838 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7839 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7840 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7841 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7842 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7843 float distance = float(rawDistance);
7844
7845 processPosition(mapper, rawX, rawY);
7846 processTouchMajor(mapper, rawTouchMajor);
7847 processTouchMinor(mapper, rawTouchMinor);
7848 processToolMajor(mapper, rawToolMajor);
7849 processToolMinor(mapper, rawToolMinor);
7850 processPressure(mapper, rawPressure);
7851 processOrientation(mapper, rawOrientation);
7852 processDistance(mapper, rawDistance);
7853 processId(mapper, id);
7854 processMTSync(mapper);
7855 processSync(mapper);
7856
7857 NotifyMotionArgs args;
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7859 ASSERT_EQ(0, args.pointerProperties[0].id);
7860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7861 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7862 orientation, distance));
7863}
7864
7865TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007866 addConfigurationProperty("touch.deviceType", "touchScreen");
7867 prepareDisplay(DISPLAY_ORIENTATION_0);
7868 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7869 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007870 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007871
7872 // These calculations are based on the input device calibration documentation.
7873 int32_t rawX = 100;
7874 int32_t rawY = 200;
7875 int32_t rawTouchMajor = 140;
7876 int32_t rawTouchMinor = 120;
7877 int32_t rawToolMajor = 180;
7878 int32_t rawToolMinor = 160;
7879
7880 float x = toDisplayX(rawX);
7881 float y = toDisplayY(rawY);
7882 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7883 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7884 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7885 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7886 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7887
7888 processPosition(mapper, rawX, rawY);
7889 processTouchMajor(mapper, rawTouchMajor);
7890 processTouchMinor(mapper, rawTouchMinor);
7891 processToolMajor(mapper, rawToolMajor);
7892 processToolMinor(mapper, rawToolMinor);
7893 processMTSync(mapper);
7894 processSync(mapper);
7895
7896 NotifyMotionArgs args;
7897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7899 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7900}
7901
7902TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007903 addConfigurationProperty("touch.deviceType", "touchScreen");
7904 prepareDisplay(DISPLAY_ORIENTATION_0);
7905 prepareAxes(POSITION | TOUCH | TOOL);
7906 addConfigurationProperty("touch.size.calibration", "diameter");
7907 addConfigurationProperty("touch.size.scale", "10");
7908 addConfigurationProperty("touch.size.bias", "160");
7909 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911
7912 // These calculations are based on the input device calibration documentation.
7913 // Note: We only provide a single common touch/tool value because the device is assumed
7914 // not to emit separate values for each pointer (isSummed = 1).
7915 int32_t rawX = 100;
7916 int32_t rawY = 200;
7917 int32_t rawX2 = 150;
7918 int32_t rawY2 = 250;
7919 int32_t rawTouchMajor = 5;
7920 int32_t rawToolMajor = 8;
7921
7922 float x = toDisplayX(rawX);
7923 float y = toDisplayY(rawY);
7924 float x2 = toDisplayX(rawX2);
7925 float y2 = toDisplayY(rawY2);
7926 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7927 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7928 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7929
7930 processPosition(mapper, rawX, rawY);
7931 processTouchMajor(mapper, rawTouchMajor);
7932 processToolMajor(mapper, rawToolMajor);
7933 processMTSync(mapper);
7934 processPosition(mapper, rawX2, rawY2);
7935 processTouchMajor(mapper, rawTouchMajor);
7936 processToolMajor(mapper, rawToolMajor);
7937 processMTSync(mapper);
7938 processSync(mapper);
7939
7940 NotifyMotionArgs args;
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7943
7944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007945 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946 ASSERT_EQ(size_t(2), args.pointerCount);
7947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7948 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7950 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7951}
7952
7953TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007954 addConfigurationProperty("touch.deviceType", "touchScreen");
7955 prepareDisplay(DISPLAY_ORIENTATION_0);
7956 prepareAxes(POSITION | TOUCH | TOOL);
7957 addConfigurationProperty("touch.size.calibration", "area");
7958 addConfigurationProperty("touch.size.scale", "43");
7959 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961
7962 // These calculations are based on the input device calibration documentation.
7963 int32_t rawX = 100;
7964 int32_t rawY = 200;
7965 int32_t rawTouchMajor = 5;
7966 int32_t rawToolMajor = 8;
7967
7968 float x = toDisplayX(rawX);
7969 float y = toDisplayY(rawY);
7970 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7971 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7972 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7973
7974 processPosition(mapper, rawX, rawY);
7975 processTouchMajor(mapper, rawTouchMajor);
7976 processToolMajor(mapper, rawToolMajor);
7977 processMTSync(mapper);
7978 processSync(mapper);
7979
7980 NotifyMotionArgs args;
7981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7983 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7984}
7985
7986TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007987 addConfigurationProperty("touch.deviceType", "touchScreen");
7988 prepareDisplay(DISPLAY_ORIENTATION_0);
7989 prepareAxes(POSITION | PRESSURE);
7990 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7991 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007992 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993
Michael Wrightaa449c92017-12-13 21:21:43 +00007994 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007995 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007996 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7997 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7998 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7999
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000 // These calculations are based on the input device calibration documentation.
8001 int32_t rawX = 100;
8002 int32_t rawY = 200;
8003 int32_t rawPressure = 60;
8004
8005 float x = toDisplayX(rawX);
8006 float y = toDisplayY(rawY);
8007 float pressure = float(rawPressure) * 0.01f;
8008
8009 processPosition(mapper, rawX, rawY);
8010 processPressure(mapper, rawPressure);
8011 processMTSync(mapper);
8012 processSync(mapper);
8013
8014 NotifyMotionArgs args;
8015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8017 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8018}
8019
8020TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008021 addConfigurationProperty("touch.deviceType", "touchScreen");
8022 prepareDisplay(DISPLAY_ORIENTATION_0);
8023 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025
8026 NotifyMotionArgs motionArgs;
8027 NotifyKeyArgs keyArgs;
8028
8029 processId(mapper, 1);
8030 processPosition(mapper, 100, 200);
8031 processSync(mapper);
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8034 ASSERT_EQ(0, motionArgs.buttonState);
8035
8036 // press BTN_LEFT, release BTN_LEFT
8037 processKey(mapper, BTN_LEFT, 1);
8038 processSync(mapper);
8039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8041 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8042
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8044 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8045 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8046
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047 processKey(mapper, BTN_LEFT, 0);
8048 processSync(mapper);
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008051 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008052
8053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008055 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008056
8057 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8058 processKey(mapper, BTN_RIGHT, 1);
8059 processKey(mapper, BTN_MIDDLE, 1);
8060 processSync(mapper);
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8063 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8064 motionArgs.buttonState);
8065
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8068 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8069
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8072 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8073 motionArgs.buttonState);
8074
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075 processKey(mapper, BTN_RIGHT, 0);
8076 processSync(mapper);
8077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008078 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008080
8081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008083 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008084
8085 processKey(mapper, BTN_MIDDLE, 0);
8086 processSync(mapper);
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008089 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008090
8091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008093 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094
8095 // press BTN_BACK, release BTN_BACK
8096 processKey(mapper, BTN_BACK, 1);
8097 processSync(mapper);
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8099 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8100 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008101
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8105
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8108 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008109
8110 processKey(mapper, BTN_BACK, 0);
8111 processSync(mapper);
8112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008115
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008118 ASSERT_EQ(0, motionArgs.buttonState);
8119
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8121 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8122 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8123
8124 // press BTN_SIDE, release BTN_SIDE
8125 processKey(mapper, BTN_SIDE, 1);
8126 processSync(mapper);
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8128 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8129 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008130
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008133 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8134
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8136 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8137 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138
8139 processKey(mapper, BTN_SIDE, 0);
8140 processSync(mapper);
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008144
8145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008147 ASSERT_EQ(0, motionArgs.buttonState);
8148
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8150 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8151 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8152
8153 // press BTN_FORWARD, release BTN_FORWARD
8154 processKey(mapper, BTN_FORWARD, 1);
8155 processSync(mapper);
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8157 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8158 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8163
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8165 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8166 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167
8168 processKey(mapper, BTN_FORWARD, 0);
8169 processSync(mapper);
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008171 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008172 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008173
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008176 ASSERT_EQ(0, motionArgs.buttonState);
8177
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8179 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8180 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8181
8182 // press BTN_EXTRA, release BTN_EXTRA
8183 processKey(mapper, BTN_EXTRA, 1);
8184 processSync(mapper);
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8186 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8187 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008188
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8192
8193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8195 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196
8197 processKey(mapper, BTN_EXTRA, 0);
8198 processSync(mapper);
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008200 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008202
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008205 ASSERT_EQ(0, motionArgs.buttonState);
8206
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8208 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8209 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8210
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8212
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213 // press BTN_STYLUS, release BTN_STYLUS
8214 processKey(mapper, BTN_STYLUS, 1);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8219
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8222 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223
8224 processKey(mapper, BTN_STYLUS, 0);
8225 processSync(mapper);
8226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008229
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008232 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233
8234 // press BTN_STYLUS2, release BTN_STYLUS2
8235 processKey(mapper, BTN_STYLUS2, 1);
8236 processSync(mapper);
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008239 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8240
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8243 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008244
8245 processKey(mapper, BTN_STYLUS2, 0);
8246 processSync(mapper);
8247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008250
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008253 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254
8255 // release touch
8256 processId(mapper, -1);
8257 processSync(mapper);
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8259 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8260 ASSERT_EQ(0, motionArgs.buttonState);
8261}
8262
8263TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 addConfigurationProperty("touch.deviceType", "touchScreen");
8265 prepareDisplay(DISPLAY_ORIENTATION_0);
8266 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008267 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268
8269 NotifyMotionArgs motionArgs;
8270
8271 // default tool type is finger
8272 processId(mapper, 1);
8273 processPosition(mapper, 100, 200);
8274 processSync(mapper);
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8276 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8278
8279 // eraser
8280 processKey(mapper, BTN_TOOL_RUBBER, 1);
8281 processSync(mapper);
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8285
8286 // stylus
8287 processKey(mapper, BTN_TOOL_RUBBER, 0);
8288 processKey(mapper, BTN_TOOL_PEN, 1);
8289 processSync(mapper);
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8293
8294 // brush
8295 processKey(mapper, BTN_TOOL_PEN, 0);
8296 processKey(mapper, BTN_TOOL_BRUSH, 1);
8297 processSync(mapper);
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8301
8302 // pencil
8303 processKey(mapper, BTN_TOOL_BRUSH, 0);
8304 processKey(mapper, BTN_TOOL_PENCIL, 1);
8305 processSync(mapper);
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8309
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008310 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311 processKey(mapper, BTN_TOOL_PENCIL, 0);
8312 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8313 processSync(mapper);
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8317
8318 // mouse
8319 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8320 processKey(mapper, BTN_TOOL_MOUSE, 1);
8321 processSync(mapper);
8322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8325
8326 // lens
8327 processKey(mapper, BTN_TOOL_MOUSE, 0);
8328 processKey(mapper, BTN_TOOL_LENS, 1);
8329 processSync(mapper);
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8333
8334 // double-tap
8335 processKey(mapper, BTN_TOOL_LENS, 0);
8336 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8337 processSync(mapper);
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8341
8342 // triple-tap
8343 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8344 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8345 processSync(mapper);
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8349
8350 // quad-tap
8351 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8352 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8353 processSync(mapper);
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8357
8358 // finger
8359 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8360 processKey(mapper, BTN_TOOL_FINGER, 1);
8361 processSync(mapper);
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8365
8366 // stylus trumps finger
8367 processKey(mapper, BTN_TOOL_PEN, 1);
8368 processSync(mapper);
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8372
8373 // eraser trumps stylus
8374 processKey(mapper, BTN_TOOL_RUBBER, 1);
8375 processSync(mapper);
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8379
8380 // mouse trumps eraser
8381 processKey(mapper, BTN_TOOL_MOUSE, 1);
8382 processSync(mapper);
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8386
8387 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8388 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8389 processSync(mapper);
8390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8393
8394 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8395 processToolType(mapper, MT_TOOL_PEN);
8396 processSync(mapper);
8397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8400
8401 // back to default tool type
8402 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8403 processKey(mapper, BTN_TOOL_MOUSE, 0);
8404 processKey(mapper, BTN_TOOL_RUBBER, 0);
8405 processKey(mapper, BTN_TOOL_PEN, 0);
8406 processKey(mapper, BTN_TOOL_FINGER, 0);
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_FINGER, motionArgs.pointerProperties[0].toolType);
8411}
8412
8413TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008414 addConfigurationProperty("touch.deviceType", "touchScreen");
8415 prepareDisplay(DISPLAY_ORIENTATION_0);
8416 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008417 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008418 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008419
8420 NotifyMotionArgs motionArgs;
8421
8422 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8423 processId(mapper, 1);
8424 processPosition(mapper, 100, 200);
8425 processSync(mapper);
8426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8429 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8430
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8434 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8435
8436 // move a little
8437 processPosition(mapper, 150, 250);
8438 processSync(mapper);
8439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8442 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8443
8444 // down when BTN_TOUCH is pressed, pressure defaults to 1
8445 processKey(mapper, BTN_TOUCH, 1);
8446 processSync(mapper);
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8448 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8450 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8451
8452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8455 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8456
8457 // up when BTN_TOUCH is released, hover restored
8458 processKey(mapper, BTN_TOUCH, 0);
8459 processSync(mapper);
8460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8461 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8463 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8464
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8466 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8468 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8469
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8473 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8474
8475 // exit hover when pointer goes away
8476 processId(mapper, -1);
8477 processSync(mapper);
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8481 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8482}
8483
8484TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485 addConfigurationProperty("touch.deviceType", "touchScreen");
8486 prepareDisplay(DISPLAY_ORIENTATION_0);
8487 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008488 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008489
8490 NotifyMotionArgs motionArgs;
8491
8492 // initially hovering because pressure is 0
8493 processId(mapper, 1);
8494 processPosition(mapper, 100, 200);
8495 processPressure(mapper, 0);
8496 processSync(mapper);
8497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8498 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8500 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8501
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8503 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8505 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8506
8507 // move a little
8508 processPosition(mapper, 150, 250);
8509 processSync(mapper);
8510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8511 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8513 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8514
8515 // down when pressure becomes non-zero
8516 processPressure(mapper, RAW_PRESSURE_MAX);
8517 processSync(mapper);
8518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8521 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8522
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8526 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8527
8528 // up when pressure becomes 0, hover restored
8529 processPressure(mapper, 0);
8530 processSync(mapper);
8531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8532 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8534 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8535
8536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8537 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8539 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8540
8541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8544 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8545
8546 // exit hover when pointer goes away
8547 processId(mapper, -1);
8548 processSync(mapper);
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8550 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8552 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8553}
8554
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008555/**
8556 * Set the input device port <--> display port associations, and check that the
8557 * events are routed to the display that matches the display port.
8558 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8559 */
8560TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008561 const std::string usb2 = "USB2";
8562 const uint8_t hdmi1 = 0;
8563 const uint8_t hdmi2 = 1;
8564 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008565 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008566
8567 addConfigurationProperty("touch.deviceType", "touchScreen");
8568 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008569 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008570
8571 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8572 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8573
8574 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8575 // for this input device is specified, and the matching viewport is not present,
8576 // the input device should be disabled (at the mapper level).
8577
8578 // Add viewport for display 2 on hdmi2
8579 prepareSecondaryDisplay(type, hdmi2);
8580 // Send a touch event
8581 processPosition(mapper, 100, 100);
8582 processSync(mapper);
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8584
8585 // Add viewport for display 1 on hdmi1
8586 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8587 // Send a touch event again
8588 processPosition(mapper, 100, 100);
8589 processSync(mapper);
8590
8591 NotifyMotionArgs args;
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8593 ASSERT_EQ(DISPLAY_ID, args.displayId);
8594}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008595
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008596TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8597 addConfigurationProperty("touch.deviceType", "touchScreen");
8598 prepareAxes(POSITION);
8599 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8600
8601 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8602
8603 prepareDisplay(DISPLAY_ORIENTATION_0);
8604 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8605
8606 // Send a touch event
8607 processPosition(mapper, 100, 100);
8608 processSync(mapper);
8609
8610 NotifyMotionArgs args;
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8612 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8613}
8614
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008615TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008616 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008617 std::shared_ptr<FakePointerController> fakePointerController =
8618 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008619 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008620 fakePointerController->setPosition(100, 200);
8621 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008622 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008623
Garfield Tan888a6a42020-01-09 11:39:16 -08008624 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008625 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008626
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008627 prepareDisplay(DISPLAY_ORIENTATION_0);
8628 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008629 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008630
8631 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008632 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008633
8634 NotifyMotionArgs motionArgs;
8635 processPosition(mapper, 100, 100);
8636 processSync(mapper);
8637
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8640 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8641}
8642
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008643/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008644 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8645 */
8646TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8647 addConfigurationProperty("touch.deviceType", "touchScreen");
8648 prepareAxes(POSITION);
8649 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8650
8651 prepareDisplay(DISPLAY_ORIENTATION_0);
8652 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8653 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8654 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8655 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8656
8657 NotifyMotionArgs args;
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8659 ASSERT_EQ(26, args.readTime);
8660
8661 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8662 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8663 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8664
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8666 ASSERT_EQ(33, args.readTime);
8667}
8668
8669/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008670 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8671 * events should not be delivered to the listener.
8672 */
8673TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8674 addConfigurationProperty("touch.deviceType", "touchScreen");
8675 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8676 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8677 ViewportType::INTERNAL);
8678 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8679 prepareAxes(POSITION);
8680 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8681
8682 NotifyMotionArgs motionArgs;
8683 processPosition(mapper, 100, 100);
8684 processSync(mapper);
8685
8686 mFakeListener->assertNotifyMotionWasNotCalled();
8687}
8688
Garfield Tanc734e4f2021-01-15 20:01:39 -08008689TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8690 addConfigurationProperty("touch.deviceType", "touchScreen");
8691 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8692 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8693 ViewportType::INTERNAL);
8694 std::optional<DisplayViewport> optionalDisplayViewport =
8695 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8696 ASSERT_TRUE(optionalDisplayViewport.has_value());
8697 DisplayViewport displayViewport = *optionalDisplayViewport;
8698
8699 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8700 prepareAxes(POSITION);
8701 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8702
8703 // Finger down
8704 int32_t x = 100, y = 100;
8705 processPosition(mapper, x, y);
8706 processSync(mapper);
8707
8708 NotifyMotionArgs motionArgs;
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8710 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8711
8712 // Deactivate display viewport
8713 displayViewport.isActive = false;
8714 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8715 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8716
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008717 // The ongoing touch should be canceled immediately
8718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8719 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8720
8721 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008722 x += 10, y += 10;
8723 processPosition(mapper, x, y);
8724 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008726
8727 // Reactivate display viewport
8728 displayViewport.isActive = true;
8729 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8730 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8731
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008732 // Finger move again starts new gesture
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->assertNotifyMotionWasCalled(&motionArgs));
8737 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008738}
8739
Arthur Hung7c645402019-01-25 17:45:42 +08008740TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8741 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008742 prepareAxes(POSITION | ID | SLOT);
8743 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008744 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008745
8746 // Create the second touch screen device, and enable multi fingers.
8747 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008748 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008749 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008750 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008751 std::shared_ptr<InputDevice> device2 =
8752 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008753 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008754
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008755 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8756 0 /*flat*/, 0 /*fuzz*/);
8757 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8758 0 /*flat*/, 0 /*fuzz*/);
8759 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8760 0 /*flat*/, 0 /*fuzz*/);
8761 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8762 0 /*flat*/, 0 /*fuzz*/);
8763 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8764 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8765 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008766
8767 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008768 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008769 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8770 device2->reset(ARBITRARY_TIME);
8771
8772 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008773 std::shared_ptr<FakePointerController> fakePointerController =
8774 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008775 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008776
8777 // Setup policy for associated displays and show touches.
8778 const uint8_t hdmi1 = 0;
8779 const uint8_t hdmi2 = 1;
8780 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8781 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8782 mFakePolicy->setShowTouches(true);
8783
8784 // Create displays.
8785 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008786 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008787
8788 // Default device will reconfigure above, need additional reconfiguration for another device.
8789 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008790 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8791 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008792
8793 // Two fingers down at default display.
8794 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8795 processPosition(mapper, x1, y1);
8796 processId(mapper, 1);
8797 processSlot(mapper, 1);
8798 processPosition(mapper, x2, y2);
8799 processId(mapper, 2);
8800 processSync(mapper);
8801
8802 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8803 fakePointerController->getSpots().find(DISPLAY_ID);
8804 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8805 ASSERT_EQ(size_t(2), iter->second.size());
8806
8807 // Two fingers down at second display.
8808 processPosition(mapper2, x1, y1);
8809 processId(mapper2, 1);
8810 processSlot(mapper2, 1);
8811 processPosition(mapper2, x2, y2);
8812 processId(mapper2, 2);
8813 processSync(mapper2);
8814
8815 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8816 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8817 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008818
8819 // Disable the show touches configuration and ensure the spots are cleared.
8820 mFakePolicy->setShowTouches(false);
8821 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8822 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8823
8824 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008825}
8826
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008827TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008828 prepareAxes(POSITION);
8829 addConfigurationProperty("touch.deviceType", "touchScreen");
8830 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008831 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008832
8833 NotifyMotionArgs motionArgs;
8834 // Unrotated video frame
8835 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8836 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008837 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008838 processPosition(mapper, 100, 200);
8839 processSync(mapper);
8840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8841 ASSERT_EQ(frames, motionArgs.videoFrames);
8842
8843 // Subsequent touch events should not have any videoframes
8844 // This is implemented separately in FakeEventHub,
8845 // but that should match the behaviour of TouchVideoDevice.
8846 processPosition(mapper, 200, 200);
8847 processSync(mapper);
8848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8849 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8850}
8851
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008852TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008853 prepareAxes(POSITION);
8854 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008855 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008856 // Unrotated video frame
8857 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8858 NotifyMotionArgs motionArgs;
8859
8860 // Test all 4 orientations
8861 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008862 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8863 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8864 clearViewports();
8865 prepareDisplay(orientation);
8866 std::vector<TouchVideoFrame> frames{frame};
8867 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8868 processPosition(mapper, 100, 200);
8869 processSync(mapper);
8870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8871 ASSERT_EQ(frames, motionArgs.videoFrames);
8872 }
8873}
8874
8875TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8876 prepareAxes(POSITION);
8877 addConfigurationProperty("touch.deviceType", "touchScreen");
8878 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8879 // orientation-aware are affected by display rotation.
8880 addConfigurationProperty("touch.orientationAware", "0");
8881 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8882 // Unrotated video frame
8883 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8884 NotifyMotionArgs motionArgs;
8885
8886 // Test all 4 orientations
8887 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008888 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8889 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8890 clearViewports();
8891 prepareDisplay(orientation);
8892 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008893 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008894 processPosition(mapper, 100, 200);
8895 processSync(mapper);
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008897 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8898 // compared to the display. This is so that when the window transform (which contains the
8899 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8900 // window's coordinate space.
8901 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008902 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008903
8904 // Release finger.
8905 processSync(mapper);
8906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008907 }
8908}
8909
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008910TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008911 prepareAxes(POSITION);
8912 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008913 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008914 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8915 // so mix these.
8916 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8917 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8918 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8919 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8920 NotifyMotionArgs motionArgs;
8921
8922 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008923 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008924 processPosition(mapper, 100, 200);
8925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008927 ASSERT_EQ(frames, motionArgs.videoFrames);
8928}
8929
8930TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8931 prepareAxes(POSITION);
8932 addConfigurationProperty("touch.deviceType", "touchScreen");
8933 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8934 // orientation-aware are affected by display rotation.
8935 addConfigurationProperty("touch.orientationAware", "0");
8936 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8937 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8938 // so mix these.
8939 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8940 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8941 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8942 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8943 NotifyMotionArgs motionArgs;
8944
8945 prepareDisplay(DISPLAY_ORIENTATION_90);
8946 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8947 processPosition(mapper, 100, 200);
8948 processSync(mapper);
8949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8950 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8951 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8952 // compared to the display. This is so that when the window transform (which contains the
8953 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8954 // window's coordinate space.
8955 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8956 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008957 ASSERT_EQ(frames, motionArgs.videoFrames);
8958}
8959
Arthur Hung9da14732019-09-02 16:16:58 +08008960/**
8961 * If we had defined port associations, but the viewport is not ready, the touch device would be
8962 * expected to be disabled, and it should be enabled after the viewport has found.
8963 */
8964TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008965 constexpr uint8_t hdmi2 = 1;
8966 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008967 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008968
8969 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8970
8971 addConfigurationProperty("touch.deviceType", "touchScreen");
8972 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008973 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008974
8975 ASSERT_EQ(mDevice->isEnabled(), false);
8976
8977 // Add display on hdmi2, the device should be enabled and can receive touch event.
8978 prepareSecondaryDisplay(type, hdmi2);
8979 ASSERT_EQ(mDevice->isEnabled(), true);
8980
8981 // Send a touch event.
8982 processPosition(mapper, 100, 100);
8983 processSync(mapper);
8984
8985 NotifyMotionArgs args;
8986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8987 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8988}
8989
Arthur Hung421eb1c2020-01-16 00:09:42 +08008990TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008991 addConfigurationProperty("touch.deviceType", "touchScreen");
8992 prepareDisplay(DISPLAY_ORIENTATION_0);
8993 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008994 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008995
8996 NotifyMotionArgs motionArgs;
8997
8998 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8999 // finger down
9000 processId(mapper, 1);
9001 processPosition(mapper, x1, y1);
9002 processSync(mapper);
9003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9004 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9006
9007 // finger move
9008 processId(mapper, 1);
9009 processPosition(mapper, x2, y2);
9010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9014
9015 // finger up.
9016 processId(mapper, -1);
9017 processSync(mapper);
9018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9019 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9021
9022 // new finger down
9023 processId(mapper, 1);
9024 processPosition(mapper, x3, y3);
9025 processSync(mapper);
9026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9029}
9030
9031/**
arthurhungcc7f9802020-04-30 17:55:40 +08009032 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9033 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009034 */
arthurhungcc7f9802020-04-30 17:55:40 +08009035TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009036 addConfigurationProperty("touch.deviceType", "touchScreen");
9037 prepareDisplay(DISPLAY_ORIENTATION_0);
9038 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009039 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009040
9041 NotifyMotionArgs motionArgs;
9042
9043 // default tool type is finger
9044 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009045 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009046 processPosition(mapper, x1, y1);
9047 processSync(mapper);
9048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9049 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9051
9052 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9053 processToolType(mapper, MT_TOOL_PALM);
9054 processSync(mapper);
9055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9056 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9057
9058 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009059 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009060 processPosition(mapper, x2, y2);
9061 processSync(mapper);
9062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9063
9064 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009065 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009066 processSync(mapper);
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9068
9069 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009070 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009071 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009072 processPosition(mapper, x3, y3);
9073 processSync(mapper);
9074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9075 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9077}
9078
arthurhungbf89a482020-04-17 17:37:55 +08009079/**
arthurhungcc7f9802020-04-30 17:55:40 +08009080 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9081 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009082 */
arthurhungcc7f9802020-04-30 17:55:40 +08009083TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009084 addConfigurationProperty("touch.deviceType", "touchScreen");
9085 prepareDisplay(DISPLAY_ORIENTATION_0);
9086 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9087 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9088
9089 NotifyMotionArgs motionArgs;
9090
9091 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009092 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9093 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009094 processPosition(mapper, x1, y1);
9095 processSync(mapper);
9096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9097 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9099
9100 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009101 processSlot(mapper, SECOND_SLOT);
9102 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009103 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009104 processSync(mapper);
9105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009106 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9108
9109 // If the tool type of the first finger changes to MT_TOOL_PALM,
9110 // we expect to receive ACTION_POINTER_UP with cancel flag.
9111 processSlot(mapper, FIRST_SLOT);
9112 processId(mapper, FIRST_TRACKING_ID);
9113 processToolType(mapper, MT_TOOL_PALM);
9114 processSync(mapper);
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009116 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009117 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9118
9119 // The following MOVE events of second finger should be processed.
9120 processSlot(mapper, SECOND_SLOT);
9121 processId(mapper, SECOND_TRACKING_ID);
9122 processPosition(mapper, x2 + 1, y2 + 1);
9123 processSync(mapper);
9124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9126 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9127
9128 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9129 // it. Second finger receive move.
9130 processSlot(mapper, FIRST_SLOT);
9131 processId(mapper, INVALID_TRACKING_ID);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9135 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9136
9137 // Second finger keeps moving.
9138 processSlot(mapper, SECOND_SLOT);
9139 processId(mapper, SECOND_TRACKING_ID);
9140 processPosition(mapper, x2 + 2, y2 + 2);
9141 processSync(mapper);
9142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9144 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9145
9146 // Second finger up.
9147 processId(mapper, INVALID_TRACKING_ID);
9148 processSync(mapper);
9149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9150 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9151 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9152}
9153
9154/**
9155 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9156 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9157 */
9158TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9159 addConfigurationProperty("touch.deviceType", "touchScreen");
9160 prepareDisplay(DISPLAY_ORIENTATION_0);
9161 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9162 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9163
9164 NotifyMotionArgs motionArgs;
9165
9166 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9167 // First finger down.
9168 processId(mapper, FIRST_TRACKING_ID);
9169 processPosition(mapper, x1, y1);
9170 processSync(mapper);
9171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9172 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9174
9175 // Second finger down.
9176 processSlot(mapper, SECOND_SLOT);
9177 processId(mapper, SECOND_TRACKING_ID);
9178 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009179 processSync(mapper);
9180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009181 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9183
arthurhungcc7f9802020-04-30 17:55:40 +08009184 // If the tool type of the first finger changes to MT_TOOL_PALM,
9185 // we expect to receive ACTION_POINTER_UP with cancel flag.
9186 processSlot(mapper, FIRST_SLOT);
9187 processId(mapper, FIRST_TRACKING_ID);
9188 processToolType(mapper, MT_TOOL_PALM);
9189 processSync(mapper);
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009191 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009192 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9193
9194 // Second finger keeps moving.
9195 processSlot(mapper, SECOND_SLOT);
9196 processId(mapper, SECOND_TRACKING_ID);
9197 processPosition(mapper, x2 + 1, y2 + 1);
9198 processSync(mapper);
9199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9201
9202 // second finger becomes palm, receive cancel due to only 1 finger is active.
9203 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009204 processToolType(mapper, MT_TOOL_PALM);
9205 processSync(mapper);
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9207 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9208
arthurhungcc7f9802020-04-30 17:55:40 +08009209 // third finger down.
9210 processSlot(mapper, THIRD_SLOT);
9211 processId(mapper, THIRD_TRACKING_ID);
9212 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009213 processPosition(mapper, x3, y3);
9214 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9216 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009218 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9219
9220 // third finger move
9221 processId(mapper, THIRD_TRACKING_ID);
9222 processPosition(mapper, x3 + 1, y3 + 1);
9223 processSync(mapper);
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9226
9227 // first finger up, third finger receive move.
9228 processSlot(mapper, FIRST_SLOT);
9229 processId(mapper, INVALID_TRACKING_ID);
9230 processSync(mapper);
9231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9233 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9234
9235 // second finger up, third finger receive move.
9236 processSlot(mapper, SECOND_SLOT);
9237 processId(mapper, INVALID_TRACKING_ID);
9238 processSync(mapper);
9239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9241 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9242
9243 // third finger up.
9244 processSlot(mapper, THIRD_SLOT);
9245 processId(mapper, INVALID_TRACKING_ID);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9249 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9250}
9251
9252/**
9253 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9254 * and the active finger could still be allowed to receive the events
9255 */
9256TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9257 addConfigurationProperty("touch.deviceType", "touchScreen");
9258 prepareDisplay(DISPLAY_ORIENTATION_0);
9259 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9260 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9261
9262 NotifyMotionArgs motionArgs;
9263
9264 // default tool type is finger
9265 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9266 processId(mapper, FIRST_TRACKING_ID);
9267 processPosition(mapper, x1, y1);
9268 processSync(mapper);
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9270 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9272
9273 // Second finger down.
9274 processSlot(mapper, SECOND_SLOT);
9275 processId(mapper, SECOND_TRACKING_ID);
9276 processPosition(mapper, x2, y2);
9277 processSync(mapper);
9278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009279 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9281
9282 // If the tool type of the second finger changes to MT_TOOL_PALM,
9283 // we expect to receive ACTION_POINTER_UP with cancel flag.
9284 processId(mapper, SECOND_TRACKING_ID);
9285 processToolType(mapper, MT_TOOL_PALM);
9286 processSync(mapper);
9287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009288 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009289 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9290
9291 // The following MOVE event should be processed.
9292 processSlot(mapper, FIRST_SLOT);
9293 processId(mapper, FIRST_TRACKING_ID);
9294 processPosition(mapper, x1 + 1, y1 + 1);
9295 processSync(mapper);
9296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9298 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9299
9300 // second finger up.
9301 processSlot(mapper, SECOND_SLOT);
9302 processId(mapper, INVALID_TRACKING_ID);
9303 processSync(mapper);
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9306
9307 // first finger keep moving
9308 processSlot(mapper, FIRST_SLOT);
9309 processId(mapper, FIRST_TRACKING_ID);
9310 processPosition(mapper, x1 + 2, y1 + 2);
9311 processSync(mapper);
9312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9314
9315 // first finger up.
9316 processId(mapper, INVALID_TRACKING_ID);
9317 processSync(mapper);
9318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9319 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9320 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009321}
9322
Arthur Hung9ad18942021-06-19 02:04:46 +00009323/**
9324 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9325 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9326 * cause slot be valid again.
9327 */
9328TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9329 addConfigurationProperty("touch.deviceType", "touchScreen");
9330 prepareDisplay(DISPLAY_ORIENTATION_0);
9331 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9332 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9333
9334 NotifyMotionArgs motionArgs;
9335
9336 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9337 // First finger down.
9338 processId(mapper, FIRST_TRACKING_ID);
9339 processPosition(mapper, x1, y1);
9340 processPressure(mapper, RAW_PRESSURE_MAX);
9341 processSync(mapper);
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9344 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9345
9346 // First finger move.
9347 processId(mapper, FIRST_TRACKING_ID);
9348 processPosition(mapper, x1 + 1, y1 + 1);
9349 processPressure(mapper, RAW_PRESSURE_MAX);
9350 processSync(mapper);
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9353 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9354
9355 // Second finger down.
9356 processSlot(mapper, SECOND_SLOT);
9357 processId(mapper, SECOND_TRACKING_ID);
9358 processPosition(mapper, x2, y2);
9359 processPressure(mapper, RAW_PRESSURE_MAX);
9360 processSync(mapper);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009362 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009363 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9364
9365 // second finger up with some unexpected data.
9366 processSlot(mapper, SECOND_SLOT);
9367 processId(mapper, INVALID_TRACKING_ID);
9368 processPosition(mapper, x2, y2);
9369 processSync(mapper);
9370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009371 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009372 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9373
9374 // first finger up with some unexpected data.
9375 processSlot(mapper, FIRST_SLOT);
9376 processId(mapper, INVALID_TRACKING_ID);
9377 processPosition(mapper, x2, y2);
9378 processPressure(mapper, RAW_PRESSURE_MAX);
9379 processSync(mapper);
9380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9381 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9382 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9383}
9384
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009385// --- MultiTouchInputMapperTest_ExternalDevice ---
9386
9387class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9388protected:
Chris Yea52ade12020-08-27 16:49:20 -07009389 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009390};
9391
9392/**
9393 * Expect fallback to internal viewport if device is external and external viewport is not present.
9394 */
9395TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9396 prepareAxes(POSITION);
9397 addConfigurationProperty("touch.deviceType", "touchScreen");
9398 prepareDisplay(DISPLAY_ORIENTATION_0);
9399 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9400
9401 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9402
9403 NotifyMotionArgs motionArgs;
9404
9405 // Expect the event to be sent to the internal viewport,
9406 // because an external viewport is not present.
9407 processPosition(mapper, 100, 100);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9410 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9411
9412 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009413 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009414 processPosition(mapper, 100, 100);
9415 processSync(mapper);
9416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9417 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9418}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009419
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009420TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9421 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9422 std::shared_ptr<FakePointerController> fakePointerController =
9423 std::make_shared<FakePointerController>();
9424 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9425 fakePointerController->setPosition(0, 0);
9426 fakePointerController->setButtonState(0);
9427
9428 // prepare device and capture
9429 prepareDisplay(DISPLAY_ORIENTATION_0);
9430 prepareAxes(POSITION | ID | SLOT);
9431 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9432 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9433 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009434 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009435 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9436
9437 // captured touchpad should be a touchpad source
9438 NotifyDeviceResetArgs resetArgs;
9439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9440 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9441
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009442 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009443
9444 const InputDeviceInfo::MotionRange* relRangeX =
9445 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9446 ASSERT_NE(relRangeX, nullptr);
9447 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9448 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9449 const InputDeviceInfo::MotionRange* relRangeY =
9450 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9451 ASSERT_NE(relRangeY, nullptr);
9452 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9453 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9454
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009455 // run captured pointer tests - note that this is unscaled, so input listener events should be
9456 // identical to what the hardware sends (accounting for any
9457 // calibration).
9458 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009459 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009460 processId(mapper, 1);
9461 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9462 processKey(mapper, BTN_TOUCH, 1);
9463 processSync(mapper);
9464
9465 // expect coord[0] to contain initial location of touch 0
9466 NotifyMotionArgs args;
9467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9468 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9469 ASSERT_EQ(1U, args.pointerCount);
9470 ASSERT_EQ(0, args.pointerProperties[0].id);
9471 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9472 ASSERT_NO_FATAL_FAILURE(
9473 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9474
9475 // FINGER 1 DOWN
9476 processSlot(mapper, 1);
9477 processId(mapper, 2);
9478 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9479 processSync(mapper);
9480
9481 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009483 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009484 ASSERT_EQ(2U, args.pointerCount);
9485 ASSERT_EQ(0, args.pointerProperties[0].id);
9486 ASSERT_EQ(1, args.pointerProperties[1].id);
9487 ASSERT_NO_FATAL_FAILURE(
9488 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9489 ASSERT_NO_FATAL_FAILURE(
9490 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9491
9492 // FINGER 1 MOVE
9493 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9494 processSync(mapper);
9495
9496 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9497 // from move
9498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9500 ASSERT_NO_FATAL_FAILURE(
9501 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9502 ASSERT_NO_FATAL_FAILURE(
9503 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9504
9505 // FINGER 0 MOVE
9506 processSlot(mapper, 0);
9507 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9508 processSync(mapper);
9509
9510 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9513 ASSERT_NO_FATAL_FAILURE(
9514 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9515 ASSERT_NO_FATAL_FAILURE(
9516 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9517
9518 // BUTTON DOWN
9519 processKey(mapper, BTN_LEFT, 1);
9520 processSync(mapper);
9521
9522 // touchinputmapper design sends a move before button press
9523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9527
9528 // BUTTON UP
9529 processKey(mapper, BTN_LEFT, 0);
9530 processSync(mapper);
9531
9532 // touchinputmapper design sends a move after button release
9533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9534 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9537
9538 // FINGER 0 UP
9539 processId(mapper, -1);
9540 processSync(mapper);
9541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9542 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9543
9544 // FINGER 1 MOVE
9545 processSlot(mapper, 1);
9546 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9547 processSync(mapper);
9548
9549 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9552 ASSERT_EQ(1U, args.pointerCount);
9553 ASSERT_EQ(1, args.pointerProperties[0].id);
9554 ASSERT_NO_FATAL_FAILURE(
9555 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9556
9557 // FINGER 1 UP
9558 processId(mapper, -1);
9559 processKey(mapper, BTN_TOUCH, 0);
9560 processSync(mapper);
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9562 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9563
9564 // non captured touchpad should be a mouse source
9565 mFakePolicy->setPointerCapture(false);
9566 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9568 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9569}
9570
9571TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9572 std::shared_ptr<FakePointerController> fakePointerController =
9573 std::make_shared<FakePointerController>();
9574 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9575 fakePointerController->setPosition(0, 0);
9576 fakePointerController->setButtonState(0);
9577
9578 // prepare device and capture
9579 prepareDisplay(DISPLAY_ORIENTATION_0);
9580 prepareAxes(POSITION | ID | SLOT);
9581 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9582 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009583 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009584 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9585 // run uncaptured pointer tests - pushes out generic events
9586 // FINGER 0 DOWN
9587 processId(mapper, 3);
9588 processPosition(mapper, 100, 100);
9589 processKey(mapper, BTN_TOUCH, 1);
9590 processSync(mapper);
9591
9592 // start at (100,100), cursor should be at (0,0) * scale
9593 NotifyMotionArgs args;
9594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9596 ASSERT_NO_FATAL_FAILURE(
9597 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9598
9599 // FINGER 0 MOVE
9600 processPosition(mapper, 200, 200);
9601 processSync(mapper);
9602
9603 // compute scaling to help with touch position checking
9604 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9605 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9606 float scale =
9607 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9608
9609 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9613 0, 0, 0, 0, 0, 0, 0));
9614}
9615
9616TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9617 std::shared_ptr<FakePointerController> fakePointerController =
9618 std::make_shared<FakePointerController>();
9619
9620 prepareDisplay(DISPLAY_ORIENTATION_0);
9621 prepareAxes(POSITION | ID | SLOT);
9622 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009623 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009624 mFakePolicy->setPointerCapture(false);
9625 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9626
9627 // uncaptured touchpad should be a pointer device
9628 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9629
9630 // captured touchpad should be a touchpad device
9631 mFakePolicy->setPointerCapture(true);
9632 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9633 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9634}
9635
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009636// --- JoystickInputMapperTest ---
9637
9638class JoystickInputMapperTest : public InputMapperTest {
9639protected:
9640 static const int32_t RAW_X_MIN;
9641 static const int32_t RAW_X_MAX;
9642 static const int32_t RAW_Y_MIN;
9643 static const int32_t RAW_Y_MAX;
9644
9645 void SetUp() override {
9646 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9647 }
9648 void prepareAxes() {
9649 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9650 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9651 }
9652
9653 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9655 }
9656
9657 void processSync(JoystickInputMapper& mapper) {
9658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9659 }
9660
9661 void prepareVirtualDisplay(int32_t orientation) {
9662 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9663 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9664 NO_PORT, ViewportType::VIRTUAL);
9665 }
9666};
9667
9668const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9669const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9670const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9671const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9672
9673TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9674 prepareAxes();
9675 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9676
9677 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9678
9679 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9680
9681 // Send an axis event
9682 processAxis(mapper, ABS_X, 100);
9683 processSync(mapper);
9684
9685 NotifyMotionArgs args;
9686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9687 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9688
9689 // Send another axis event
9690 processAxis(mapper, ABS_Y, 100);
9691 processSync(mapper);
9692
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9694 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9695}
9696
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009697// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009698
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009699class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009700protected:
9701 static const char* DEVICE_NAME;
9702 static const char* DEVICE_LOCATION;
9703 static const int32_t DEVICE_ID;
9704 static const int32_t DEVICE_GENERATION;
9705 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009706 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009707 static const int32_t EVENTHUB_ID;
9708
9709 std::shared_ptr<FakeEventHub> mFakeEventHub;
9710 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009711 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009712 std::unique_ptr<InstrumentedInputReader> mReader;
9713 std::shared_ptr<InputDevice> mDevice;
9714
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009715 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009716 mFakeEventHub = std::make_unique<FakeEventHub>();
9717 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009718 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009719 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009720 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009721 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9722 }
9723
9724 void SetUp() override { SetUp(DEVICE_CLASSES); }
9725
9726 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009727 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009728 mFakePolicy.clear();
9729 }
9730
9731 void configureDevice(uint32_t changes) {
9732 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9733 mReader->requestRefreshConfiguration(changes);
9734 mReader->loopOnce();
9735 }
9736 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9737 }
9738
9739 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9740 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009741 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009742 InputDeviceIdentifier identifier;
9743 identifier.name = name;
9744 identifier.location = location;
9745 std::shared_ptr<InputDevice> device =
9746 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9747 identifier);
9748 mReader->pushNextDevice(device);
9749 mFakeEventHub->addDevice(eventHubId, name, classes);
9750 mReader->loopOnce();
9751 return device;
9752 }
9753
9754 template <class T, typename... Args>
9755 T& addControllerAndConfigure(Args... args) {
9756 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9757
9758 return controller;
9759 }
9760};
9761
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009762const char* PeripheralControllerTest::DEVICE_NAME = "device";
9763const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9764const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9765const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9766const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009767const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9768 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009769const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009770
9771// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009772class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009773protected:
9774 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009775 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009776 }
9777};
9778
9779TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009780 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009781
9782 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9783 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9784}
9785
9786TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009787 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009788
9789 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9790 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9791}
9792
9793// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009794class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009795protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009796 void SetUp() override {
9797 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9798 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009799};
9800
Chris Ye85758332021-05-16 23:05:17 -07009801TEST_F(LightControllerTest, MonoLight) {
9802 RawLightInfo infoMono = {.id = 1,
9803 .name = "Mono",
9804 .maxBrightness = 255,
9805 .flags = InputLightClass::BRIGHTNESS,
9806 .path = ""};
9807 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009808
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009809 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009810 InputDeviceInfo info;
9811 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009812 std::vector<InputDeviceLightInfo> lights = info.getLights();
9813 ASSERT_EQ(1U, lights.size());
9814 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009815
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009816 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9817 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009818}
9819
9820TEST_F(LightControllerTest, RGBLight) {
9821 RawLightInfo infoRed = {.id = 1,
9822 .name = "red",
9823 .maxBrightness = 255,
9824 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9825 .path = ""};
9826 RawLightInfo infoGreen = {.id = 2,
9827 .name = "green",
9828 .maxBrightness = 255,
9829 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9830 .path = ""};
9831 RawLightInfo infoBlue = {.id = 3,
9832 .name = "blue",
9833 .maxBrightness = 255,
9834 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9835 .path = ""};
9836 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9837 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9838 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9839
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009840 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009841 InputDeviceInfo info;
9842 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009843 std::vector<InputDeviceLightInfo> lights = info.getLights();
9844 ASSERT_EQ(1U, lights.size());
9845 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009846
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009847 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9848 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009849}
9850
9851TEST_F(LightControllerTest, MultiColorRGBLight) {
9852 RawLightInfo infoColor = {.id = 1,
9853 .name = "red",
9854 .maxBrightness = 255,
9855 .flags = InputLightClass::BRIGHTNESS |
9856 InputLightClass::MULTI_INTENSITY |
9857 InputLightClass::MULTI_INDEX,
9858 .path = ""};
9859
9860 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9861
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009862 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009863 InputDeviceInfo info;
9864 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009865 std::vector<InputDeviceLightInfo> lights = info.getLights();
9866 ASSERT_EQ(1U, lights.size());
9867 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009868
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009869 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9870 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009871}
9872
9873TEST_F(LightControllerTest, PlayerIdLight) {
9874 RawLightInfo info1 = {.id = 1,
9875 .name = "player1",
9876 .maxBrightness = 255,
9877 .flags = InputLightClass::BRIGHTNESS,
9878 .path = ""};
9879 RawLightInfo info2 = {.id = 2,
9880 .name = "player2",
9881 .maxBrightness = 255,
9882 .flags = InputLightClass::BRIGHTNESS,
9883 .path = ""};
9884 RawLightInfo info3 = {.id = 3,
9885 .name = "player3",
9886 .maxBrightness = 255,
9887 .flags = InputLightClass::BRIGHTNESS,
9888 .path = ""};
9889 RawLightInfo info4 = {.id = 4,
9890 .name = "player4",
9891 .maxBrightness = 255,
9892 .flags = InputLightClass::BRIGHTNESS,
9893 .path = ""};
9894 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9895 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9896 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9897 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9898
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009899 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009900 InputDeviceInfo info;
9901 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009902 std::vector<InputDeviceLightInfo> lights = info.getLights();
9903 ASSERT_EQ(1U, lights.size());
9904 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009905
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009906 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9907 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9908 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009909}
9910
Michael Wrightd02c5b62014-02-10 15:10:22 -08009911} // namespace android