blob: e1befedcff00f9ba90d7c22b77d072a7b636a096 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000059static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080060static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000061static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_WIDTH = 480;
63static constexpr int32_t DISPLAY_HEIGHT = 800;
64static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
65static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
66static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070067static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070068static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080069
arthurhungcc7f9802020-04-30 17:55:40 +080070static constexpr int32_t FIRST_SLOT = 0;
71static constexpr int32_t SECOND_SLOT = 1;
72static constexpr int32_t THIRD_SLOT = 2;
73static constexpr int32_t INVALID_TRACKING_ID = -1;
74static constexpr int32_t FIRST_TRACKING_ID = 0;
75static constexpr int32_t SECOND_TRACKING_ID = 1;
76static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080077static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080078static constexpr int32_t BATTERY_STATUS = 4;
79static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080080static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
81static constexpr int32_t LIGHT_COLOR = 0x7F448866;
82static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080083
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080084static constexpr int32_t ACTION_POINTER_0_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_DOWN =
89 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92
Michael Wrightd02c5b62014-02-10 15:10:22 -080093// Error tolerance for floating point assertions.
94static const float EPSILON = 0.001f;
95
Prabir Pradhanc04d04d2022-09-08 22:03:30 +000096using ::testing::AllOf;
97
98MATCHER_P(WithAction, action, "InputEvent with specified action") {
99 return arg.action == action;
100}
101
102MATCHER_P(WithSource, source, "InputEvent with specified source") {
103 return arg.source == source;
104}
105
106MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
107 return arg.displayId == displayId;
108}
109
110MATCHER_P2(WithCoords, x, y, "MotionEvent with specified action") {
111 return arg.pointerCoords[0].getX() == x && arg.pointerCoords[0].getY();
112}
113
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114template<typename T>
115static inline T min(T a, T b) {
116 return a < b ? a : b;
117}
118
119static inline float avg(float x, float y) {
120 return (x + y) / 2;
121}
122
Chris Ye3fdbfef2021-01-06 18:45:18 -0800123// Mapping for light color name and the light color
124const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
125 {"green", LightColor::GREEN},
126 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700128static int32_t getInverseRotation(int32_t orientation) {
129 switch (orientation) {
130 case DISPLAY_ORIENTATION_90:
131 return DISPLAY_ORIENTATION_270;
132 case DISPLAY_ORIENTATION_270:
133 return DISPLAY_ORIENTATION_90;
134 default:
135 return orientation;
136 }
137}
138
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800139static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
140 InputDeviceInfo info;
141 mapper.populateDeviceInfo(&info);
142
143 const InputDeviceInfo::MotionRange* motionRange =
144 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
145 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
146}
147
148static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
149 InputDeviceInfo info;
150 mapper.populateDeviceInfo(&info);
151
152 const InputDeviceInfo::MotionRange* motionRange =
153 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
154 ASSERT_EQ(nullptr, motionRange);
155}
156
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157// --- FakePointerController ---
158
159class FakePointerController : public PointerControllerInterface {
160 bool mHaveBounds;
161 float mMinX, mMinY, mMaxX, mMaxY;
162 float mX, mY;
163 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800164 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166public:
167 FakePointerController() :
168 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800169 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 }
171
Michael Wright17db18e2020-06-26 20:51:44 +0100172 virtual ~FakePointerController() {}
173
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174 void setBounds(float minX, float minY, float maxX, float maxY) {
175 mHaveBounds = true;
176 mMinX = minX;
177 mMinY = minY;
178 mMaxX = maxX;
179 mMaxY = maxY;
180 }
181
Chris Yea52ade12020-08-27 16:49:20 -0700182 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183 mX = x;
184 mY = y;
185 }
186
Chris Yea52ade12020-08-27 16:49:20 -0700187 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
Chris Yea52ade12020-08-27 16:49:20 -0700191 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 *outX = mX;
193 *outY = mY;
194 }
195
Chris Yea52ade12020-08-27 16:49:20 -0700196 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800197
Chris Yea52ade12020-08-27 16:49:20 -0700198 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800199 mDisplayId = viewport.displayId;
200 }
201
Arthur Hung7c645402019-01-25 17:45:42 +0800202 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
203 return mSpotsByDisplay;
204 }
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206private:
Chris Yea52ade12020-08-27 16:49:20 -0700207 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 *outMinX = mMinX;
209 *outMinY = mMinY;
210 *outMaxX = mMaxX;
211 *outMaxY = mMaxY;
212 return mHaveBounds;
213 }
214
Chris Yea52ade12020-08-27 16:49:20 -0700215 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 mX += deltaX;
217 if (mX < mMinX) mX = mMinX;
218 if (mX > mMaxX) mX = mMaxX;
219 mY += deltaY;
220 if (mY < mMinY) mY = mMinY;
221 if (mY > mMaxY) mY = mMaxY;
222 }
223
Chris Yea52ade12020-08-27 16:49:20 -0700224 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225
Chris Yea52ade12020-08-27 16:49:20 -0700226 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
Chris Yea52ade12020-08-27 16:49:20 -0700228 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229
Chris Yea52ade12020-08-27 16:49:20 -0700230 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
231 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800232 std::vector<int32_t> newSpots;
233 // Add spots for fingers that are down.
234 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
235 uint32_t id = idBits.clearFirstMarkedBit();
236 newSpots.push_back(id);
237 }
238
239 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240 }
241
Chris Yea52ade12020-08-27 16:49:20 -0700242 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800243
244 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245};
246
247
248// --- FakeInputReaderPolicy ---
249
250class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700251 std::mutex mLock;
252 std::condition_variable mDevicesChangedCondition;
253
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000255 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700256 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
257 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100258 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700259 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260
261protected:
Chris Yea52ade12020-08-27 16:49:20 -0700262 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263
264public:
265 FakeInputReaderPolicy() {
266 }
267
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700268 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800269 waitForInputDevices([](bool devicesChanged) {
270 if (!devicesChanged) {
271 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
272 }
273 });
274 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700275
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800276 void assertInputDevicesNotChanged() {
277 waitForInputDevices([](bool devicesChanged) {
278 if (devicesChanged) {
279 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
280 }
281 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700282 }
283
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700284 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100285 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100286 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700287 }
288
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700289 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
290 return mConfig.getDisplayViewportByUniqueId(uniqueId);
291 }
292 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
293 return mConfig.getDisplayViewportByType(type);
294 }
295
296 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
297 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700298 }
299
Prabir Pradhan5632d622021-09-06 07:57:20 -0700300 void addDisplayViewport(DisplayViewport viewport) {
301 mViewports.push_back(std::move(viewport));
302 mConfig.setDisplayViewports(mViewports);
303 }
304
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700305 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000306 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700307 std::optional<uint8_t> physicalPort, ViewportType type) {
308 const bool isRotated =
309 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
310 DisplayViewport v;
311 v.displayId = displayId;
312 v.orientation = orientation;
313 v.logicalLeft = 0;
314 v.logicalTop = 0;
315 v.logicalRight = isRotated ? height : width;
316 v.logicalBottom = isRotated ? width : height;
317 v.physicalLeft = 0;
318 v.physicalTop = 0;
319 v.physicalRight = isRotated ? height : width;
320 v.physicalBottom = isRotated ? width : height;
321 v.deviceWidth = isRotated ? height : width;
322 v.deviceHeight = isRotated ? width : height;
323 v.isActive = isActive;
324 v.uniqueId = uniqueId;
325 v.physicalPort = physicalPort;
326 v.type = type;
327
328 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 }
330
Arthur Hung6cd19a42019-08-30 19:04:12 +0800331 bool updateViewport(const DisplayViewport& viewport) {
332 size_t count = mViewports.size();
333 for (size_t i = 0; i < count; i++) {
334 const DisplayViewport& currentViewport = mViewports[i];
335 if (currentViewport.displayId == viewport.displayId) {
336 mViewports[i] = viewport;
337 mConfig.setDisplayViewports(mViewports);
338 return true;
339 }
340 }
341 // no viewport found.
342 return false;
343 }
344
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100345 void addExcludedDeviceName(const std::string& deviceName) {
346 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700349 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
350 mConfig.portAssociations.insert({inputPort, displayPort});
351 }
352
Christine Franks1ba71cc2021-04-07 14:37:42 -0700353 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
354 const std::string& displayUniqueId) {
355 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
356 }
357
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000358 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700359
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000360 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700361
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000362 void setPointerController(std::shared_ptr<FakePointerController> controller) {
363 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 }
365
366 const InputReaderConfiguration* getReaderConfiguration() const {
367 return &mConfig;
368 }
369
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800370 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 return mInputDevices;
372 }
373
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100374 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700375 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700376 return transform;
377 }
378
379 void setTouchAffineTransformation(const TouchAffineTransformation t) {
380 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800381 }
382
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000383 PointerCaptureRequest setPointerCapture(bool enabled) {
384 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
385 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800386 }
387
Arthur Hung7c645402019-01-25 17:45:42 +0800388 void setShowTouches(bool enabled) {
389 mConfig.showTouches = enabled;
390 }
391
Garfield Tan888a6a42020-01-09 11:39:16 -0800392 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
393 mConfig.defaultPointerDisplayId = pointerDisplayId;
394 }
395
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800396 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
397
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000398 void setVelocityControlParams(const VelocityControlParameters& params) {
399 mConfig.pointerVelocityControlParameters = params;
400 mConfig.wheelVelocityControlParameters = params;
401 }
402
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000404 uint32_t mNextPointerCaptureSequenceNumber = 0;
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 *outConfig = mConfig;
408 }
409
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000410 std::shared_ptr<PointerControllerInterface> obtainPointerController(
411 int32_t /*deviceId*/) override {
412 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413 }
414
Chris Yea52ade12020-08-27 16:49:20 -0700415 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700416 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700418 mInputDevicesChanged = true;
419 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 }
421
Chris Yea52ade12020-08-27 16:49:20 -0700422 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
423 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700424 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425 }
426
Chris Yea52ade12020-08-27 16:49:20 -0700427 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800428
429 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
430 std::unique_lock<std::mutex> lock(mLock);
431 base::ScopedLockAssertion assumeLocked(mLock);
432
433 const bool devicesChanged =
434 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
435 return mInputDevicesChanged;
436 });
437 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
438 mInputDevicesChanged = false;
439 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440};
441
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442// --- FakeEventHub ---
443
444class FakeEventHub : public EventHubInterface {
445 struct KeyInfo {
446 int32_t keyCode;
447 uint32_t flags;
448 };
449
Chris Yef59a2f42020-10-16 12:55:26 -0700450 struct SensorInfo {
451 InputDeviceSensorType sensorType;
452 int32_t sensorDataIndex;
453 };
454
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 struct Device {
456 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700457 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 PropertyMap configuration;
459 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
460 KeyedVector<int, bool> relativeAxes;
461 KeyedVector<int32_t, int32_t> keyCodeStates;
462 KeyedVector<int32_t, int32_t> scanCodeStates;
463 KeyedVector<int32_t, int32_t> switchStates;
464 KeyedVector<int32_t, int32_t> absoluteAxisValue;
465 KeyedVector<int32_t, KeyInfo> keysByScanCode;
466 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
467 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100468 // fake mapping which would normally come from keyCharacterMap
469 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700470 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
471 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800472 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700473 bool enabled;
474
475 status_t enable() {
476 enabled = true;
477 return OK;
478 }
479
480 status_t disable() {
481 enabled = false;
482 return OK;
483 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700485 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486 };
487
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700488 std::mutex mLock;
489 std::condition_variable mEventsCondition;
490
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100492 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000493 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600494 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000495 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800496 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
497 // Simulates a device light brightness, from light id to light brightness.
498 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
499 // Simulates a device light intensities, from light id to light intensities map.
500 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
501 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700503public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 virtual ~FakeEventHub() {
505 for (size_t i = 0; i < mDevices.size(); i++) {
506 delete mDevices.valueAt(i);
507 }
508 }
509
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 FakeEventHub() { }
511
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700512 void addDevice(int32_t deviceId, const std::string& name,
513 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 Device* device = new Device(classes);
515 device->identifier.name = name;
516 mDevices.add(deviceId, device);
517
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000518 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 }
520
521 void removeDevice(int32_t deviceId) {
522 delete mDevices.valueFor(deviceId);
523 mDevices.removeItem(deviceId);
524
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000525 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526 }
527
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700528 bool isDeviceEnabled(int32_t deviceId) {
529 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700530 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700531 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
532 return false;
533 }
534 return device->enabled;
535 }
536
537 status_t enableDevice(int32_t deviceId) {
538 status_t result;
539 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700540 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700541 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
542 return BAD_VALUE;
543 }
544 if (device->enabled) {
545 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
546 return OK;
547 }
548 result = device->enable();
549 return result;
550 }
551
552 status_t disableDevice(int32_t deviceId) {
553 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700554 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700555 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
556 return BAD_VALUE;
557 }
558 if (!device->enabled) {
559 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
560 return OK;
561 }
562 return device->disable();
563 }
564
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000566 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 }
568
569 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
570 Device* device = getDevice(deviceId);
571 device->configuration.addProperty(key, value);
572 }
573
574 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
575 Device* device = getDevice(deviceId);
576 device->configuration.addAll(configuration);
577 }
578
579 void addAbsoluteAxis(int32_t deviceId, int axis,
580 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
581 Device* device = getDevice(deviceId);
582
583 RawAbsoluteAxisInfo info;
584 info.valid = true;
585 info.minValue = minValue;
586 info.maxValue = maxValue;
587 info.flat = flat;
588 info.fuzz = fuzz;
589 info.resolution = resolution;
590 device->absoluteAxes.add(axis, info);
591 }
592
593 void addRelativeAxis(int32_t deviceId, int32_t axis) {
594 Device* device = getDevice(deviceId);
595 device->relativeAxes.add(axis, true);
596 }
597
598 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
599 Device* device = getDevice(deviceId);
600 device->keyCodeStates.replaceValueFor(keyCode, state);
601 }
602
603 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
604 Device* device = getDevice(deviceId);
605 device->scanCodeStates.replaceValueFor(scanCode, state);
606 }
607
608 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
609 Device* device = getDevice(deviceId);
610 device->switchStates.replaceValueFor(switchCode, state);
611 }
612
613 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
614 Device* device = getDevice(deviceId);
615 device->absoluteAxisValue.replaceValueFor(axis, value);
616 }
617
618 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
619 int32_t keyCode, uint32_t flags) {
620 Device* device = getDevice(deviceId);
621 KeyInfo info;
622 info.keyCode = keyCode;
623 info.flags = flags;
624 if (scanCode) {
625 device->keysByScanCode.add(scanCode, info);
626 }
627 if (usageCode) {
628 device->keysByUsageCode.add(usageCode, info);
629 }
630 }
631
Philip Junker4af3b3d2021-12-14 10:36:55 +0100632 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
633 Device* device = getDevice(deviceId);
634 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
635 }
636
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 void addLed(int32_t deviceId, int32_t led, bool initialState) {
638 Device* device = getDevice(deviceId);
639 device->leds.add(led, initialState);
640 }
641
Chris Yef59a2f42020-10-16 12:55:26 -0700642 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
643 int32_t sensorDataIndex) {
644 Device* device = getDevice(deviceId);
645 SensorInfo info;
646 info.sensorType = sensorType;
647 info.sensorDataIndex = sensorDataIndex;
648 device->sensorsByAbsCode.emplace(absCode, info);
649 }
650
651 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
652 Device* device = getDevice(deviceId);
653 typename BitArray<MSC_MAX>::Buffer buffer;
654 buffer[mscEvent / 32] = 1 << mscEvent % 32;
655 device->mscBitmask.loadFromBuffer(buffer);
656 }
657
Chris Ye3fdbfef2021-01-06 18:45:18 -0800658 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
659 mRawLightInfos.emplace(rawId, std::move(info));
660 }
661
662 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
663 mLightBrightness.emplace(rawId, brightness);
664 }
665
666 void fakeLightIntensities(int32_t rawId,
667 const std::unordered_map<LightColor, int32_t> intensities) {
668 mLightIntensities.emplace(rawId, std::move(intensities));
669 }
670
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 bool getLedState(int32_t deviceId, int32_t led) {
672 Device* device = getDevice(deviceId);
673 return device->leds.valueFor(led);
674 }
675
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100676 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 return mExcludedDevices;
678 }
679
680 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
681 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800682 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 }
684
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000685 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
686 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700687 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 RawEvent event;
689 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000690 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 event.deviceId = deviceId;
692 event.type = type;
693 event.code = code;
694 event.value = value;
695 mEvents.push_back(event);
696
697 if (type == EV_ABS) {
698 setAbsoluteAxisValue(deviceId, code, value);
699 }
700 }
701
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600702 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
703 std::vector<TouchVideoFrame>> videoFrames) {
704 mVideoFrames = std::move(videoFrames);
705 }
706
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700708 std::unique_lock<std::mutex> lock(mLock);
709 base::ScopedLockAssertion assumeLocked(mLock);
710 const bool queueIsEmpty =
711 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
712 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
713 if (!queueIsEmpty) {
714 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
715 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 }
717
718private:
719 Device* getDevice(int32_t deviceId) const {
720 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100721 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 }
723
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700724 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700726 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 }
728
Chris Yea52ade12020-08-27 16:49:20 -0700729 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 Device* device = getDevice(deviceId);
731 return device ? device->identifier : InputDeviceIdentifier();
732 }
733
Chris Yea52ade12020-08-27 16:49:20 -0700734 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735
Chris Yea52ade12020-08-27 16:49:20 -0700736 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 Device* device = getDevice(deviceId);
738 if (device) {
739 *outConfiguration = device->configuration;
740 }
741 }
742
Chris Yea52ade12020-08-27 16:49:20 -0700743 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
744 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800746 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 ssize_t index = device->absoluteAxes.indexOfKey(axis);
748 if (index >= 0) {
749 *outAxisInfo = device->absoluteAxes.valueAt(index);
750 return OK;
751 }
752 }
753 outAxisInfo->clear();
754 return -1;
755 }
756
Chris Yea52ade12020-08-27 16:49:20 -0700757 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 Device* device = getDevice(deviceId);
759 if (device) {
760 return device->relativeAxes.indexOfKey(axis) >= 0;
761 }
762 return false;
763 }
764
Chris Yea52ade12020-08-27 16:49:20 -0700765 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766
Chris Yef59a2f42020-10-16 12:55:26 -0700767 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
768 Device* device = getDevice(deviceId);
769 if (device) {
770 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
771 }
772 return false;
773 }
774
Chris Yea52ade12020-08-27 16:49:20 -0700775 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
776 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 Device* device = getDevice(deviceId);
778 if (device) {
779 const KeyInfo* key = getKey(device, scanCode, usageCode);
780 if (key) {
781 if (outKeycode) {
782 *outKeycode = key->keyCode;
783 }
784 if (outFlags) {
785 *outFlags = key->flags;
786 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700787 if (outMetaState) {
788 *outMetaState = metaState;
789 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 return OK;
791 }
792 }
793 return NAME_NOT_FOUND;
794 }
795
796 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
797 if (usageCode) {
798 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
799 if (index >= 0) {
800 return &device->keysByUsageCode.valueAt(index);
801 }
802 }
803 if (scanCode) {
804 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
805 if (index >= 0) {
806 return &device->keysByScanCode.valueAt(index);
807 }
808 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700809 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
Chris Yea52ade12020-08-27 16:49:20 -0700812 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813
Chris Yef59a2f42020-10-16 12:55:26 -0700814 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
815 int32_t absCode) {
816 Device* device = getDevice(deviceId);
817 if (!device) {
818 return Errorf("Sensor device not found.");
819 }
820 auto it = device->sensorsByAbsCode.find(absCode);
821 if (it == device->sensorsByAbsCode.end()) {
822 return Errorf("Sensor map not found.");
823 }
824 const SensorInfo& info = it->second;
825 return std::make_pair(info.sensorType, info.sensorDataIndex);
826 }
827
Chris Yea52ade12020-08-27 16:49:20 -0700828 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 mExcludedDevices = devices;
830 }
831
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000832 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
833 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000835 const size_t filledSize = std::min(mEvents.size(), bufferSize);
836 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
837
838 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700839 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000840 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800841 }
842
Chris Yea52ade12020-08-27 16:49:20 -0700843 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600844 auto it = mVideoFrames.find(deviceId);
845 if (it != mVideoFrames.end()) {
846 std::vector<TouchVideoFrame> frames = std::move(it->second);
847 mVideoFrames.erase(deviceId);
848 return frames;
849 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800850 return {};
851 }
852
Chris Yea52ade12020-08-27 16:49:20 -0700853 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 Device* device = getDevice(deviceId);
855 if (device) {
856 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
857 if (index >= 0) {
858 return device->scanCodeStates.valueAt(index);
859 }
860 }
861 return AKEY_STATE_UNKNOWN;
862 }
863
Chris Yea52ade12020-08-27 16:49:20 -0700864 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865 Device* device = getDevice(deviceId);
866 if (device) {
867 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
868 if (index >= 0) {
869 return device->keyCodeStates.valueAt(index);
870 }
871 }
872 return AKEY_STATE_UNKNOWN;
873 }
874
Chris Yea52ade12020-08-27 16:49:20 -0700875 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 Device* device = getDevice(deviceId);
877 if (device) {
878 ssize_t index = device->switchStates.indexOfKey(sw);
879 if (index >= 0) {
880 return device->switchStates.valueAt(index);
881 }
882 }
883 return AKEY_STATE_UNKNOWN;
884 }
885
Chris Yea52ade12020-08-27 16:49:20 -0700886 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
887 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800888 Device* device = getDevice(deviceId);
889 if (device) {
890 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
891 if (index >= 0) {
892 *outValue = device->absoluteAxisValue.valueAt(index);
893 return OK;
894 }
895 }
896 *outValue = 0;
897 return -1;
898 }
899
Philip Junker4af3b3d2021-12-14 10:36:55 +0100900 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
901 Device* device = getDevice(deviceId);
902 if (!device) {
903 return AKEYCODE_UNKNOWN;
904 }
905 auto it = device->keyCodeMapping.find(locationKeyCode);
906 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
907 }
908
Chris Yea52ade12020-08-27 16:49:20 -0700909 // Return true if the device has non-empty key layout.
910 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
911 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 bool result = false;
913 Device* device = getDevice(deviceId);
914 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700915 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 for (size_t i = 0; i < numCodes; i++) {
917 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
918 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
919 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920 }
921 }
922 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
923 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
924 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 }
926 }
927 }
928 }
929 return result;
930 }
931
Chris Yea52ade12020-08-27 16:49:20 -0700932 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 Device* device = getDevice(deviceId);
934 if (device) {
935 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
936 return index >= 0;
937 }
938 return false;
939 }
940
Arthur Hungcb40a002021-08-03 14:31:01 +0000941 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
942 Device* device = getDevice(deviceId);
943 if (!device) {
944 return false;
945 }
946 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
947 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
948 return true;
949 }
950 }
951 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
952 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
953 return true;
954 }
955 }
956 return false;
957 }
958
Chris Yea52ade12020-08-27 16:49:20 -0700959 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 Device* device = getDevice(deviceId);
961 return device && device->leds.indexOfKey(led) >= 0;
962 }
963
Chris Yea52ade12020-08-27 16:49:20 -0700964 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 Device* device = getDevice(deviceId);
966 if (device) {
967 ssize_t index = device->leds.indexOfKey(led);
968 if (index >= 0) {
969 device->leds.replaceValueAt(led, on);
970 } else {
971 ADD_FAILURE()
972 << "Attempted to set the state of an LED that the EventHub declared "
973 "was not present. led=" << led;
974 }
975 }
976 }
977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void getVirtualKeyDefinitions(
979 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 outVirtualKeys.clear();
981
982 Device* device = getDevice(deviceId);
983 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800984 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 }
986 }
987
Chris Yea52ade12020-08-27 16:49:20 -0700988 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700989 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 return false;
994 }
995
Chris Yea52ade12020-08-27 16:49:20 -0700996 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997
Chris Yea52ade12020-08-27 16:49:20 -0700998 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999
Chris Ye87143712020-11-10 05:05:58 +00001000 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
1001
Chris Yee2b1e5c2021-03-10 22:45:12 -08001002 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1003 return BATTERY_CAPACITY;
1004 }
Kim Low03ea0352020-11-06 12:45:07 -08001005
Chris Yee2b1e5c2021-03-10 22:45:12 -08001006 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1007 return BATTERY_STATUS;
1008 }
1009
1010 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
1011
1012 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
1013 return std::nullopt;
1014 }
Kim Low03ea0352020-11-06 12:45:07 -08001015
Chris Ye3fdbfef2021-01-06 18:45:18 -08001016 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1017 std::vector<int32_t> ids;
1018 for (const auto& [rawId, info] : mRawLightInfos) {
1019 ids.push_back(rawId);
1020 }
1021 return ids;
1022 }
1023
1024 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1025 auto it = mRawLightInfos.find(lightId);
1026 if (it == mRawLightInfos.end()) {
1027 return std::nullopt;
1028 }
1029 return it->second;
1030 }
1031
1032 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1033 mLightBrightness.emplace(lightId, brightness);
1034 }
1035
1036 void setLightIntensities(int32_t deviceId, int32_t lightId,
1037 std::unordered_map<LightColor, int32_t> intensities) override {
1038 mLightIntensities.emplace(lightId, intensities);
1039 };
1040
1041 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1042 auto lightIt = mLightBrightness.find(lightId);
1043 if (lightIt == mLightBrightness.end()) {
1044 return std::nullopt;
1045 }
1046 return lightIt->second;
1047 }
1048
1049 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1050 int32_t deviceId, int32_t lightId) override {
1051 auto lightIt = mLightIntensities.find(lightId);
1052 if (lightIt == mLightIntensities.end()) {
1053 return std::nullopt;
1054 }
1055 return lightIt->second;
1056 };
1057
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001058 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001059 return false;
1060 }
1061
Chris Yea52ade12020-08-27 16:49:20 -07001062 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063
Chris Yea52ade12020-08-27 16:49:20 -07001064 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Chris Yea52ade12020-08-27 16:49:20 -07001066 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067
Chris Yea52ade12020-08-27 16:49:20 -07001068 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069};
1070
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071// --- FakeInputMapper ---
1072
1073class FakeInputMapper : public InputMapper {
1074 uint32_t mSources;
1075 int32_t mKeyboardType;
1076 int32_t mMetaState;
1077 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1078 KeyedVector<int32_t, int32_t> mScanCodeStates;
1079 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001080 // fake mapping which would normally come from keyCharacterMap
1081 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001082 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001084 std::mutex mLock;
1085 std::condition_variable mStateChangedCondition;
1086 bool mConfigureWasCalled GUARDED_BY(mLock);
1087 bool mResetWasCalled GUARDED_BY(mLock);
1088 bool mProcessWasCalled GUARDED_BY(mLock);
1089 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090
Arthur Hungc23540e2018-11-29 20:42:11 +08001091 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001093 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1094 : InputMapper(deviceContext),
1095 mSources(sources),
1096 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001098 mConfigureWasCalled(false),
1099 mResetWasCalled(false),
1100 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101
Chris Yea52ade12020-08-27 16:49:20 -07001102 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103
1104 void setKeyboardType(int32_t keyboardType) {
1105 mKeyboardType = keyboardType;
1106 }
1107
1108 void setMetaState(int32_t metaState) {
1109 mMetaState = metaState;
1110 }
1111
1112 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001113 std::unique_lock<std::mutex> lock(mLock);
1114 base::ScopedLockAssertion assumeLocked(mLock);
1115 const bool configureCalled =
1116 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1117 return mConfigureWasCalled;
1118 });
1119 if (!configureCalled) {
1120 FAIL() << "Expected configure() to have been called.";
1121 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001122 mConfigureWasCalled = false;
1123 }
1124
1125 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001126 std::unique_lock<std::mutex> lock(mLock);
1127 base::ScopedLockAssertion assumeLocked(mLock);
1128 const bool resetCalled =
1129 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1130 return mResetWasCalled;
1131 });
1132 if (!resetCalled) {
1133 FAIL() << "Expected reset() to have been called.";
1134 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 mResetWasCalled = false;
1136 }
1137
Yi Kong9b14ac62018-07-17 13:48:38 -07001138 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001139 std::unique_lock<std::mutex> lock(mLock);
1140 base::ScopedLockAssertion assumeLocked(mLock);
1141 const bool processCalled =
1142 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1143 return mProcessWasCalled;
1144 });
1145 if (!processCalled) {
1146 FAIL() << "Expected process() to have been called.";
1147 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 if (outLastEvent) {
1149 *outLastEvent = mLastEvent;
1150 }
1151 mProcessWasCalled = false;
1152 }
1153
1154 void setKeyCodeState(int32_t keyCode, int32_t state) {
1155 mKeyCodeStates.replaceValueFor(keyCode, state);
1156 }
1157
1158 void setScanCodeState(int32_t scanCode, int32_t state) {
1159 mScanCodeStates.replaceValueFor(scanCode, state);
1160 }
1161
1162 void setSwitchState(int32_t switchCode, int32_t state) {
1163 mSwitchStates.replaceValueFor(switchCode, state);
1164 }
1165
1166 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001167 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 }
1169
Philip Junker4af3b3d2021-12-14 10:36:55 +01001170 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1171 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1172 }
1173
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001175 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176
Chris Yea52ade12020-08-27 16:49:20 -07001177 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178 InputMapper::populateDeviceInfo(deviceInfo);
1179
1180 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1181 deviceInfo->setKeyboardType(mKeyboardType);
1182 }
1183 }
1184
Chris Yea52ade12020-08-27 16:49:20 -07001185 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001186 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001188
1189 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001190 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001191 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1192 mViewport = config->getDisplayViewportByPort(*displayPort);
1193 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001194
1195 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 }
1197
Chris Yea52ade12020-08-27 16:49:20 -07001198 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001199 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001201 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 }
1203
Chris Yea52ade12020-08-27 16:49:20 -07001204 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001205 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 mLastEvent = *rawEvent;
1207 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001208 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 }
1210
Chris Yea52ade12020-08-27 16:49:20 -07001211 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001212 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1213 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1214 }
1215
Philip Junker4af3b3d2021-12-14 10:36:55 +01001216 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1217 auto it = mKeyCodeMapping.find(locationKeyCode);
1218 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1219 }
1220
Chris Yea52ade12020-08-27 16:49:20 -07001221 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1223 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1224 }
1225
Chris Yea52ade12020-08-27 16:49:20 -07001226 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001227 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1228 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1229 }
1230
Chris Yea52ade12020-08-27 16:49:20 -07001231 // Return true if the device has non-empty key layout.
1232 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1233 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234 for (size_t i = 0; i < numCodes; i++) {
1235 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1236 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1237 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238 }
1239 }
1240 }
Chris Yea52ade12020-08-27 16:49:20 -07001241 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 return result;
1243 }
1244
1245 virtual int32_t getMetaState() {
1246 return mMetaState;
1247 }
1248
1249 virtual void fadePointer() {
1250 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001251
1252 virtual std::optional<int32_t> getAssociatedDisplay() {
1253 if (mViewport) {
1254 return std::make_optional(mViewport->displayId);
1255 }
1256 return std::nullopt;
1257 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258};
1259
1260
1261// --- InstrumentedInputReader ---
1262
1263class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001264 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265
1266public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001267 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1268 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001269 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001270 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001271
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001272 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001274 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001276 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001277 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278 InputDeviceIdentifier identifier;
1279 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001280 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001282 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 }
1284
Prabir Pradhan28efc192019-11-05 01:10:04 +00001285 // Make the protected loopOnce method accessible to tests.
1286 using InputReader::loopOnce;
1287
Michael Wrightd02c5b62014-02-10 15:10:22 -08001288protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001289 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1290 const InputDeviceIdentifier& identifier)
1291 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001292 if (!mNextDevices.empty()) {
1293 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1294 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295 return device;
1296 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001297 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001298 }
1299
arthurhungdcef2dc2020-08-11 14:47:50 +08001300 // --- FakeInputReaderContext ---
1301 class FakeInputReaderContext : public ContextImpl {
1302 int32_t mGlobalMetaState;
1303 bool mUpdateGlobalMetaStateWasCalled;
1304 int32_t mGeneration;
1305
1306 public:
1307 FakeInputReaderContext(InputReader* reader)
1308 : ContextImpl(reader),
1309 mGlobalMetaState(0),
1310 mUpdateGlobalMetaStateWasCalled(false),
1311 mGeneration(1) {}
1312
1313 virtual ~FakeInputReaderContext() {}
1314
1315 void assertUpdateGlobalMetaStateWasCalled() {
1316 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1317 << "Expected updateGlobalMetaState() to have been called.";
1318 mUpdateGlobalMetaStateWasCalled = false;
1319 }
1320
1321 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1322
1323 uint32_t getGeneration() { return mGeneration; }
1324
1325 void updateGlobalMetaState() override {
1326 mUpdateGlobalMetaStateWasCalled = true;
1327 ContextImpl::updateGlobalMetaState();
1328 }
1329
1330 int32_t getGlobalMetaState() override {
1331 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1332 }
1333
1334 int32_t bumpGeneration() override {
1335 mGeneration = ContextImpl::bumpGeneration();
1336 return mGeneration;
1337 }
1338 } mFakeContext;
1339
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001341
1342public:
1343 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001344};
1345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346// --- InputReaderPolicyTest ---
1347class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001348protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001349 sp<FakeInputReaderPolicy> mFakePolicy;
1350
Chris Yea52ade12020-08-27 16:49:20 -07001351 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1352 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001353};
1354
1355/**
1356 * Check that empty set of viewports is an acceptable configuration.
1357 * Also try to get internal viewport two different ways - by type and by uniqueId.
1358 *
1359 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1360 * Such configuration is not currently allowed.
1361 */
1362TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001363 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364
1365 // We didn't add any viewports yet, so there shouldn't be any.
1366 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001367 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368 ASSERT_FALSE(internalViewport);
1369
1370 // Add an internal viewport, then clear it
1371 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001372 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001373 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374
1375 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001376 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001377 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001378 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379
1380 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001381 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001382 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001383 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384
1385 mFakePolicy->clearViewports();
1386 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001387 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 ASSERT_FALSE(internalViewport);
1391}
1392
1393TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1394 const std::string internalUniqueId = "local:0";
1395 const std::string externalUniqueId = "local:1";
1396 const std::string virtualUniqueId1 = "virtual:2";
1397 const std::string virtualUniqueId2 = "virtual:3";
1398 constexpr int32_t virtualDisplayId1 = 2;
1399 constexpr int32_t virtualDisplayId2 = 3;
1400
1401 // Add an internal viewport
1402 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001403 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1404 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001405 // Add an external viewport
1406 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001407 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1408 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001409 // Add an virtual viewport
1410 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001411 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1412 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 // Add another virtual viewport
1414 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1416 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417
1418 // Check matching by type for internal
1419 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001420 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 ASSERT_TRUE(internalViewport);
1422 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1423
1424 // Check matching by type for external
1425 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001426 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_TRUE(externalViewport);
1428 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1429
1430 // Check matching by uniqueId for virtual viewport #1
1431 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001432 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001433 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001434 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1436 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1437
1438 // Check matching by uniqueId for virtual viewport #2
1439 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1444 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1445}
1446
1447
1448/**
1449 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1450 * that lookup works by checking display id.
1451 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1452 */
1453TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1454 const std::string uniqueId1 = "uniqueId1";
1455 const std::string uniqueId2 = "uniqueId2";
1456 constexpr int32_t displayId1 = 2;
1457 constexpr int32_t displayId2 = 3;
1458
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001459 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1460 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001461 for (const ViewportType& type : types) {
1462 mFakePolicy->clearViewports();
1463 // Add a viewport
1464 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001465 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1466 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001467 // Add another viewport
1468 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001469 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1470 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001471
1472 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001473 std::optional<DisplayViewport> viewport1 =
1474 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 ASSERT_TRUE(viewport1);
1476 ASSERT_EQ(displayId1, viewport1->displayId);
1477 ASSERT_EQ(type, viewport1->type);
1478
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001479 std::optional<DisplayViewport> viewport2 =
1480 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001481 ASSERT_TRUE(viewport2);
1482 ASSERT_EQ(displayId2, viewport2->displayId);
1483 ASSERT_EQ(type, viewport2->type);
1484
1485 // When there are multiple viewports of the same kind, and uniqueId is not specified
1486 // in the call to getDisplayViewport, then that situation is not supported.
1487 // The viewports can be stored in any order, so we cannot rely on the order, since that
1488 // is just implementation detail.
1489 // However, we can check that it still returns *a* viewport, we just cannot assert
1490 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001491 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001492 ASSERT_TRUE(someViewport);
1493 }
1494}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001496/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001497 * When we have multiple internal displays make sure we always return the default display when
1498 * querying by type.
1499 */
1500TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1501 const std::string uniqueId1 = "uniqueId1";
1502 const std::string uniqueId2 = "uniqueId2";
1503 constexpr int32_t nonDefaultDisplayId = 2;
1504 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1505 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1506
1507 // Add the default display first and ensure it gets returned.
1508 mFakePolicy->clearViewports();
1509 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001510 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001511 ViewportType::INTERNAL);
1512 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001513 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001514 ViewportType::INTERNAL);
1515
1516 std::optional<DisplayViewport> viewport =
1517 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1518 ASSERT_TRUE(viewport);
1519 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1520 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1521
1522 // Add the default display second to make sure order doesn't matter.
1523 mFakePolicy->clearViewports();
1524 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001525 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001526 ViewportType::INTERNAL);
1527 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001528 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001529 ViewportType::INTERNAL);
1530
1531 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1532 ASSERT_TRUE(viewport);
1533 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1534 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1535}
1536
1537/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001538 * Check getDisplayViewportByPort
1539 */
1540TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001541 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001542 const std::string uniqueId1 = "uniqueId1";
1543 const std::string uniqueId2 = "uniqueId2";
1544 constexpr int32_t displayId1 = 1;
1545 constexpr int32_t displayId2 = 2;
1546 const uint8_t hdmi1 = 0;
1547 const uint8_t hdmi2 = 1;
1548 const uint8_t hdmi3 = 2;
1549
1550 mFakePolicy->clearViewports();
1551 // Add a viewport that's associated with some display port that's not of interest.
1552 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001553 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1554 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001555 // Add another viewport, connected to HDMI1 port
1556 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001557 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1558 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001559
1560 // Check that correct display viewport was returned by comparing the display ports.
1561 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1562 ASSERT_TRUE(hdmi1Viewport);
1563 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1564 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1565
1566 // Check that we can still get the same viewport using the uniqueId
1567 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1568 ASSERT_TRUE(hdmi1Viewport);
1569 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1570 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1571 ASSERT_EQ(type, hdmi1Viewport->type);
1572
1573 // Check that we cannot find a port with "HDMI2", because we never added one
1574 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1575 ASSERT_FALSE(hdmi2Viewport);
1576}
1577
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578// --- InputReaderTest ---
1579
1580class InputReaderTest : public testing::Test {
1581protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001582 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001583 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001584 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001585 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586
Chris Yea52ade12020-08-27 16:49:20 -07001587 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001588 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001589 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001590 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591
Prabir Pradhan28efc192019-11-05 01:10:04 +00001592 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001593 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 }
1595
Chris Yea52ade12020-08-27 16:49:20 -07001596 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001597 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001599 }
1600
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001601 void addDevice(int32_t eventHubId, const std::string& name,
1602 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001603 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604
1605 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 }
1608 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001609 mReader->loopOnce();
1610 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001611 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1612 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613 }
1614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001615 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001616 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001617 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001618 }
1619
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001620 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001622 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 }
1624
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001626 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001627 ftl::Flags<InputDeviceClass> classes,
1628 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001629 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1631 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001632 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634 return mapper;
1635 }
1636};
1637
Chris Ye98d3f532020-10-01 21:48:59 -07001638TEST_F(InputReaderTest, PolicyGetInputDevices) {
1639 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001640 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001641 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642
1643 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001644 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001647 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1649 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001650 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651}
1652
Chris Yee7310032020-09-22 15:36:28 -07001653TEST_F(InputReaderTest, GetMergedInputDevices) {
1654 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1655 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1656 // Add two subdevices to device
1657 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1658 // Must add at least one mapper or the device will be ignored!
1659 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1660 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1661
1662 // Push same device instance for next device to be added, so they'll have same identifier.
1663 mReader->pushNextDevice(device);
1664 mReader->pushNextDevice(device);
1665 ASSERT_NO_FATAL_FAILURE(
1666 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1667 ASSERT_NO_FATAL_FAILURE(
1668 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1669
1670 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001671 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001672}
1673
Chris Yee14523a2020-12-19 13:46:00 -08001674TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1675 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1676 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1677 // Add two subdevices to device
1678 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1679 // Must add at least one mapper or the device will be ignored!
1680 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1681 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1682
1683 // Push same device instance for next device to be added, so they'll have same identifier.
1684 mReader->pushNextDevice(device);
1685 mReader->pushNextDevice(device);
1686 // Sensor device is initially disabled
1687 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1688 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1689 nullptr));
1690 // Device is disabled because the only sub device is a sensor device and disabled initially.
1691 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1692 ASSERT_FALSE(device->isEnabled());
1693 ASSERT_NO_FATAL_FAILURE(
1694 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1695 // The merged device is enabled if any sub device is enabled
1696 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1697 ASSERT_TRUE(device->isEnabled());
1698}
1699
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001700TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001701 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001702 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001703 constexpr int32_t eventHubId = 1;
1704 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001705 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001707 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001709
Yi Kong9b14ac62018-07-17 13:48:38 -07001710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001711
1712 NotifyDeviceResetArgs resetArgs;
1713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714 ASSERT_EQ(deviceId, resetArgs.deviceId);
1715
1716 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001717 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001718 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001719
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001721 ASSERT_EQ(deviceId, resetArgs.deviceId);
1722 ASSERT_EQ(device->isEnabled(), false);
1723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001724 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001725 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001728 ASSERT_EQ(device->isEnabled(), false);
1729
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001730 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001731 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001733 ASSERT_EQ(deviceId, resetArgs.deviceId);
1734 ASSERT_EQ(device->isEnabled(), true);
1735}
1736
Michael Wrightd02c5b62014-02-10 15:10:22 -08001737TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001738 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001739 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001741 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001743 AINPUT_SOURCE_KEYBOARD, nullptr);
1744 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001745
1746 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1747 AINPUT_SOURCE_ANY, AKEYCODE_A))
1748 << "Should return unknown when the device id is >= 0 but unknown.";
1749
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001750 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1751 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1752 << "Should return unknown when the device id is valid but the sources are not "
1753 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001754
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001755 ASSERT_EQ(AKEY_STATE_DOWN,
1756 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1757 AKEYCODE_A))
1758 << "Should return value provided by mapper when device id is valid and the device "
1759 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001760
1761 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1762 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1763 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1764
1765 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1766 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1767 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1768}
1769
Philip Junker4af3b3d2021-12-14 10:36:55 +01001770TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1771 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1772 constexpr int32_t eventHubId = 1;
1773 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1774 InputDeviceClass::KEYBOARD,
1775 AINPUT_SOURCE_KEYBOARD, nullptr);
1776 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1777
1778 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1779 << "Should return unknown when the device with the specified id is not found.";
1780
1781 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1782 << "Should return correct mapping when device id is valid and mapping exists.";
1783
1784 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1785 << "Should return the location key code when device id is valid and there's no "
1786 "mapping.";
1787}
1788
1789TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1790 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1791 constexpr int32_t eventHubId = 1;
1792 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1793 InputDeviceClass::JOYSTICK,
1794 AINPUT_SOURCE_GAMEPAD, nullptr);
1795 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1796
1797 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1798 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1799}
1800
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001802 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001803 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001805 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 AINPUT_SOURCE_KEYBOARD, nullptr);
1808 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809
1810 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1811 AINPUT_SOURCE_ANY, KEY_A))
1812 << "Should return unknown when the device id is >= 0 but unknown.";
1813
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1815 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1816 << "Should return unknown when the device id is valid but the sources are not "
1817 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 ASSERT_EQ(AKEY_STATE_DOWN,
1820 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1821 KEY_A))
1822 << "Should return value provided by mapper when device id is valid and the device "
1823 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824
1825 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1826 AINPUT_SOURCE_TRACKBALL, KEY_A))
1827 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1828
1829 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1830 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1831 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1832}
1833
1834TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001835 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001836 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001838 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001839 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001840 AINPUT_SOURCE_KEYBOARD, nullptr);
1841 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842
1843 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1844 AINPUT_SOURCE_ANY, SW_LID))
1845 << "Should return unknown when the device id is >= 0 but unknown.";
1846
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1848 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1849 << "Should return unknown when the device id is valid but the sources are not "
1850 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 ASSERT_EQ(AKEY_STATE_DOWN,
1853 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1854 SW_LID))
1855 << "Should return value provided by mapper when device id is valid and the device "
1856 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857
1858 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1859 AINPUT_SOURCE_TRACKBALL, SW_LID))
1860 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1861
1862 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1863 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1864 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1865}
1866
1867TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001869 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001875 mapper.addSupportedKeyCode(AKEYCODE_A);
1876 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001877
1878 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1879 uint8_t flags[4] = { 0, 0, 0, 1 };
1880
1881 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1882 << "Should return false when device id is >= 0 but unknown.";
1883 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1884
1885 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001886 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1887 << "Should return false when device id is valid but the sources are not supported by "
1888 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1890
1891 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001892 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1893 keyCodes, flags))
1894 << "Should return value provided by mapper when device id is valid and the device "
1895 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1897
1898 flags[3] = 1;
1899 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1900 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1901 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1902
1903 flags[3] = 1;
1904 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1905 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1906 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1907}
1908
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001909TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001911 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001912
1913 NotifyConfigurationChangedArgs args;
1914
1915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1916 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1917}
1918
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001919TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001920 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001921 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001922 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001923 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001924 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001925 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001927 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001928
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001929 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001930 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001931 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1932
1933 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001934 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001935 ASSERT_EQ(when, event.when);
1936 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001937 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001938 ASSERT_EQ(EV_KEY, event.type);
1939 ASSERT_EQ(KEY_A, event.code);
1940 ASSERT_EQ(1, event.value);
1941}
1942
Garfield Tan1c7bc862020-01-28 13:24:04 -08001943TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001944 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001945 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001946 constexpr int32_t eventHubId = 1;
1947 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001948 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001949 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001950 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001951 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001952
1953 NotifyDeviceResetArgs resetArgs;
1954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001955 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001957 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001958 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001960 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001961 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001963 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001964 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001966 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001967 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001969 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001970 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001972 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001973 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001974}
1975
Garfield Tan1c7bc862020-01-28 13:24:04 -08001976TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1977 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001978 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001979 constexpr int32_t eventHubId = 1;
1980 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1981 // Must add at least one mapper or the device will be ignored!
1982 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001983 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001984 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1985
1986 NotifyDeviceResetArgs resetArgs;
1987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1988 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1989}
1990
Arthur Hungc23540e2018-11-29 20:42:11 +08001991TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001992 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001993 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001994 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001995 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001996 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1997 FakeInputMapper& mapper =
1998 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001999 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002000
2001 const uint8_t hdmi1 = 1;
2002
2003 // Associated touch screen with second display.
2004 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2005
2006 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002007 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002008 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002009 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002010 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002011 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002012 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002013 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002014 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002015 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002016
2017 // Add the device, and make sure all of the callbacks are triggered.
2018 // The device is added after the input port associations are processed since
2019 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002020 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002023 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002024
Arthur Hung2c9a3342019-07-23 14:18:59 +08002025 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002026 ASSERT_EQ(deviceId, device->getId());
2027 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2028 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002029
2030 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002031 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002032 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002033 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002034}
2035
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002036TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2037 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002038 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002039 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2040 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2041 // Must add at least one mapper or the device will be ignored!
2042 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2043 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2044 mReader->pushNextDevice(device);
2045 mReader->pushNextDevice(device);
2046 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2047 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2048
2049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2050
2051 NotifyDeviceResetArgs resetArgs;
2052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2053 ASSERT_EQ(deviceId, resetArgs.deviceId);
2054 ASSERT_TRUE(device->isEnabled());
2055 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2056 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2057
2058 disableDevice(deviceId);
2059 mReader->loopOnce();
2060
2061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2062 ASSERT_EQ(deviceId, resetArgs.deviceId);
2063 ASSERT_FALSE(device->isEnabled());
2064 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2065 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2066
2067 enableDevice(deviceId);
2068 mReader->loopOnce();
2069
2070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2071 ASSERT_EQ(deviceId, resetArgs.deviceId);
2072 ASSERT_TRUE(device->isEnabled());
2073 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2074 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2075}
2076
2077TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2078 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002079 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002080 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2081 // Add two subdevices to device
2082 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2083 FakeInputMapper& mapperDevice1 =
2084 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2085 FakeInputMapper& mapperDevice2 =
2086 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2087 mReader->pushNextDevice(device);
2088 mReader->pushNextDevice(device);
2089 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2090 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2091
2092 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2093 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2094
2095 ASSERT_EQ(AKEY_STATE_DOWN,
2096 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2097 ASSERT_EQ(AKEY_STATE_DOWN,
2098 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2099 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2100 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2101}
2102
Prabir Pradhan7e186182020-11-10 13:56:45 -08002103TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2104 NotifyPointerCaptureChangedArgs args;
2105
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002106 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002107 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2108 mReader->loopOnce();
2109 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002110 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2111 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002112
2113 mFakePolicy->setPointerCapture(false);
2114 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2115 mReader->loopOnce();
2116 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002117 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002118
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002119 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002120 // does not change.
2121 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2122 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002123 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002124}
2125
Chris Ye87143712020-11-10 05:05:58 +00002126class FakeVibratorInputMapper : public FakeInputMapper {
2127public:
2128 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2129 : FakeInputMapper(deviceContext, sources) {}
2130
2131 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2132};
2133
2134TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2135 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002136 ftl::Flags<InputDeviceClass> deviceClass =
2137 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002138 constexpr int32_t eventHubId = 1;
2139 const char* DEVICE_LOCATION = "BLUETOOTH";
2140 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2141 FakeVibratorInputMapper& mapper =
2142 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2143 mReader->pushNextDevice(device);
2144
2145 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2146 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2147
2148 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2149 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2150}
2151
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002152// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002153
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002154class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002155public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002156 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002157
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002158 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002159
2160 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2161
2162 void dump(std::string& dump) override {}
2163
2164 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2165 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002166 }
2167
Chris Yee2b1e5c2021-03-10 22:45:12 -08002168 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2169 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002170 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002171
2172 bool setLightColor(int32_t lightId, int32_t color) override {
2173 getDeviceContext().setLightBrightness(lightId, color >> 24);
2174 return true;
2175 }
2176
2177 std::optional<int32_t> getLightColor(int32_t lightId) override {
2178 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2179 if (!result.has_value()) {
2180 return std::nullopt;
2181 }
2182 return result.value() << 24;
2183 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002184
2185 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2186
2187 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2188
2189private:
2190 InputDeviceContext& mDeviceContext;
2191 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2192 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002193};
2194
Chris Yee2b1e5c2021-03-10 22:45:12 -08002195TEST_F(InputReaderTest, BatteryGetCapacity) {
2196 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002197 ftl::Flags<InputDeviceClass> deviceClass =
2198 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002199 constexpr int32_t eventHubId = 1;
2200 const char* DEVICE_LOCATION = "BLUETOOTH";
2201 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002202 FakePeripheralController& controller =
2203 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002204 mReader->pushNextDevice(device);
2205
2206 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2207
2208 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2209 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2210}
2211
2212TEST_F(InputReaderTest, BatteryGetStatus) {
2213 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002214 ftl::Flags<InputDeviceClass> deviceClass =
2215 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002216 constexpr int32_t eventHubId = 1;
2217 const char* DEVICE_LOCATION = "BLUETOOTH";
2218 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002219 FakePeripheralController& controller =
2220 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002221 mReader->pushNextDevice(device);
2222
2223 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2224
2225 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2226 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2227}
2228
Chris Ye3fdbfef2021-01-06 18:45:18 -08002229TEST_F(InputReaderTest, LightGetColor) {
2230 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002231 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002232 constexpr int32_t eventHubId = 1;
2233 const char* DEVICE_LOCATION = "BLUETOOTH";
2234 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002235 FakePeripheralController& controller =
2236 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002237 mReader->pushNextDevice(device);
2238 RawLightInfo info = {.id = 1,
2239 .name = "Mono",
2240 .maxBrightness = 255,
2241 .flags = InputLightClass::BRIGHTNESS,
2242 .path = ""};
2243 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2244 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2245
2246 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002247
Chris Yee2b1e5c2021-03-10 22:45:12 -08002248 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2249 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002250 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2251 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2252}
2253
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002254// --- InputReaderIntegrationTest ---
2255
2256// These tests create and interact with the InputReader only through its interface.
2257// The InputReader is started during SetUp(), which starts its processing in its own
2258// thread. The tests use linux uinput to emulate input devices.
2259// NOTE: Interacting with the physical device while these tests are running may cause
2260// the tests to fail.
2261class InputReaderIntegrationTest : public testing::Test {
2262protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002263 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002264 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002265 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002266
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002267 std::shared_ptr<FakePointerController> mFakePointerController;
2268
Chris Yea52ade12020-08-27 16:49:20 -07002269 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002270 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002271 mFakePointerController = std::make_shared<FakePointerController>();
2272 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002273 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2274 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002275
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002276 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2277 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002278 ASSERT_EQ(mReader->start(), OK);
2279
2280 // Since this test is run on a real device, all the input devices connected
2281 // to the test device will show up in mReader. We wait for those input devices to
2282 // show up before beginning the tests.
2283 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2284 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2285 }
2286
Chris Yea52ade12020-08-27 16:49:20 -07002287 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002288 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002289 mReader.reset();
2290 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002291 mFakePolicy.clear();
2292 }
2293};
2294
2295TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2296 // An invalid input device that is only used for this test.
2297 class InvalidUinputDevice : public UinputDevice {
2298 public:
2299 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2300
2301 private:
2302 void configureDevice(int fd, uinput_user_dev* device) override {}
2303 };
2304
2305 const size_t numDevices = mFakePolicy->getInputDevices().size();
2306
2307 // UinputDevice does not set any event or key bits, so InputReader should not
2308 // consider it as a valid device.
2309 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2310 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2311 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2312 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2313
2314 invalidDevice.reset();
2315 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2316 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2317 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2318}
2319
2320TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2321 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2322
2323 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2324 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2325 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2326 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2327
2328 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002329 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002330 const auto& it =
2331 std::find_if(inputDevices.begin(), inputDevices.end(),
2332 [&keyboard](const InputDeviceInfo& info) {
2333 return info.getIdentifier().name == keyboard->getName();
2334 });
2335
2336 ASSERT_NE(it, inputDevices.end());
2337 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2338 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2339 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002340
2341 keyboard.reset();
2342 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2343 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2344 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2345}
2346
2347TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2348 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2349 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2350
2351 NotifyConfigurationChangedArgs configChangedArgs;
2352 ASSERT_NO_FATAL_FAILURE(
2353 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002354 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002355 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2356
2357 NotifyKeyArgs keyArgs;
2358 keyboard->pressAndReleaseHomeKey();
2359 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2360 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002361 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002362 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002363 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002364 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002365 prevTimestamp = keyArgs.eventTime;
2366
2367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2368 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002369 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002370 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002371 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002372}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002373
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002374/**
2375 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2376 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2377 * are passed to the listener.
2378 */
2379static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2380TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2381 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2382 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2383 NotifyKeyArgs keyArgs;
2384
2385 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2386 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2387 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2388 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2389
2390 controller->pressAndReleaseKey(BTN_GEAR_UP);
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_UP, keyArgs.scanCode);
2394}
2395
Arthur Hungaab25622020-01-16 11:22:11 +08002396// --- TouchProcessTest ---
2397class TouchIntegrationTest : public InputReaderIntegrationTest {
2398protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002399 const std::string UNIQUE_ID = "local:0";
2400
Chris Yea52ade12020-08-27 16:49:20 -07002401 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002402 InputReaderIntegrationTest::SetUp();
2403 // At least add an internal display.
2404 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2405 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002406 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002407
2408 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2409 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2410 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2411 }
2412
2413 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2414 int32_t orientation, const std::string& uniqueId,
2415 std::optional<uint8_t> physicalPort,
2416 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002417 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2418 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002419 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2420 }
2421
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002422 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2423 NotifyMotionArgs args;
2424 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2425 EXPECT_EQ(action, args.action);
2426 ASSERT_EQ(points.size(), args.pointerCount);
2427 for (size_t i = 0; i < args.pointerCount; i++) {
2428 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2429 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2430 }
2431 }
2432
Arthur Hungaab25622020-01-16 11:22:11 +08002433 std::unique_ptr<UinputTouchScreen> mDevice;
2434};
2435
2436TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2437 NotifyMotionArgs args;
2438 const Point centerPoint = mDevice->getCenterPoint();
2439
2440 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002441 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002442 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002443 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2445 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2446
2447 // ACTION_MOVE
2448 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002449 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2452
2453 // ACTION_UP
2454 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002455 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002456 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2457 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2458}
2459
2460TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2461 NotifyMotionArgs args;
2462 const Point centerPoint = mDevice->getCenterPoint();
2463
2464 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002465 mDevice->sendSlot(FIRST_SLOT);
2466 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002467 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002468 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2470 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2471
2472 // ACTION_POINTER_DOWN (Second slot)
2473 const Point secondPoint = centerPoint + Point(100, 100);
2474 mDevice->sendSlot(SECOND_SLOT);
2475 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476 mDevice->sendDown(secondPoint);
2477 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002479 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002480
2481 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002482 mDevice->sendMove(secondPoint + Point(1, 1));
2483 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002484 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2486
2487 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002488 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002489 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002490 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002491 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002492
2493 // ACTION_UP
2494 mDevice->sendSlot(FIRST_SLOT);
2495 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002496 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2498 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2499}
2500
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002501/**
2502 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2503 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2504 * data?
2505 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2506 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2507 * for Pointer 0 only is generated after.
2508 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2509 * events, we will not miss any information.
2510 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2511 * event generated afterwards that contains the newest movement of pointer 0.
2512 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2513 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2514 * losing information about non-palm pointers.
2515 */
2516TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2517 NotifyMotionArgs args;
2518 const Point centerPoint = mDevice->getCenterPoint();
2519
2520 // ACTION_DOWN
2521 mDevice->sendSlot(FIRST_SLOT);
2522 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2523 mDevice->sendDown(centerPoint);
2524 mDevice->sendSync();
2525 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2526
2527 // ACTION_POINTER_DOWN (Second slot)
2528 const Point secondPoint = centerPoint + Point(100, 100);
2529 mDevice->sendSlot(SECOND_SLOT);
2530 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2531 mDevice->sendDown(secondPoint);
2532 mDevice->sendSync();
2533 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2534
2535 // ACTION_MOVE (First slot)
2536 mDevice->sendSlot(FIRST_SLOT);
2537 mDevice->sendMove(centerPoint + Point(5, 5));
2538 // ACTION_POINTER_UP (Second slot)
2539 mDevice->sendSlot(SECOND_SLOT);
2540 mDevice->sendPointerUp();
2541 // Send a single sync for the above 2 pointer updates
2542 mDevice->sendSync();
2543
2544 // First, we should get POINTER_UP for the second pointer
2545 assertReceivedMotion(ACTION_POINTER_1_UP,
2546 {/*first pointer */ centerPoint + Point(5, 5),
2547 /*second pointer*/ secondPoint});
2548
2549 // Next, the MOVE event for the first pointer
2550 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2551}
2552
2553/**
2554 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2555 * move, and then it will go up, all in the same frame.
2556 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2557 * gets sent to the listener.
2558 */
2559TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2560 NotifyMotionArgs args;
2561 const Point centerPoint = mDevice->getCenterPoint();
2562
2563 // ACTION_DOWN
2564 mDevice->sendSlot(FIRST_SLOT);
2565 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2566 mDevice->sendDown(centerPoint);
2567 mDevice->sendSync();
2568 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2569
2570 // ACTION_POINTER_DOWN (Second slot)
2571 const Point secondPoint = centerPoint + Point(100, 100);
2572 mDevice->sendSlot(SECOND_SLOT);
2573 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2574 mDevice->sendDown(secondPoint);
2575 mDevice->sendSync();
2576 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2577
2578 // ACTION_MOVE (First slot)
2579 mDevice->sendSlot(FIRST_SLOT);
2580 mDevice->sendMove(centerPoint + Point(5, 5));
2581 // ACTION_POINTER_UP (Second slot)
2582 mDevice->sendSlot(SECOND_SLOT);
2583 mDevice->sendMove(secondPoint + Point(6, 6));
2584 mDevice->sendPointerUp();
2585 // Send a single sync for the above 2 pointer updates
2586 mDevice->sendSync();
2587
2588 // First, we should get POINTER_UP for the second pointer
2589 // The movement of the second pointer during the liftoff frame is ignored.
2590 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2591 assertReceivedMotion(ACTION_POINTER_1_UP,
2592 {/*first pointer */ centerPoint + Point(5, 5),
2593 /*second pointer*/ secondPoint});
2594
2595 // Next, the MOVE event for the first pointer
2596 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2597}
2598
Arthur Hungaab25622020-01-16 11:22:11 +08002599TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2600 NotifyMotionArgs args;
2601 const Point centerPoint = mDevice->getCenterPoint();
2602
2603 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002604 mDevice->sendSlot(FIRST_SLOT);
2605 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002606 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002607 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002608 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2609 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2610
arthurhungcc7f9802020-04-30 17:55:40 +08002611 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002612 const Point secondPoint = centerPoint + Point(100, 100);
2613 mDevice->sendSlot(SECOND_SLOT);
2614 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2615 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002616 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002617 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002618 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002619
arthurhungcc7f9802020-04-30 17:55:40 +08002620 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002621 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002622 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002623 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2625
arthurhungcc7f9802020-04-30 17:55:40 +08002626 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2627 // a palm event.
2628 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002629 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002630 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002631 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002632 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002633 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002634
arthurhungcc7f9802020-04-30 17:55:40 +08002635 // Send up to second slot, expect first slot send moving.
2636 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002637 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002638 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002640
arthurhungcc7f9802020-04-30 17:55:40 +08002641 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002642 mDevice->sendSlot(FIRST_SLOT);
2643 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002644 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002645
arthurhungcc7f9802020-04-30 17:55:40 +08002646 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2647 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002648}
2649
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651class InputDeviceTest : public testing::Test {
2652protected:
2653 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002654 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655 static const int32_t DEVICE_ID;
2656 static const int32_t DEVICE_GENERATION;
2657 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002658 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002659 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002661 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002663 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002664 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002665 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666
Chris Yea52ade12020-08-27 16:49:20 -07002667 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002668 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002670 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002671 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002672 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673 InputDeviceIdentifier identifier;
2674 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002675 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002676 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002677 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002678 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002679 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002680 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681 }
2682
Chris Yea52ade12020-08-27 16:49:20 -07002683 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002684 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686 }
2687};
2688
2689const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002690const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002691const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2693const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002694const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002695 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002696const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697
2698TEST_F(InputDeviceTest, ImmutableProperties) {
2699 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002700 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002701 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702}
2703
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002704TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2705 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002706}
2707
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2709 // Configuration.
2710 InputReaderConfiguration config;
2711 mDevice->configure(ARBITRARY_TIME, &config, 0);
2712
2713 // Reset.
2714 mDevice->reset(ARBITRARY_TIME);
2715
2716 NotifyDeviceResetArgs resetArgs;
2717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2718 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2719 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2720
2721 // Metadata.
2722 ASSERT_TRUE(mDevice->isIgnored());
2723 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2724
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002725 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002727 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2729 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2730
2731 // State queries.
2732 ASSERT_EQ(0, mDevice->getMetaState());
2733
2734 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2735 << "Ignored device should return unknown key code state.";
2736 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2737 << "Ignored device should return unknown scan code state.";
2738 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2739 << "Ignored device should return unknown switch state.";
2740
2741 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2742 uint8_t flags[2] = { 0, 1 };
2743 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2744 << "Ignored device should never mark any key codes.";
2745 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2746 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2747}
2748
2749TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2750 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002751 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002753 FakeInputMapper& mapper1 =
2754 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002755 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2756 mapper1.setMetaState(AMETA_ALT_ON);
2757 mapper1.addSupportedKeyCode(AKEYCODE_A);
2758 mapper1.addSupportedKeyCode(AKEYCODE_B);
2759 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2760 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2761 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2762 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2763 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002765 FakeInputMapper& mapper2 =
2766 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768
2769 InputReaderConfiguration config;
2770 mDevice->configure(ARBITRARY_TIME, &config, 0);
2771
2772 String8 propertyValue;
2773 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2774 << "Device should have read configuration during configuration phase.";
2775 ASSERT_STREQ("value", propertyValue.string());
2776
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002777 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2778 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779
2780 // Reset
2781 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2783 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
2785 NotifyDeviceResetArgs resetArgs;
2786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2787 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2788 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2789
2790 // Metadata.
2791 ASSERT_FALSE(mDevice->isIgnored());
2792 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2793
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002794 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002796 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002797 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2798 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2799
2800 // State queries.
2801 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2802 << "Should query mappers and combine meta states.";
2803
2804 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2805 << "Should return unknown key code state when source not supported.";
2806 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2807 << "Should return unknown scan code state when source not supported.";
2808 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2809 << "Should return unknown switch state when source not supported.";
2810
2811 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2812 << "Should query mapper when source is supported.";
2813 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2814 << "Should query mapper when source is supported.";
2815 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2816 << "Should query mapper when source is supported.";
2817
2818 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2819 uint8_t flags[4] = { 0, 0, 0, 1 };
2820 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2821 << "Should do nothing when source is unsupported.";
2822 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2823 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2824 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2825 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2826
2827 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2828 << "Should query mapper when source is supported.";
2829 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2830 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2831 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2832 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2833
2834 // Event handling.
2835 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002836 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837 mDevice->process(&event, 1);
2838
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002839 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2840 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841}
2842
Arthur Hung2c9a3342019-07-23 14:18:59 +08002843// A single input device is associated with a specific display. Check that:
2844// 1. Device is disabled if the viewport corresponding to the associated display is not found
2845// 2. Device is disabled when setEnabled API is called
2846TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002847 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002848
2849 // First Configuration.
2850 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2851
2852 // Device should be enabled by default.
2853 ASSERT_TRUE(mDevice->isEnabled());
2854
2855 // Prepare associated info.
2856 constexpr uint8_t hdmi = 1;
2857 const std::string UNIQUE_ID = "local:1";
2858
2859 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2860 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2861 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2862 // Device should be disabled because it is associated with a specific display via
2863 // input port <-> display port association, but the corresponding display is not found
2864 ASSERT_FALSE(mDevice->isEnabled());
2865
2866 // Prepare displays.
2867 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002868 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2869 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002870 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2871 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2872 ASSERT_TRUE(mDevice->isEnabled());
2873
2874 // Device should be disabled after set disable.
2875 mFakePolicy->addDisabledDevice(mDevice->getId());
2876 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2877 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2878 ASSERT_FALSE(mDevice->isEnabled());
2879
2880 // Device should still be disabled even found the associated display.
2881 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2882 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2883 ASSERT_FALSE(mDevice->isEnabled());
2884}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885
Christine Franks1ba71cc2021-04-07 14:37:42 -07002886TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2887 // Device should be enabled by default.
2888 mFakePolicy->clearViewports();
2889 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2890 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2891 ASSERT_TRUE(mDevice->isEnabled());
2892
2893 // Device should be disabled because it is associated with a specific display, but the
2894 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002895 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002896 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2897 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2898 ASSERT_FALSE(mDevice->isEnabled());
2899
2900 // Device should be enabled when a display is found.
2901 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2902 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2903 NO_PORT, ViewportType::INTERNAL);
2904 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2905 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2906 ASSERT_TRUE(mDevice->isEnabled());
2907
2908 // Device should be disabled after set disable.
2909 mFakePolicy->addDisabledDevice(mDevice->getId());
2910 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2911 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2912 ASSERT_FALSE(mDevice->isEnabled());
2913
2914 // Device should still be disabled even found the associated display.
2915 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2916 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2917 ASSERT_FALSE(mDevice->isEnabled());
2918}
2919
Christine Franks2a2293c2022-01-18 11:51:16 -08002920TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2921 mFakePolicy->clearViewports();
2922 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2923 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2924
Christine Franks2a2293c2022-01-18 11:51:16 -08002925 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2926 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2927 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2928 NO_PORT, ViewportType::INTERNAL);
2929 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2930 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2931 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2932}
2933
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934// --- InputMapperTest ---
2935
2936class InputMapperTest : public testing::Test {
2937protected:
2938 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002939 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940 static const int32_t DEVICE_ID;
2941 static const int32_t DEVICE_GENERATION;
2942 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002943 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002944 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002946 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002948 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002949 std::unique_ptr<InstrumentedInputReader> mReader;
2950 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002951
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002952 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002953 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002955 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002956 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002957 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002958 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002959 // Consume the device reset notification generated when adding a new device.
2960 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961 }
2962
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002963 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002964 SetUp(DEVICE_CLASSES);
2965 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002966
Chris Yea52ade12020-08-27 16:49:20 -07002967 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002968 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002970 }
2971
2972 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002973 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 }
2975
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002976 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002977 if (!changes ||
2978 (changes &
2979 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2980 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002981 mReader->requestRefreshConfiguration(changes);
2982 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002983 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002984 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002985 // Loop the reader to flush the input listener queue.
2986 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002987 }
2988
arthurhungdcef2dc2020-08-11 14:47:50 +08002989 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2990 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002991 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002992 InputDeviceIdentifier identifier;
2993 identifier.name = name;
2994 identifier.location = location;
2995 std::shared_ptr<InputDevice> device =
2996 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2997 identifier);
2998 mReader->pushNextDevice(device);
2999 mFakeEventHub->addDevice(eventHubId, name, classes);
3000 mReader->loopOnce();
3001 return device;
3002 }
3003
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003004 template <class T, typename... Args>
3005 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003006 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003007 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003009 mapper.reset(ARBITRARY_TIME);
Prabir Pradhan36690412022-08-05 22:26:56 +00003010 // Loop the reader to flush the input listener queue.
3011 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003012 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003013 }
3014
3015 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003016 int32_t orientation, const std::string& uniqueId,
3017 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003018 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3019 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003020 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3021 }
3022
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003023 void clearViewports() {
3024 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 }
3026
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003027 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3028 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029 RawEvent event;
3030 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003031 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003032 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033 event.type = type;
3034 event.code = code;
3035 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003036 mapper.process(&event);
Prabir Pradhan36690412022-08-05 22:26:56 +00003037 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003038 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003039 }
3040
3041 static void assertMotionRange(const InputDeviceInfo& info,
3042 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3043 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003044 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3046 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3047 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3048 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3049 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3050 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3051 }
3052
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003053 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3054 float size, float touchMajor, float touchMinor, float toolMajor,
3055 float toolMinor, float orientation, float distance,
3056 float scaledAxisEpsilon = 1.f) {
3057 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3058 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3060 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003061 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3062 scaledAxisEpsilon);
3063 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3064 scaledAxisEpsilon);
3065 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3066 scaledAxisEpsilon);
3067 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3068 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3070 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3071 }
3072
Michael Wright17db18e2020-06-26 20:51:44 +01003073 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003075 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 ASSERT_NEAR(x, actualX, 1);
3077 ASSERT_NEAR(y, actualY, 1);
3078 }
3079};
3080
3081const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003082const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003083const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3085const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003086const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3087 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003088const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
3090// --- SwitchInputMapperTest ---
3091
3092class SwitchInputMapperTest : public InputMapperTest {
3093protected:
3094};
3095
3096TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003097 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100}
3101
3102TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003103 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003105 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003106 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003108 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003109 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110}
3111
3112TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003113 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119
3120 NotifySwitchArgs args;
3121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3122 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003123 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3124 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 args.switchMask);
3126 ASSERT_EQ(uint32_t(0), args.policyFlags);
3127}
3128
Chris Ye87143712020-11-10 05:05:58 +00003129// --- VibratorInputMapperTest ---
3130class VibratorInputMapperTest : public InputMapperTest {
3131protected:
3132 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3133};
3134
3135TEST_F(VibratorInputMapperTest, GetSources) {
3136 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3137
3138 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3139}
3140
3141TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3142 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3143
3144 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3145}
3146
3147TEST_F(VibratorInputMapperTest, Vibrate) {
3148 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003149 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003150 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3151
3152 VibrationElement pattern(2);
3153 VibrationSequence sequence(2);
3154 pattern.duration = std::chrono::milliseconds(200);
3155 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3156 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3157 sequence.addElement(pattern);
3158 pattern.duration = std::chrono::milliseconds(500);
3159 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3160 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3161 sequence.addElement(pattern);
3162
3163 std::vector<int64_t> timings = {0, 1};
3164 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3165
3166 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003167 // Start vibrating
3168 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003169 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003170 // Verify vibrator state listener was notified.
3171 mReader->loopOnce();
3172 NotifyVibratorStateArgs args;
3173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3174 ASSERT_EQ(DEVICE_ID, args.deviceId);
3175 ASSERT_TRUE(args.isOn);
3176 // Stop vibrating
3177 mapper.cancelVibrate(VIBRATION_TOKEN);
3178 ASSERT_FALSE(mapper.isVibrating());
3179 // Verify vibrator state listener was notified.
3180 mReader->loopOnce();
3181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3182 ASSERT_EQ(DEVICE_ID, args.deviceId);
3183 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003184}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185
Chris Yef59a2f42020-10-16 12:55:26 -07003186// --- SensorInputMapperTest ---
3187
3188class SensorInputMapperTest : public InputMapperTest {
3189protected:
3190 static const int32_t ACCEL_RAW_MIN;
3191 static const int32_t ACCEL_RAW_MAX;
3192 static const int32_t ACCEL_RAW_FUZZ;
3193 static const int32_t ACCEL_RAW_FLAT;
3194 static const int32_t ACCEL_RAW_RESOLUTION;
3195
3196 static const int32_t GYRO_RAW_MIN;
3197 static const int32_t GYRO_RAW_MAX;
3198 static const int32_t GYRO_RAW_FUZZ;
3199 static const int32_t GYRO_RAW_FLAT;
3200 static const int32_t GYRO_RAW_RESOLUTION;
3201
3202 static const float GRAVITY_MS2_UNIT;
3203 static const float DEGREE_RADIAN_UNIT;
3204
3205 void prepareAccelAxes();
3206 void prepareGyroAxes();
3207 void setAccelProperties();
3208 void setGyroProperties();
3209 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3210};
3211
3212const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3213const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3214const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3215const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3216const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3217
3218const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3219const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3220const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3221const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3222const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3223
3224const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3225const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3226
3227void SensorInputMapperTest::prepareAccelAxes() {
3228 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3229 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3230 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3231 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3232 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3233 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3234}
3235
3236void SensorInputMapperTest::prepareGyroAxes() {
3237 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3238 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3239 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3240 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3241 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3242 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3243}
3244
3245void SensorInputMapperTest::setAccelProperties() {
3246 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3247 /* sensorDataIndex */ 0);
3248 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3249 /* sensorDataIndex */ 1);
3250 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3251 /* sensorDataIndex */ 2);
3252 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3253 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3254 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3255 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3256 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3257}
3258
3259void SensorInputMapperTest::setGyroProperties() {
3260 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3261 /* sensorDataIndex */ 0);
3262 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3263 /* sensorDataIndex */ 1);
3264 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3265 /* sensorDataIndex */ 2);
3266 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3267 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3268 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3269 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3270 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3271}
3272
3273TEST_F(SensorInputMapperTest, GetSources) {
3274 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3275
3276 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3277}
3278
3279TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3280 setAccelProperties();
3281 prepareAccelAxes();
3282 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3283
3284 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3285 std::chrono::microseconds(10000),
3286 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003287 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003293
3294 NotifySensorArgs args;
3295 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3296 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3297 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3298
3299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3300 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3301 ASSERT_EQ(args.deviceId, DEVICE_ID);
3302 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3303 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3304 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3305 ASSERT_EQ(args.values, values);
3306 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3307}
3308
3309TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3310 setGyroProperties();
3311 prepareGyroAxes();
3312 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3313
3314 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3315 std::chrono::microseconds(10000),
3316 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003317 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3320 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003323
3324 NotifySensorArgs args;
3325 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3326 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3327 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3328
3329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3330 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3331 ASSERT_EQ(args.deviceId, DEVICE_ID);
3332 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3333 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3334 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3335 ASSERT_EQ(args.values, values);
3336 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3337}
3338
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339// --- KeyboardInputMapperTest ---
3340
3341class KeyboardInputMapperTest : public InputMapperTest {
3342protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003343 const std::string UNIQUE_ID = "local:0";
3344
3345 void prepareDisplay(int32_t orientation);
3346
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003347 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003348 int32_t originalKeyCode, int32_t rotatedKeyCode,
3349 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350};
3351
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003352/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3353 * orientation.
3354 */
3355void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003356 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3357 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003358}
3359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003361 int32_t originalScanCode, int32_t originalKeyCode,
3362 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363 NotifyKeyArgs args;
3364
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3367 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3368 ASSERT_EQ(originalScanCode, args.scanCode);
3369 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003370 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3374 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3375 ASSERT_EQ(originalScanCode, args.scanCode);
3376 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003377 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378}
3379
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003381 KeyboardInputMapper& mapper =
3382 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3383 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003385 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386}
3387
3388TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3389 const int32_t USAGE_A = 0x070004;
3390 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003391 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3392 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003393 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3394 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3395 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003397 KeyboardInputMapper& mapper =
3398 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3399 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003400 // Initial metastate is AMETA_NONE.
3401 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402
3403 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405 NotifyKeyArgs args;
3406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3407 ASSERT_EQ(DEVICE_ID, args.deviceId);
3408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3409 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3410 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3411 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3412 ASSERT_EQ(KEY_HOME, args.scanCode);
3413 ASSERT_EQ(AMETA_NONE, args.metaState);
3414 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3415 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3416 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3417
3418 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003419 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3421 ASSERT_EQ(DEVICE_ID, args.deviceId);
3422 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3423 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3424 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3425 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3426 ASSERT_EQ(KEY_HOME, args.scanCode);
3427 ASSERT_EQ(AMETA_NONE, args.metaState);
3428 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3429 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3430 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3431
3432 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3436 ASSERT_EQ(DEVICE_ID, args.deviceId);
3437 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3438 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3439 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3440 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3441 ASSERT_EQ(0, args.scanCode);
3442 ASSERT_EQ(AMETA_NONE, args.metaState);
3443 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3444 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3445 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3446
3447 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3449 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3451 ASSERT_EQ(DEVICE_ID, args.deviceId);
3452 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3453 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3454 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3455 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3456 ASSERT_EQ(0, args.scanCode);
3457 ASSERT_EQ(AMETA_NONE, args.metaState);
3458 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3459 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3460 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3461
3462 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3466 ASSERT_EQ(DEVICE_ID, args.deviceId);
3467 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3468 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3469 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3470 ASSERT_EQ(0, args.keyCode);
3471 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3472 ASSERT_EQ(AMETA_NONE, args.metaState);
3473 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3474 ASSERT_EQ(0U, args.policyFlags);
3475 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3476
3477 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3479 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3481 ASSERT_EQ(DEVICE_ID, args.deviceId);
3482 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3483 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3484 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3485 ASSERT_EQ(0, args.keyCode);
3486 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3487 ASSERT_EQ(AMETA_NONE, args.metaState);
3488 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3489 ASSERT_EQ(0U, args.policyFlags);
3490 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3491}
3492
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003493/**
3494 * Ensure that the readTime is set to the time when the EV_KEY is received.
3495 */
3496TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3497 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3498
3499 KeyboardInputMapper& mapper =
3500 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3501 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3502 NotifyKeyArgs args;
3503
3504 // Key down
3505 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3507 ASSERT_EQ(12, args.readTime);
3508
3509 // Key up
3510 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3512 ASSERT_EQ(15, args.readTime);
3513}
3514
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003516 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3517 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003518 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3519 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3520 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003522 KeyboardInputMapper& mapper =
3523 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3524 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525
Arthur Hung95f68612022-04-07 14:08:22 +08003526 // Initial metastate is AMETA_NONE.
3527 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528
3529 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 NotifyKeyArgs args;
3532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3533 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003534 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003535 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536
3537 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003538 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3540 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003541 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542
3543 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003544 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3546 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003547 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548
3549 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003550 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3552 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003553 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003554 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555}
3556
3557TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3560 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003563 KeyboardInputMapper& mapper =
3564 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3565 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003567 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3569 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3570 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3571 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3572 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3573 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3575 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3576}
3577
3578TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3580 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3581 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3582 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003583
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003585 KeyboardInputMapper& mapper =
3586 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3587 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003589 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003590 ASSERT_NO_FATAL_FAILURE(
3591 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3593 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3595 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3597 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003599 clearViewports();
3600 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003601 ASSERT_NO_FATAL_FAILURE(
3602 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3603 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3604 AKEYCODE_DPAD_UP, DISPLAY_ID));
3605 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3606 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3608 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003610 clearViewports();
3611 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003612 ASSERT_NO_FATAL_FAILURE(
3613 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3615 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3616 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3617 AKEYCODE_DPAD_UP, DISPLAY_ID));
3618 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3619 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003621 clearViewports();
3622 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003623 ASSERT_NO_FATAL_FAILURE(
3624 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3626 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3627 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3628 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3629 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3630 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003631
3632 // Special case: if orientation changes while key is down, we still emit the same keycode
3633 // in the key up as we did in the key down.
3634 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003635 clearViewports();
3636 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3639 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3640 ASSERT_EQ(KEY_UP, args.scanCode);
3641 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3642
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003643 clearViewports();
3644 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3647 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3648 ASSERT_EQ(KEY_UP, args.scanCode);
3649 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3650}
3651
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003652TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3653 // If the keyboard is not orientation aware,
3654 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003655 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003656
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003657 KeyboardInputMapper& mapper =
3658 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3659 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660 NotifyKeyArgs args;
3661
3662 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3667 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3668
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003669 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3674 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3675}
3676
3677TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3678 // If the keyboard is orientation aware,
3679 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003680 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003681
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003682 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003683 KeyboardInputMapper& mapper =
3684 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3685 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003686 NotifyKeyArgs args;
3687
3688 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3689 // ^--- already checked by the previous test
3690
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003691 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003692 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3697 ASSERT_EQ(DISPLAY_ID, args.displayId);
3698
3699 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003700 clearViewports();
3701 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003702 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3707 ASSERT_EQ(newDisplayId, args.displayId);
3708}
3709
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003711 KeyboardInputMapper& mapper =
3712 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3713 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003715 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003718 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003719 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720}
3721
Philip Junker4af3b3d2021-12-14 10:36:55 +01003722TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3723 KeyboardInputMapper& mapper =
3724 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3725 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3726
3727 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3728 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3729 << "If a mapping is available, the result is equal to the mapping";
3730
3731 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3732 << "If no mapping is available, the result is the key location";
3733}
3734
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003736 KeyboardInputMapper& mapper =
3737 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3738 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003740 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003741 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003743 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003744 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745}
3746
3747TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003748 KeyboardInputMapper& mapper =
3749 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3750 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003752 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753
3754 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3755 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003756 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003757 ASSERT_TRUE(flags[0]);
3758 ASSERT_FALSE(flags[1]);
3759}
3760
3761TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003762 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3763 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3764 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3765 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3766 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3767 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003769 KeyboardInputMapper& mapper =
3770 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3771 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003772 // Initial metastate is AMETA_NONE.
3773 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003774
3775 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003776 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3777 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3778 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003779
3780 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003783 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3784 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3785 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003786 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003787
3788 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003791 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3792 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3793 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003794 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795
3796 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003799 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3800 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003802 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803
3804 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003807 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3808 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3809 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003810 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811
3812 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003815 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3817 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003818 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819
3820 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003823 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003826 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827}
3828
Chris Yea52ade12020-08-27 16:49:20 -07003829TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3830 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3831 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3832 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3833 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3834
3835 KeyboardInputMapper& mapper =
3836 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3837 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3838
Chris Yea52ade12020-08-27 16:49:20 -07003839 // Meta state should be AMETA_NONE after reset
3840 mapper.reset(ARBITRARY_TIME);
3841 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3842 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3843 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3844 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3845
3846 NotifyKeyArgs args;
3847 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3850 ASSERT_EQ(AMETA_NONE, args.metaState);
3851 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3852 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3853 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3854
3855 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003856 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3858 ASSERT_EQ(AMETA_NONE, args.metaState);
3859 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3860 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3861 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3862}
3863
Arthur Hung2c9a3342019-07-23 14:18:59 +08003864TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3865 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003866 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3867 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3868 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3869 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003870
3871 // keyboard 2.
3872 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003873 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003874 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003875 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003876 std::shared_ptr<InputDevice> device2 =
3877 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003878 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003879
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003880 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3881 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3882 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3883 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 KeyboardInputMapper& mapper =
3886 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3887 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003888
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003889 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003890 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003891 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003892 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3893 device2->reset(ARBITRARY_TIME);
3894
3895 // Prepared displays and associated info.
3896 constexpr uint8_t hdmi1 = 0;
3897 constexpr uint8_t hdmi2 = 1;
3898 const std::string SECONDARY_UNIQUE_ID = "local:1";
3899
3900 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3901 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3902
3903 // No associated display viewport found, should disable the device.
3904 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3905 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3906 ASSERT_FALSE(device2->isEnabled());
3907
3908 // Prepare second display.
3909 constexpr int32_t newDisplayId = 2;
3910 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003911 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003912 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003913 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003914 // Default device will reconfigure above, need additional reconfiguration for another device.
3915 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3916 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3917
3918 // Device should be enabled after the associated display is found.
3919 ASSERT_TRUE(mDevice->isEnabled());
3920 ASSERT_TRUE(device2->isEnabled());
3921
3922 // Test pad key events
3923 ASSERT_NO_FATAL_FAILURE(
3924 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3925 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3926 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3927 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3928 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3929 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3930 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3931
3932 ASSERT_NO_FATAL_FAILURE(
3933 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3934 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3935 AKEYCODE_DPAD_RIGHT, newDisplayId));
3936 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3937 AKEYCODE_DPAD_DOWN, newDisplayId));
3938 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3939 AKEYCODE_DPAD_LEFT, newDisplayId));
3940}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003941
arthurhungc903df12020-08-11 15:08:42 +08003942TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3943 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3944 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3945 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3946 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3947 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3948 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3949
3950 KeyboardInputMapper& mapper =
3951 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3952 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003953 // Initial metastate is AMETA_NONE.
3954 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003955
3956 // Initialization should have turned all of the lights off.
3957 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3958 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3959 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3960
3961 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003964 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3965 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3966
3967 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003970 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3971 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3972
3973 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003976 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3977 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3978
3979 mFakeEventHub->removeDevice(EVENTHUB_ID);
3980 mReader->loopOnce();
3981
3982 // keyboard 2 should default toggle keys.
3983 const std::string USB2 = "USB2";
3984 const std::string DEVICE_NAME2 = "KEYBOARD2";
3985 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3986 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3987 std::shared_ptr<InputDevice> device2 =
3988 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003989 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003990 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3991 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3992 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3993 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3994 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3995 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3996
arthurhung6fe95782020-10-05 22:41:16 +08003997 KeyboardInputMapper& mapper2 =
3998 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3999 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004000 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4001 device2->reset(ARBITRARY_TIME);
4002
4003 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4004 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4005 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004006 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4007 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004008}
4009
Arthur Hungcb40a002021-08-03 14:31:01 +00004010TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4011 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4012 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4013 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4014
4015 // Suppose we have two mappers. (DPAD + KEYBOARD)
4016 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4017 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4018 KeyboardInputMapper& mapper =
4019 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4020 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004021 // Initial metastate is AMETA_NONE.
4022 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004023
4024 mReader->toggleCapsLockState(DEVICE_ID);
4025 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4026}
4027
Arthur Hungfb3cc112022-04-13 07:39:50 +00004028TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4029 // keyboard 1.
4030 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4031 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4032 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4033 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4034 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4035 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4036
4037 KeyboardInputMapper& mapper1 =
4038 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4039 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4040
4041 // keyboard 2.
4042 const std::string USB2 = "USB2";
4043 const std::string DEVICE_NAME2 = "KEYBOARD2";
4044 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4045 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4046 std::shared_ptr<InputDevice> device2 =
4047 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4048 ftl::Flags<InputDeviceClass>(0));
4049 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4050 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4051 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4052 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4053 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4054 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4055
4056 KeyboardInputMapper& mapper2 =
4057 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4058 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4059 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4060 device2->reset(ARBITRARY_TIME);
4061
Arthur Hung95f68612022-04-07 14:08:22 +08004062 // Initial metastate is AMETA_NONE.
4063 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4064 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4065
4066 // Toggle num lock on and off.
4067 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004069 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4070 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4071 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4072
4073 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4075 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4076 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4077 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4078
4079 // Toggle caps lock on and off.
4080 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4082 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4083 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4084 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4085
4086 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4087 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4088 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4089 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4090 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4091
4092 // Toggle scroll lock on and off.
4093 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4094 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4095 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4096 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4097 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4098
4099 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4100 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4101 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4102 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4103 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4104}
4105
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004106// --- KeyboardInputMapperTest_ExternalDevice ---
4107
4108class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4109protected:
Chris Yea52ade12020-08-27 16:49:20 -07004110 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004111};
4112
4113TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004114 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4115 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004116
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004117 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4118 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4119 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4120 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004121
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004122 KeyboardInputMapper& mapper =
4123 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4124 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004125
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004127 NotifyKeyArgs args;
4128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4129 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4130
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004131 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4133 ASSERT_EQ(uint32_t(0), args.policyFlags);
4134
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4137 ASSERT_EQ(uint32_t(0), args.policyFlags);
4138
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4141 ASSERT_EQ(uint32_t(0), args.policyFlags);
4142
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4145 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4146
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004147 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4149 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4150}
4151
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004152TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004153 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004154
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004155 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4156 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4157 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004158
Powei Fengd041c5d2019-05-03 17:11:33 -07004159 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004160 KeyboardInputMapper& mapper =
4161 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4162 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004163
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004165 NotifyKeyArgs args;
4166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4168
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004169 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4171 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4172
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4175 ASSERT_EQ(uint32_t(0), args.policyFlags);
4176
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004177 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4179 ASSERT_EQ(uint32_t(0), args.policyFlags);
4180
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4183 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4184
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004185 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4187 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4188}
4189
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190// --- CursorInputMapperTest ---
4191
4192class CursorInputMapperTest : public InputMapperTest {
4193protected:
4194 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4195
Michael Wright17db18e2020-06-26 20:51:44 +01004196 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197
Chris Yea52ade12020-08-27 16:49:20 -07004198 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199 InputMapperTest::SetUp();
4200
Michael Wright17db18e2020-06-26 20:51:44 +01004201 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004202 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 }
4204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004205 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4206 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004207
4208 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004209 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4210 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4211 }
4212
4213 void prepareSecondaryDisplay() {
4214 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4215 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4216 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004217 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004218
4219 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4220 float pressure) {
4221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4222 0.0f, 0.0f, 0.0f, EPSILON));
4223 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224};
4225
4226const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004228void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4229 int32_t originalY, int32_t rotatedX,
4230 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 NotifyMotionArgs args;
4232
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004238 ASSERT_NO_FATAL_FAILURE(
4239 assertCursorPointerCoords(args.pointerCoords[0],
4240 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4241 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242}
4243
4244TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004246 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004248 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249}
4250
4251TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004253 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004255 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256}
4257
4258TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004260 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261
4262 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004263 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264
4265 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004266 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4267 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4269 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4270
4271 // When the bounds are set, then there should be a valid motion range.
4272 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4273
4274 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004275 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276
4277 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4278 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4279 1, 800 - 1, 0.0f, 0.0f));
4280 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4281 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4282 2, 480 - 1, 0.0f, 0.0f));
4283 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4284 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4285 0.0f, 1.0f, 0.0f, 0.0f));
4286}
4287
4288TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004290 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291
4292 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004293 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294
4295 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4296 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4297 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4298 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4299 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4300 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4301 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4302 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4303 0.0f, 1.0f, 0.0f, 0.0f));
4304}
4305
4306TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004308 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
arthurhungdcef2dc2020-08-11 14:47:50 +08004310 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311
4312 NotifyMotionArgs args;
4313
4314 // Button press.
4315 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4319 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4320 ASSERT_EQ(DEVICE_ID, args.deviceId);
4321 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4322 ASSERT_EQ(uint32_t(0), args.policyFlags);
4323 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4324 ASSERT_EQ(0, args.flags);
4325 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4326 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4327 ASSERT_EQ(0, args.edgeFlags);
4328 ASSERT_EQ(uint32_t(1), args.pointerCount);
4329 ASSERT_EQ(0, args.pointerProperties[0].id);
4330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004331 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4333 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4334 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4335
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4337 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4338 ASSERT_EQ(DEVICE_ID, args.deviceId);
4339 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4340 ASSERT_EQ(uint32_t(0), args.policyFlags);
4341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4342 ASSERT_EQ(0, args.flags);
4343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4344 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4345 ASSERT_EQ(0, args.edgeFlags);
4346 ASSERT_EQ(uint32_t(1), args.pointerCount);
4347 ASSERT_EQ(0, args.pointerProperties[0].id);
4348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004349 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4351 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4352 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4353
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004355 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4356 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4358 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4359 ASSERT_EQ(DEVICE_ID, args.deviceId);
4360 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4361 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4363 ASSERT_EQ(0, args.flags);
4364 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4365 ASSERT_EQ(0, args.buttonState);
4366 ASSERT_EQ(0, args.edgeFlags);
4367 ASSERT_EQ(uint32_t(1), args.pointerCount);
4368 ASSERT_EQ(0, args.pointerProperties[0].id);
4369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004370 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004371 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4372 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4373 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4374
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4376 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4377 ASSERT_EQ(DEVICE_ID, args.deviceId);
4378 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4379 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4381 ASSERT_EQ(0, args.flags);
4382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4383 ASSERT_EQ(0, args.buttonState);
4384 ASSERT_EQ(0, args.edgeFlags);
4385 ASSERT_EQ(uint32_t(1), args.pointerCount);
4386 ASSERT_EQ(0, args.pointerProperties[0].id);
4387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004388 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4390 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4391 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4392}
4393
4394TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004396 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397
4398 NotifyMotionArgs args;
4399
4400 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004405 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4406 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4407 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408
4409 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004414 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4415 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416}
4417
4418TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004420 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421
4422 NotifyMotionArgs args;
4423
4424 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4428 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004429 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004433 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004434
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004440 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441
4442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004444 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445}
4446
4447TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004449 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450
4451 NotifyMotionArgs args;
4452
4453 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004460 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4461 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4462 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004466 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4467 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4468 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004469
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004476 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4477 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4478 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479
4480 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004486
4487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004489 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490}
4491
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004492TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004493 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004495 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4496 // need to be rotated.
4497 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004498 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004500 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4509}
4510
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004511TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004512 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004514 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4515 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004516 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004518 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004519 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4528
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004529 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004530 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4537 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004540 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004541 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4543 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4544 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4545 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4546 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4547 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4548 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4550
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004551 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004552 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004553 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4554 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4555 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4556 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4557 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4558 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4559 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561}
4562
4563TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004565 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566
4567 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4568 mFakePointerController->setPosition(100, 200);
4569 mFakePointerController->setButtonState(0);
4570
4571 NotifyMotionArgs motionArgs;
4572 NotifyKeyArgs keyArgs;
4573
4574 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4578 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4579 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4580 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004581 ASSERT_NO_FATAL_FAILURE(
4582 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4586 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4587 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004588 ASSERT_NO_FATAL_FAILURE(
4589 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004590
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595 ASSERT_EQ(0, motionArgs.buttonState);
4596 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004597 ASSERT_NO_FATAL_FAILURE(
4598 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599
4600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004601 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 ASSERT_EQ(0, motionArgs.buttonState);
4603 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004604 ASSERT_NO_FATAL_FAILURE(
4605 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004606
4607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004609 ASSERT_EQ(0, motionArgs.buttonState);
4610 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004611 ASSERT_NO_FATAL_FAILURE(
4612 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613
4614 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4619 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4620 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4621 motionArgs.buttonState);
4622 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4623 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004624 ASSERT_NO_FATAL_FAILURE(
4625 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4628 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4629 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4630 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4631 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004632 ASSERT_NO_FATAL_FAILURE(
4633 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004634
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4637 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4638 motionArgs.buttonState);
4639 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4640 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004641 ASSERT_NO_FATAL_FAILURE(
4642 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004643
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4649 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004650 ASSERT_NO_FATAL_FAILURE(
4651 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4656 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004657 ASSERT_NO_FATAL_FAILURE(
4658 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4664 ASSERT_EQ(0, motionArgs.buttonState);
4665 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004666 ASSERT_NO_FATAL_FAILURE(
4667 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004670
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 ASSERT_EQ(0, motionArgs.buttonState);
4673 ASSERT_EQ(0, mFakePointerController->getButtonState());
4674 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004675 ASSERT_NO_FATAL_FAILURE(
4676 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004677
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4679 ASSERT_EQ(0, motionArgs.buttonState);
4680 ASSERT_EQ(0, mFakePointerController->getButtonState());
4681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004682 ASSERT_NO_FATAL_FAILURE(
4683 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684
4685 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4689 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4690 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004691
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4695 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004696 ASSERT_NO_FATAL_FAILURE(
4697 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004698
4699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4700 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4701 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4702 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004703 ASSERT_NO_FATAL_FAILURE(
4704 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 ASSERT_EQ(0, motionArgs.buttonState);
4711 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004712 ASSERT_NO_FATAL_FAILURE(
4713 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004714
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004717 ASSERT_EQ(0, motionArgs.buttonState);
4718 ASSERT_EQ(0, mFakePointerController->getButtonState());
4719
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004720 ASSERT_NO_FATAL_FAILURE(
4721 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4723 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4724 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4725
4726 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4730 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4731 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004732
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4736 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004737 ASSERT_NO_FATAL_FAILURE(
4738 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004739
4740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4741 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4742 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4743 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004744 ASSERT_NO_FATAL_FAILURE(
4745 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004746
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751 ASSERT_EQ(0, motionArgs.buttonState);
4752 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004753 ASSERT_NO_FATAL_FAILURE(
4754 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004755
4756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4758 ASSERT_EQ(0, motionArgs.buttonState);
4759 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004760 ASSERT_NO_FATAL_FAILURE(
4761 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004762
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4764 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4765 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4766
4767 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4771 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4772 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004773
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4777 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004778 ASSERT_NO_FATAL_FAILURE(
4779 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004780
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4782 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4783 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4784 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004785 ASSERT_NO_FATAL_FAILURE(
4786 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004791 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792 ASSERT_EQ(0, motionArgs.buttonState);
4793 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004794 ASSERT_NO_FATAL_FAILURE(
4795 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004796
4797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4798 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4799 ASSERT_EQ(0, motionArgs.buttonState);
4800 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004801 ASSERT_NO_FATAL_FAILURE(
4802 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004803
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4805 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4806 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4807
4808 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4812 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4813 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004816 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4818 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004819 ASSERT_NO_FATAL_FAILURE(
4820 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004821
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4824 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4825 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004826 ASSERT_NO_FATAL_FAILURE(
4827 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004832 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_EQ(0, motionArgs.buttonState);
4834 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004835 ASSERT_NO_FATAL_FAILURE(
4836 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004837
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4840 ASSERT_EQ(0, motionArgs.buttonState);
4841 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004842 ASSERT_NO_FATAL_FAILURE(
4843 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004844
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4846 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4847 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4848}
4849
4850TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004852 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853
4854 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4855 mFakePointerController->setPosition(100, 200);
4856 mFakePointerController->setButtonState(0);
4857
4858 NotifyMotionArgs args;
4859
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004864 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4867 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 +01004868 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004869}
4870
4871TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004872 addConfigurationProperty("cursor.mode", "pointer");
4873 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004874 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004875
4876 NotifyDeviceResetArgs resetArgs;
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4878 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4879 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4880
4881 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4882 mFakePointerController->setPosition(100, 200);
4883 mFakePointerController->setButtonState(0);
4884
4885 NotifyMotionArgs args;
4886
4887 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4892 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4895 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 +01004896 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004897
4898 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4902 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4903 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4905 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4907 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4908 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, 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
4912 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004913 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4914 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4916 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4917 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4919 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4921 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4922 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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
4926 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4928 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4931 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4934 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 +01004935 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004936
4937 // Disable pointer capture and check that the device generation got bumped
4938 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004939 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004940 mFakePolicy->setPointerCapture(false);
4941 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004942 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004943
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004945 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4946
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4951 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4954 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 +01004955 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956}
4957
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004958/**
4959 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4960 * pointer acceleration or speed processing should not be applied.
4961 */
4962TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4963 addConfigurationProperty("cursor.mode", "pointer");
4964 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4965 100.f /*high threshold*/, 10.f /*acceleration*/);
4966 mFakePolicy->setVelocityControlParams(testParams);
4967 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4968
4969 NotifyDeviceResetArgs resetArgs;
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4971 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4972 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4973
4974 NotifyMotionArgs args;
4975
4976 // Move and verify scale is applied.
4977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4981 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4982 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4983 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4984 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4985 ASSERT_GT(relX, 10);
4986 ASSERT_GT(relY, 20);
4987
4988 // Enable Pointer Capture
4989 mFakePolicy->setPointerCapture(true);
4990 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4991 NotifyPointerCaptureChangedArgs captureArgs;
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4993 ASSERT_TRUE(captureArgs.request.enable);
4994
4995 // Move and verify scale is not applied.
4996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5000 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5002 ASSERT_EQ(10, args.pointerCoords[0].getX());
5003 ASSERT_EQ(20, args.pointerCoords[0].getY());
5004}
5005
Prabir Pradhan258e2b92022-06-24 18:37:04 +00005006TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5007 addConfigurationProperty("cursor.mode", "pointer");
5008 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5009
5010 NotifyDeviceResetArgs resetArgs;
5011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5012 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5013 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5014
5015 // Ensure the display is rotated.
5016 prepareDisplay(DISPLAY_ORIENTATION_90);
5017
5018 NotifyMotionArgs args;
5019
5020 // Verify that the coordinates are rotated.
5021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5025 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5026 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5027 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5028 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5029
5030 // Enable Pointer Capture.
5031 mFakePolicy->setPointerCapture(true);
5032 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5033 NotifyPointerCaptureChangedArgs captureArgs;
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5035 ASSERT_TRUE(captureArgs.request.enable);
5036
5037 // Move and verify rotation is not applied.
5038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5042 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5044 ASSERT_EQ(10, args.pointerCoords[0].getX());
5045 ASSERT_EQ(20, args.pointerCoords[0].getY());
5046}
5047
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005048TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005049 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005050
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005051 // Set up the default display.
5052 prepareDisplay(DISPLAY_ORIENTATION_90);
5053
5054 // Set up the secondary display as the display on which the pointer should be shown.
5055 // The InputDevice is not associated with any display.
5056 prepareSecondaryDisplay();
5057 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005058 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5059
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005060 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005061 mFakePointerController->setPosition(100, 200);
5062 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005063
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005064 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5069 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5070 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005071 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005072}
5073
5074TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5075 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5076
5077 // Set up the default display.
5078 prepareDisplay(DISPLAY_ORIENTATION_90);
5079
5080 // Set up the secondary display as the display on which the pointer should be shown,
5081 // and associate the InputDevice with the secondary display.
5082 prepareSecondaryDisplay();
5083 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5084 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5085 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5086
5087 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5088 mFakePointerController->setPosition(100, 200);
5089 mFakePointerController->setButtonState(0);
5090
5091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5095 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5096 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
5097 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5098}
5099
5100TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5101 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5102
5103 // Set up the default display as the display on which the pointer should be shown.
5104 prepareDisplay(DISPLAY_ORIENTATION_90);
5105 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5106
5107 // Associate the InputDevice with the secondary display.
5108 prepareSecondaryDisplay();
5109 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5110 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5111
5112 // The mapper should not generate any events because it is associated with a display that is
5113 // different from the pointer display.
5114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005118}
5119
Michael Wrightd02c5b62014-02-10 15:10:22 -08005120// --- TouchInputMapperTest ---
5121
5122class TouchInputMapperTest : public InputMapperTest {
5123protected:
5124 static const int32_t RAW_X_MIN;
5125 static const int32_t RAW_X_MAX;
5126 static const int32_t RAW_Y_MIN;
5127 static const int32_t RAW_Y_MAX;
5128 static const int32_t RAW_TOUCH_MIN;
5129 static const int32_t RAW_TOUCH_MAX;
5130 static const int32_t RAW_TOOL_MIN;
5131 static const int32_t RAW_TOOL_MAX;
5132 static const int32_t RAW_PRESSURE_MIN;
5133 static const int32_t RAW_PRESSURE_MAX;
5134 static const int32_t RAW_ORIENTATION_MIN;
5135 static const int32_t RAW_ORIENTATION_MAX;
5136 static const int32_t RAW_DISTANCE_MIN;
5137 static const int32_t RAW_DISTANCE_MAX;
5138 static const int32_t RAW_TILT_MIN;
5139 static const int32_t RAW_TILT_MAX;
5140 static const int32_t RAW_ID_MIN;
5141 static const int32_t RAW_ID_MAX;
5142 static const int32_t RAW_SLOT_MIN;
5143 static const int32_t RAW_SLOT_MAX;
5144 static const float X_PRECISION;
5145 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005146 static const float X_PRECISION_VIRTUAL;
5147 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148
5149 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005150 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151
5152 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5153
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005154 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005155 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005156
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157 enum Axes {
5158 POSITION = 1 << 0,
5159 TOUCH = 1 << 1,
5160 TOOL = 1 << 2,
5161 PRESSURE = 1 << 3,
5162 ORIENTATION = 1 << 4,
5163 MINOR = 1 << 5,
5164 ID = 1 << 6,
5165 DISTANCE = 1 << 7,
5166 TILT = 1 << 8,
5167 SLOT = 1 << 9,
5168 TOOL_TYPE = 1 << 10,
5169 };
5170
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005171 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5172 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005173 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005175 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176 int32_t toRawX(float displayX);
5177 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005178 int32_t toRotatedRawX(float displayX);
5179 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005180 float toCookedX(float rawX, float rawY);
5181 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005183 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005185 float toDisplayY(int32_t rawY, int32_t displayHeight);
5186
Michael Wrightd02c5b62014-02-10 15:10:22 -08005187};
5188
5189const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5190const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5191const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5192const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5193const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5194const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5195const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5196const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005197const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5198const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5200const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5201const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5202const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5203const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5204const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5205const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5206const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5207const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5208const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5209const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5210const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005211const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5212 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5213const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5214 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005215const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5216 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217
5218const float TouchInputMapperTest::GEOMETRIC_SCALE =
5219 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5220 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5221
5222const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5223 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5224 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5225};
5226
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005227void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005228 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5229 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005230}
5231
5232void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5233 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5234 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235}
5236
Santos Cordonfa5cf462017-04-05 10:37:00 -07005237void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005238 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5239 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5240 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005241}
5242
Michael Wrightd02c5b62014-02-10 15:10:22 -08005243void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005244 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5245 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5246 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5247 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248}
5249
Jason Gerecke489fda82012-09-07 17:19:40 -07005250void TouchInputMapperTest::prepareLocationCalibration() {
5251 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5252}
5253
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254int32_t TouchInputMapperTest::toRawX(float displayX) {
5255 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5256}
5257
5258int32_t TouchInputMapperTest::toRawY(float displayY) {
5259 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5260}
5261
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005262int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5263 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5264}
5265
5266int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5267 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5268}
5269
Jason Gerecke489fda82012-09-07 17:19:40 -07005270float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5271 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5272 return rawX;
5273}
5274
5275float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5276 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5277 return rawY;
5278}
5279
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005281 return toDisplayX(rawX, DISPLAY_WIDTH);
5282}
5283
5284float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5285 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286}
5287
5288float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005289 return toDisplayY(rawY, DISPLAY_HEIGHT);
5290}
5291
5292float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5293 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294}
5295
5296
5297// --- SingleTouchInputMapperTest ---
5298
5299class SingleTouchInputMapperTest : public TouchInputMapperTest {
5300protected:
5301 void prepareButtons();
5302 void prepareAxes(int axes);
5303
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005304 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5305 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5306 void processUp(SingleTouchInputMapper& mappery);
5307 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5308 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5309 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5310 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5311 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5312 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313};
5314
5315void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005316 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317}
5318
5319void SingleTouchInputMapperTest::prepareAxes(int axes) {
5320 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005321 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5322 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323 }
5324 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005325 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5326 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327 }
5328 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005329 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5330 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331 }
5332 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005333 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5334 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335 }
5336 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005337 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5338 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339 }
5340}
5341
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005342void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346}
5347
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005348void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005349 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::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355}
5356
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005357void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359}
5360
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005361void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5362 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364}
5365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368}
5369
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005370void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5371 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374}
5375
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005376void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5377 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379}
5380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383}
5384
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386 prepareButtons();
5387 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005388 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005390 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391}
5392
5393TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005394 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5395 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396 prepareButtons();
5397 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005398 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005400 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401}
5402
5403TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 prepareButtons();
5405 prepareAxes(POSITION);
5406 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005407 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005409 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410}
5411
5412TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 prepareButtons();
5414 prepareAxes(POSITION);
5415 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005416 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005418 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419}
5420
5421TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 addConfigurationProperty("touch.deviceType", "touchScreen");
5423 prepareDisplay(DISPLAY_ORIENTATION_0);
5424 prepareButtons();
5425 prepareAxes(POSITION);
5426 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428
5429 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005430 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431
5432 // Virtual key is down.
5433 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5434 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5435 processDown(mapper, x, y);
5436 processSync(mapper);
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5438
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005439 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440
5441 // Virtual key is up.
5442 processUp(mapper);
5443 processSync(mapper);
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5445
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005446 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447}
5448
5449TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005450 addConfigurationProperty("touch.deviceType", "touchScreen");
5451 prepareDisplay(DISPLAY_ORIENTATION_0);
5452 prepareButtons();
5453 prepareAxes(POSITION);
5454 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005455 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456
5457 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005458 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459
5460 // Virtual key is down.
5461 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5462 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5463 processDown(mapper, x, y);
5464 processSync(mapper);
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5466
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005467 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468
5469 // Virtual key is up.
5470 processUp(mapper);
5471 processSync(mapper);
5472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005474 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475}
5476
5477TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478 addConfigurationProperty("touch.deviceType", "touchScreen");
5479 prepareDisplay(DISPLAY_ORIENTATION_0);
5480 prepareButtons();
5481 prepareAxes(POSITION);
5482 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005483 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484
5485 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5486 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005487 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005488 ASSERT_TRUE(flags[0]);
5489 ASSERT_FALSE(flags[1]);
5490}
5491
5492TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493 addConfigurationProperty("touch.deviceType", "touchScreen");
5494 prepareDisplay(DISPLAY_ORIENTATION_0);
5495 prepareButtons();
5496 prepareAxes(POSITION);
5497 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005498 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499
arthurhungdcef2dc2020-08-11 14:47:50 +08005500 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501
5502 NotifyKeyArgs args;
5503
5504 // Press virtual key.
5505 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5506 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5507 processDown(mapper, x, y);
5508 processSync(mapper);
5509
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5511 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5512 ASSERT_EQ(DEVICE_ID, args.deviceId);
5513 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5514 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5515 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5516 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5517 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5518 ASSERT_EQ(KEY_HOME, args.scanCode);
5519 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5520 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5521
5522 // Release virtual key.
5523 processUp(mapper);
5524 processSync(mapper);
5525
5526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5527 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5528 ASSERT_EQ(DEVICE_ID, args.deviceId);
5529 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5530 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5531 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5532 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5533 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5534 ASSERT_EQ(KEY_HOME, args.scanCode);
5535 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5536 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5537
5538 // Should not have sent any motions.
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5540}
5541
5542TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543 addConfigurationProperty("touch.deviceType", "touchScreen");
5544 prepareDisplay(DISPLAY_ORIENTATION_0);
5545 prepareButtons();
5546 prepareAxes(POSITION);
5547 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005548 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549
arthurhungdcef2dc2020-08-11 14:47:50 +08005550 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551
5552 NotifyKeyArgs keyArgs;
5553
5554 // Press virtual key.
5555 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5556 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5557 processDown(mapper, x, y);
5558 processSync(mapper);
5559
5560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5561 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5562 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5563 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5564 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5565 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5566 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5567 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5568 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5569 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5570 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5571
5572 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5573 // into the display area.
5574 y -= 100;
5575 processMove(mapper, x, y);
5576 processSync(mapper);
5577
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5579 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5580 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5581 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5582 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5583 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5584 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5585 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5586 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5587 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5589 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5590
5591 NotifyMotionArgs motionArgs;
5592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5593 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5594 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5595 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5596 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5597 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5598 ASSERT_EQ(0, motionArgs.flags);
5599 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5600 ASSERT_EQ(0, motionArgs.buttonState);
5601 ASSERT_EQ(0, motionArgs.edgeFlags);
5602 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5603 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5606 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5607 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5608 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5609 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5610
5611 // Keep moving out of bounds. Should generate a pointer move.
5612 y -= 50;
5613 processMove(mapper, x, y);
5614 processSync(mapper);
5615
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5618 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5619 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5620 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5622 ASSERT_EQ(0, motionArgs.flags);
5623 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5624 ASSERT_EQ(0, motionArgs.buttonState);
5625 ASSERT_EQ(0, motionArgs.edgeFlags);
5626 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5630 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5631 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5632 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5633 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5634
5635 // Release out of bounds. Should generate a pointer up.
5636 processUp(mapper);
5637 processSync(mapper);
5638
5639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5640 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5641 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5642 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5643 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5645 ASSERT_EQ(0, motionArgs.flags);
5646 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5647 ASSERT_EQ(0, motionArgs.buttonState);
5648 ASSERT_EQ(0, motionArgs.edgeFlags);
5649 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5653 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5654 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5655 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5656 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5657
5658 // Should not have sent any more keys or motions.
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5661}
5662
5663TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664 addConfigurationProperty("touch.deviceType", "touchScreen");
5665 prepareDisplay(DISPLAY_ORIENTATION_0);
5666 prepareButtons();
5667 prepareAxes(POSITION);
5668 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005669 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005670
arthurhungdcef2dc2020-08-11 14:47:50 +08005671 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005672
5673 NotifyMotionArgs motionArgs;
5674
5675 // Initially go down out of bounds.
5676 int32_t x = -10;
5677 int32_t y = -10;
5678 processDown(mapper, x, y);
5679 processSync(mapper);
5680
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5682
5683 // Move into the display area. Should generate a pointer down.
5684 x = 50;
5685 y = 75;
5686 processMove(mapper, x, y);
5687 processSync(mapper);
5688
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5690 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5691 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5692 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5693 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5694 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5695 ASSERT_EQ(0, motionArgs.flags);
5696 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5697 ASSERT_EQ(0, motionArgs.buttonState);
5698 ASSERT_EQ(0, motionArgs.edgeFlags);
5699 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5700 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5703 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5704 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5705 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5706 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5707
5708 // Release. Should generate a pointer up.
5709 processUp(mapper);
5710 processSync(mapper);
5711
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5713 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5714 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5715 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5716 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5717 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5718 ASSERT_EQ(0, motionArgs.flags);
5719 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5720 ASSERT_EQ(0, motionArgs.buttonState);
5721 ASSERT_EQ(0, motionArgs.edgeFlags);
5722 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5723 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5726 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5727 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5728 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5729 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5730
5731 // Should not have sent any more keys or motions.
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5734}
5735
Santos Cordonfa5cf462017-04-05 10:37:00 -07005736TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005737 addConfigurationProperty("touch.deviceType", "touchScreen");
5738 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5739
5740 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5741 prepareButtons();
5742 prepareAxes(POSITION);
5743 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005744 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005745
arthurhungdcef2dc2020-08-11 14:47:50 +08005746 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005747
5748 NotifyMotionArgs motionArgs;
5749
5750 // Down.
5751 int32_t x = 100;
5752 int32_t y = 125;
5753 processDown(mapper, x, y);
5754 processSync(mapper);
5755
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5757 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5758 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5759 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5760 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5761 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5762 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5763 ASSERT_EQ(0, motionArgs.flags);
5764 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5765 ASSERT_EQ(0, motionArgs.buttonState);
5766 ASSERT_EQ(0, motionArgs.edgeFlags);
5767 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5768 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5771 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5772 1, 0, 0, 0, 0, 0, 0, 0));
5773 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5774 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5775 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5776
5777 // Move.
5778 x += 50;
5779 y += 75;
5780 processMove(mapper, x, y);
5781 processSync(mapper);
5782
5783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5784 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5785 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5786 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5787 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5788 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5790 ASSERT_EQ(0, motionArgs.flags);
5791 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5792 ASSERT_EQ(0, motionArgs.buttonState);
5793 ASSERT_EQ(0, motionArgs.edgeFlags);
5794 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5795 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5796 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5798 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5799 1, 0, 0, 0, 0, 0, 0, 0));
5800 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5801 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5802 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5803
5804 // Up.
5805 processUp(mapper);
5806 processSync(mapper);
5807
5808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5809 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5810 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5811 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5812 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5813 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5814 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5815 ASSERT_EQ(0, motionArgs.flags);
5816 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5817 ASSERT_EQ(0, motionArgs.buttonState);
5818 ASSERT_EQ(0, motionArgs.edgeFlags);
5819 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5820 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5823 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5824 1, 0, 0, 0, 0, 0, 0, 0));
5825 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5826 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5827 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5828
5829 // Should not have sent any more keys or motions.
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5832}
5833
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 addConfigurationProperty("touch.deviceType", "touchScreen");
5836 prepareDisplay(DISPLAY_ORIENTATION_0);
5837 prepareButtons();
5838 prepareAxes(POSITION);
5839 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005840 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005841
arthurhungdcef2dc2020-08-11 14:47:50 +08005842 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843
5844 NotifyMotionArgs motionArgs;
5845
5846 // Down.
5847 int32_t x = 100;
5848 int32_t y = 125;
5849 processDown(mapper, x, y);
5850 processSync(mapper);
5851
5852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5853 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5854 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5855 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5856 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5858 ASSERT_EQ(0, motionArgs.flags);
5859 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5860 ASSERT_EQ(0, motionArgs.buttonState);
5861 ASSERT_EQ(0, motionArgs.edgeFlags);
5862 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5863 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5866 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5867 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5868 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5869 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5870
5871 // Move.
5872 x += 50;
5873 y += 75;
5874 processMove(mapper, x, y);
5875 processSync(mapper);
5876
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5878 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5879 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5880 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5881 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5883 ASSERT_EQ(0, motionArgs.flags);
5884 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5885 ASSERT_EQ(0, motionArgs.buttonState);
5886 ASSERT_EQ(0, motionArgs.edgeFlags);
5887 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5888 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5891 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5892 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5893 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5894 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5895
5896 // Up.
5897 processUp(mapper);
5898 processSync(mapper);
5899
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5901 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5902 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5903 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5904 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5905 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5906 ASSERT_EQ(0, motionArgs.flags);
5907 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5908 ASSERT_EQ(0, motionArgs.buttonState);
5909 ASSERT_EQ(0, motionArgs.edgeFlags);
5910 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5911 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5914 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5915 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5916 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5917 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5918
5919 // Should not have sent any more keys or motions.
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5922}
5923
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005924TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005925 addConfigurationProperty("touch.deviceType", "touchScreen");
5926 prepareButtons();
5927 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005928 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5929 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005930 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005931
5932 NotifyMotionArgs args;
5933
5934 // Rotation 90.
5935 prepareDisplay(DISPLAY_ORIENTATION_90);
5936 processDown(mapper, toRawX(50), toRawY(75));
5937 processSync(mapper);
5938
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5940 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5941 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5942
5943 processUp(mapper);
5944 processSync(mapper);
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5946}
5947
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005948TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949 addConfigurationProperty("touch.deviceType", "touchScreen");
5950 prepareButtons();
5951 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005952 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5953 // orientation-aware are affected by display rotation.
5954 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005955 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956
5957 NotifyMotionArgs args;
5958
5959 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005960 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961 prepareDisplay(DISPLAY_ORIENTATION_0);
5962 processDown(mapper, toRawX(50), toRawY(75));
5963 processSync(mapper);
5964
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5966 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5967 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5968
5969 processUp(mapper);
5970 processSync(mapper);
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5972
5973 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005974 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005975 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005976 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977 processSync(mapper);
5978
5979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5980 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5981 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5982
5983 processUp(mapper);
5984 processSync(mapper);
5985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5986
5987 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005988 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989 prepareDisplay(DISPLAY_ORIENTATION_180);
5990 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5991 processSync(mapper);
5992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5994 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5995 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5996
5997 processUp(mapper);
5998 processSync(mapper);
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6000
6001 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006002 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006004 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 processSync(mapper);
6006
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6008 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6009 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6010
6011 processUp(mapper);
6012 processSync(mapper);
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6014}
6015
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006016TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6017 addConfigurationProperty("touch.deviceType", "touchScreen");
6018 prepareButtons();
6019 prepareAxes(POSITION);
6020 addConfigurationProperty("touch.orientationAware", "1");
6021 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6022 clearViewports();
6023 prepareDisplay(DISPLAY_ORIENTATION_0);
6024 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6025 NotifyMotionArgs args;
6026
6027 // Orientation 0.
6028 processDown(mapper, toRawX(50), toRawY(75));
6029 processSync(mapper);
6030
6031 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6032 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6033 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6034
6035 processUp(mapper);
6036 processSync(mapper);
6037 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6038}
6039
6040TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6041 addConfigurationProperty("touch.deviceType", "touchScreen");
6042 prepareButtons();
6043 prepareAxes(POSITION);
6044 addConfigurationProperty("touch.orientationAware", "1");
6045 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6046 clearViewports();
6047 prepareDisplay(DISPLAY_ORIENTATION_0);
6048 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6049 NotifyMotionArgs args;
6050
6051 // Orientation 90.
6052 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6053 processSync(mapper);
6054
6055 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6056 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6057 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6058
6059 processUp(mapper);
6060 processSync(mapper);
6061 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6062}
6063
6064TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6065 addConfigurationProperty("touch.deviceType", "touchScreen");
6066 prepareButtons();
6067 prepareAxes(POSITION);
6068 addConfigurationProperty("touch.orientationAware", "1");
6069 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6070 clearViewports();
6071 prepareDisplay(DISPLAY_ORIENTATION_0);
6072 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6073 NotifyMotionArgs args;
6074
6075 // Orientation 180.
6076 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6077 processSync(mapper);
6078
6079 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6080 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6081 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6082
6083 processUp(mapper);
6084 processSync(mapper);
6085 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6086}
6087
6088TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6089 addConfigurationProperty("touch.deviceType", "touchScreen");
6090 prepareButtons();
6091 prepareAxes(POSITION);
6092 addConfigurationProperty("touch.orientationAware", "1");
6093 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6094 clearViewports();
6095 prepareDisplay(DISPLAY_ORIENTATION_0);
6096 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6097 NotifyMotionArgs args;
6098
6099 // Orientation 270.
6100 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6101 processSync(mapper);
6102
6103 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6104 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6105 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6106
6107 processUp(mapper);
6108 processSync(mapper);
6109 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6110}
6111
6112TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6113 addConfigurationProperty("touch.deviceType", "touchScreen");
6114 prepareButtons();
6115 prepareAxes(POSITION);
6116 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6117 // orientation-aware are affected by display rotation.
6118 addConfigurationProperty("touch.orientationAware", "0");
6119 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6120 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6121
6122 NotifyMotionArgs args;
6123
6124 // Orientation 90, Rotation 0.
6125 clearViewports();
6126 prepareDisplay(DISPLAY_ORIENTATION_0);
6127 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6128 processSync(mapper);
6129
6130 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6131 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6132 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6133
6134 processUp(mapper);
6135 processSync(mapper);
6136 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6137
6138 // Orientation 90, Rotation 90.
6139 clearViewports();
6140 prepareDisplay(DISPLAY_ORIENTATION_90);
6141 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6142 processSync(mapper);
6143
6144 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6145 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6146 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6147
6148 processUp(mapper);
6149 processSync(mapper);
6150 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6151
6152 // Orientation 90, Rotation 180.
6153 clearViewports();
6154 prepareDisplay(DISPLAY_ORIENTATION_180);
6155 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6156 processSync(mapper);
6157
6158 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6159 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6160 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6161
6162 processUp(mapper);
6163 processSync(mapper);
6164 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6165
6166 // Orientation 90, Rotation 270.
6167 clearViewports();
6168 prepareDisplay(DISPLAY_ORIENTATION_270);
6169 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6170 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6171 processSync(mapper);
6172
6173 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6174 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6175 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6176
6177 processUp(mapper);
6178 processSync(mapper);
6179 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6180}
6181
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183 addConfigurationProperty("touch.deviceType", "touchScreen");
6184 prepareDisplay(DISPLAY_ORIENTATION_0);
6185 prepareButtons();
6186 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006187 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188
6189 // These calculations are based on the input device calibration documentation.
6190 int32_t rawX = 100;
6191 int32_t rawY = 200;
6192 int32_t rawPressure = 10;
6193 int32_t rawToolMajor = 12;
6194 int32_t rawDistance = 2;
6195 int32_t rawTiltX = 30;
6196 int32_t rawTiltY = 110;
6197
6198 float x = toDisplayX(rawX);
6199 float y = toDisplayY(rawY);
6200 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6201 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6202 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6203 float distance = float(rawDistance);
6204
6205 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6206 float tiltScale = M_PI / 180;
6207 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6208 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6209 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6210 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6211
6212 processDown(mapper, rawX, rawY);
6213 processPressure(mapper, rawPressure);
6214 processToolMajor(mapper, rawToolMajor);
6215 processDistance(mapper, rawDistance);
6216 processTilt(mapper, rawTiltX, rawTiltY);
6217 processSync(mapper);
6218
6219 NotifyMotionArgs args;
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6222 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6223 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6224}
6225
Jason Gerecke489fda82012-09-07 17:19:40 -07006226TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006227 addConfigurationProperty("touch.deviceType", "touchScreen");
6228 prepareDisplay(DISPLAY_ORIENTATION_0);
6229 prepareLocationCalibration();
6230 prepareButtons();
6231 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006232 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006233
6234 int32_t rawX = 100;
6235 int32_t rawY = 200;
6236
6237 float x = toDisplayX(toCookedX(rawX, rawY));
6238 float y = toDisplayY(toCookedY(rawX, rawY));
6239
6240 processDown(mapper, rawX, rawY);
6241 processSync(mapper);
6242
6243 NotifyMotionArgs args;
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6246 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6247}
6248
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250 addConfigurationProperty("touch.deviceType", "touchScreen");
6251 prepareDisplay(DISPLAY_ORIENTATION_0);
6252 prepareButtons();
6253 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006254 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255
6256 NotifyMotionArgs motionArgs;
6257 NotifyKeyArgs keyArgs;
6258
6259 processDown(mapper, 100, 200);
6260 processSync(mapper);
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6263 ASSERT_EQ(0, motionArgs.buttonState);
6264
6265 // press BTN_LEFT, release BTN_LEFT
6266 processKey(mapper, BTN_LEFT, 1);
6267 processSync(mapper);
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6270 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6271
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6274 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6275
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 processKey(mapper, BTN_LEFT, 0);
6277 processSync(mapper);
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006281
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006284 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285
6286 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6287 processKey(mapper, BTN_RIGHT, 1);
6288 processKey(mapper, BTN_MIDDLE, 1);
6289 processSync(mapper);
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6292 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6293 motionArgs.buttonState);
6294
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6298
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6301 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6302 motionArgs.buttonState);
6303
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 processKey(mapper, BTN_RIGHT, 0);
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006309
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313
6314 processKey(mapper, BTN_MIDDLE, 0);
6315 processSync(mapper);
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006319
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006322 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323
6324 // press BTN_BACK, release BTN_BACK
6325 processKey(mapper, BTN_BACK, 1);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6328 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6329 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6334
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6337 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 processKey(mapper, BTN_BACK, 0);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347 ASSERT_EQ(0, motionArgs.buttonState);
6348
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6350 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6351 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6352
6353 // press BTN_SIDE, release BTN_SIDE
6354 processKey(mapper, BTN_SIDE, 1);
6355 processSync(mapper);
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6357 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6358 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006359
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6366 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367
6368 processKey(mapper, BTN_SIDE, 0);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(0, motionArgs.buttonState);
6377
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6379 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6380 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6381
6382 // press BTN_FORWARD, release BTN_FORWARD
6383 processKey(mapper, BTN_FORWARD, 1);
6384 processSync(mapper);
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6387 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006388
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6392
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6395 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396
6397 processKey(mapper, BTN_FORWARD, 0);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006402
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006405 ASSERT_EQ(0, motionArgs.buttonState);
6406
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6408 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6409 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6410
6411 // press BTN_EXTRA, release BTN_EXTRA
6412 processKey(mapper, BTN_EXTRA, 1);
6413 processSync(mapper);
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6415 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6416 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006417
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006420 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6424 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425
6426 processKey(mapper, BTN_EXTRA, 0);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006430 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006431
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006434 ASSERT_EQ(0, motionArgs.buttonState);
6435
Michael Wrightd02c5b62014-02-10 15:10:22 -08006436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6437 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6438 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6439
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6441
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442 // press BTN_STYLUS, release BTN_STYLUS
6443 processKey(mapper, BTN_STYLUS, 1);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006447 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6448
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6451 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006452
6453 processKey(mapper, BTN_STYLUS, 0);
6454 processSync(mapper);
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006458
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006461 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462
6463 // press BTN_STYLUS2, release BTN_STYLUS2
6464 processKey(mapper, BTN_STYLUS2, 1);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006468 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6469
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6472 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473
6474 processKey(mapper, BTN_STYLUS2, 0);
6475 processSync(mapper);
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006482 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483
6484 // release touch
6485 processUp(mapper);
6486 processSync(mapper);
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6489 ASSERT_EQ(0, motionArgs.buttonState);
6490}
6491
6492TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493 addConfigurationProperty("touch.deviceType", "touchScreen");
6494 prepareDisplay(DISPLAY_ORIENTATION_0);
6495 prepareButtons();
6496 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006497 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498
6499 NotifyMotionArgs motionArgs;
6500
6501 // default tool type is finger
6502 processDown(mapper, 100, 200);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6507
6508 // eraser
6509 processKey(mapper, BTN_TOOL_RUBBER, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6514
6515 // stylus
6516 processKey(mapper, BTN_TOOL_RUBBER, 0);
6517 processKey(mapper, BTN_TOOL_PEN, 1);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6522
6523 // brush
6524 processKey(mapper, BTN_TOOL_PEN, 0);
6525 processKey(mapper, BTN_TOOL_BRUSH, 1);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6530
6531 // pencil
6532 processKey(mapper, BTN_TOOL_BRUSH, 0);
6533 processKey(mapper, BTN_TOOL_PENCIL, 1);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6538
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006539 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006540 processKey(mapper, BTN_TOOL_PENCIL, 0);
6541 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6542 processSync(mapper);
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6546
6547 // mouse
6548 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6549 processKey(mapper, BTN_TOOL_MOUSE, 1);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6554
6555 // lens
6556 processKey(mapper, BTN_TOOL_MOUSE, 0);
6557 processKey(mapper, BTN_TOOL_LENS, 1);
6558 processSync(mapper);
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6562
6563 // double-tap
6564 processKey(mapper, BTN_TOOL_LENS, 0);
6565 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6566 processSync(mapper);
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6570
6571 // triple-tap
6572 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6573 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6574 processSync(mapper);
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6578
6579 // quad-tap
6580 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6581 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6582 processSync(mapper);
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6586
6587 // finger
6588 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6589 processKey(mapper, BTN_TOOL_FINGER, 1);
6590 processSync(mapper);
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6594
6595 // stylus trumps finger
6596 processKey(mapper, BTN_TOOL_PEN, 1);
6597 processSync(mapper);
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6601
6602 // eraser trumps stylus
6603 processKey(mapper, BTN_TOOL_RUBBER, 1);
6604 processSync(mapper);
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6608
6609 // mouse trumps eraser
6610 processKey(mapper, BTN_TOOL_MOUSE, 1);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6615
6616 // back to default tool type
6617 processKey(mapper, BTN_TOOL_MOUSE, 0);
6618 processKey(mapper, BTN_TOOL_RUBBER, 0);
6619 processKey(mapper, BTN_TOOL_PEN, 0);
6620 processKey(mapper, BTN_TOOL_FINGER, 0);
6621 processSync(mapper);
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6625}
6626
6627TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006628 addConfigurationProperty("touch.deviceType", "touchScreen");
6629 prepareDisplay(DISPLAY_ORIENTATION_0);
6630 prepareButtons();
6631 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006632 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006633 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006634
6635 NotifyMotionArgs motionArgs;
6636
6637 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6638 processKey(mapper, BTN_TOOL_FINGER, 1);
6639 processMove(mapper, 100, 200);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6644 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6645
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // move a little
6652 processMove(mapper, 150, 250);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6657 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6658
6659 // down when BTN_TOUCH is pressed, pressure defaults to 1
6660 processKey(mapper, BTN_TOUCH, 1);
6661 processSync(mapper);
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6665 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6671
6672 // up when BTN_TOUCH is released, hover restored
6673 processKey(mapper, BTN_TOUCH, 0);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6679
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6683 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6684
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
6691 processKey(mapper, BTN_TOOL_FINGER, 0);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6697}
6698
6699TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006700 addConfigurationProperty("touch.deviceType", "touchScreen");
6701 prepareDisplay(DISPLAY_ORIENTATION_0);
6702 prepareButtons();
6703 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006704 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705
6706 NotifyMotionArgs motionArgs;
6707
6708 // initially hovering because pressure is 0
6709 processDown(mapper, 100, 200);
6710 processPressure(mapper, 0);
6711 processSync(mapper);
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6715 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6716
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6718 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // move a little
6723 processMove(mapper, 150, 250);
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6728 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6729
6730 // down when pressure is non-zero
6731 processPressure(mapper, RAW_PRESSURE_MAX);
6732 processSync(mapper);
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6736 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6737
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6742
6743 // up when pressure becomes 0, hover restored
6744 processPressure(mapper, 0);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6749 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6750
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6754 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6755
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
6762 processUp(mapper);
6763 processSync(mapper);
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6765 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6767 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6768}
6769
lilinnane74b35f2022-07-19 16:00:50 +08006770TEST_F(SingleTouchInputMapperTest,
6771 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6772 addConfigurationProperty("touch.deviceType", "touchScreen");
6773 prepareDisplay(DISPLAY_ORIENTATION_0);
6774 prepareButtons();
6775 prepareAxes(POSITION);
6776 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6777 NotifyMotionArgs motionArgs;
6778
6779 // Down.
6780 processDown(mapper, 100, 200);
6781 processSync(mapper);
6782
6783 // We should receive a down event
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6785 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6786
6787 // Change display id
6788 clearViewports();
6789 prepareSecondaryDisplay(ViewportType::INTERNAL);
6790
6791 // We should receive a cancel event
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6793 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6794 // Then receive reset called
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6796}
6797
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006798TEST_F(SingleTouchInputMapperTest,
6799 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6800 addConfigurationProperty("touch.deviceType", "touchScreen");
6801 prepareDisplay(DISPLAY_ORIENTATION_0);
6802 prepareButtons();
6803 prepareAxes(POSITION);
6804 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6806 NotifyMotionArgs motionArgs;
6807
6808 // Start a new gesture.
6809 processDown(mapper, 100, 200);
6810 processSync(mapper);
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6812 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6813
6814 // Make the viewport inactive. This will put the device in disabled mode.
6815 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6816 viewport->isActive = false;
6817 mFakePolicy->updateViewport(*viewport);
6818 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6819
6820 // We should receive a cancel event for the ongoing gesture.
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6822 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6823 // Then we should be notified that the device was reset.
6824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6825
6826 // No events are generated while the viewport is inactive.
6827 processMove(mapper, 101, 201);
6828 processSync(mapper);
6829 processDown(mapper, 102, 202);
6830 processSync(mapper);
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6832
6833 // Make the viewport active again. The device should resume processing events.
6834 viewport->isActive = true;
6835 mFakePolicy->updateViewport(*viewport);
6836 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6837
6838 // The device is reset because it changes back to direct mode, without generating any events.
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6841
6842 // Start a new gesture.
6843 processDown(mapper, 100, 200);
6844 processSync(mapper);
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6846 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6847
6848 // No more events.
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6851}
6852
Prabir Pradhan5632d622021-09-06 07:57:20 -07006853// --- TouchDisplayProjectionTest ---
6854
6855class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6856public:
6857 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6858 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6859 // rotated equivalent of the given un-rotated physical display bounds.
6860 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6861 uint32_t inverseRotationFlags;
6862 auto width = DISPLAY_WIDTH;
6863 auto height = DISPLAY_HEIGHT;
6864 switch (orientation) {
6865 case DISPLAY_ORIENTATION_90:
6866 inverseRotationFlags = ui::Transform::ROT_270;
6867 std::swap(width, height);
6868 break;
6869 case DISPLAY_ORIENTATION_180:
6870 inverseRotationFlags = ui::Transform::ROT_180;
6871 break;
6872 case DISPLAY_ORIENTATION_270:
6873 inverseRotationFlags = ui::Transform::ROT_90;
6874 std::swap(width, height);
6875 break;
6876 case DISPLAY_ORIENTATION_0:
6877 inverseRotationFlags = ui::Transform::ROT_0;
6878 break;
6879 default:
6880 FAIL() << "Invalid orientation: " << orientation;
6881 }
6882
6883 const ui::Transform rotation(inverseRotationFlags, width, height);
6884 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6885
6886 std::optional<DisplayViewport> internalViewport =
6887 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6888 DisplayViewport& v = *internalViewport;
6889 v.displayId = DISPLAY_ID;
6890 v.orientation = orientation;
6891
6892 v.logicalLeft = 0;
6893 v.logicalTop = 0;
6894 v.logicalRight = 100;
6895 v.logicalBottom = 100;
6896
6897 v.physicalLeft = rotatedPhysicalDisplay.left;
6898 v.physicalTop = rotatedPhysicalDisplay.top;
6899 v.physicalRight = rotatedPhysicalDisplay.right;
6900 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6901
6902 v.deviceWidth = width;
6903 v.deviceHeight = height;
6904
6905 v.isActive = true;
6906 v.uniqueId = UNIQUE_ID;
6907 v.type = ViewportType::INTERNAL;
6908 mFakePolicy->updateViewport(v);
6909 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6910 }
6911
6912 void assertReceivedMove(const Point& point) {
6913 NotifyMotionArgs motionArgs;
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6916 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6918 1, 0, 0, 0, 0, 0, 0, 0));
6919 }
6920};
6921
6922TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6923 addConfigurationProperty("touch.deviceType", "touchScreen");
6924 prepareDisplay(DISPLAY_ORIENTATION_0);
6925
6926 prepareButtons();
6927 prepareAxes(POSITION);
6928 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6929
6930 NotifyMotionArgs motionArgs;
6931
6932 // Configure the DisplayViewport such that the logical display maps to a subsection of
6933 // the display panel called the physical display. Here, the physical display is bounded by the
6934 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6935 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6936 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6937 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6938
6939 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6940 DISPLAY_ORIENTATION_270}) {
6941 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6942
6943 // Touches outside the physical display should be ignored, and should not generate any
6944 // events. Ensure touches at the following points that lie outside of the physical display
6945 // area do not generate any events.
6946 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6947 processDown(mapper, toRawX(point.x), toRawY(point.y));
6948 processSync(mapper);
6949 processUp(mapper);
6950 processSync(mapper);
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6952 << "Unexpected event generated for touch outside physical display at point: "
6953 << point.x << ", " << point.y;
6954 }
6955 }
6956}
6957
6958TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6959 addConfigurationProperty("touch.deviceType", "touchScreen");
6960 prepareDisplay(DISPLAY_ORIENTATION_0);
6961
6962 prepareButtons();
6963 prepareAxes(POSITION);
6964 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6965
6966 NotifyMotionArgs motionArgs;
6967
6968 // Configure the DisplayViewport such that the logical display maps to a subsection of
6969 // the display panel called the physical display. Here, the physical display is bounded by the
6970 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6971 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6972
6973 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6974 DISPLAY_ORIENTATION_270}) {
6975 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6976
6977 // Touches that start outside the physical display should be ignored until it enters the
6978 // physical display bounds, at which point it should generate a down event. Start a touch at
6979 // the point (5, 100), which is outside the physical display bounds.
6980 static const Point kOutsidePoint{5, 100};
6981 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6982 processSync(mapper);
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6984
6985 // Move the touch into the physical display area. This should generate a pointer down.
6986 processMove(mapper, toRawX(11), toRawY(21));
6987 processSync(mapper);
6988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6990 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6991 ASSERT_NO_FATAL_FAILURE(
6992 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6993
6994 // Move the touch inside the physical display area. This should generate a pointer move.
6995 processMove(mapper, toRawX(69), toRawY(159));
6996 processSync(mapper);
6997 assertReceivedMove({69, 159});
6998
6999 // Move outside the physical display area. Since the pointer is already down, this should
7000 // now continue generating events.
7001 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7002 processSync(mapper);
7003 assertReceivedMove(kOutsidePoint);
7004
7005 // Release. This should generate a pointer up.
7006 processUp(mapper);
7007 processSync(mapper);
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7009 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7011 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7012
7013 // Ensure no more events were generated.
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7016 }
7017}
7018
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019// --- MultiTouchInputMapperTest ---
7020
7021class MultiTouchInputMapperTest : public TouchInputMapperTest {
7022protected:
7023 void prepareAxes(int axes);
7024
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007025 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7026 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7027 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7028 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7029 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7030 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7031 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7032 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7033 void processId(MultiTouchInputMapper& mapper, int32_t id);
7034 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7035 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7036 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7037 void processMTSync(MultiTouchInputMapper& mapper);
7038 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007039};
7040
7041void MultiTouchInputMapperTest::prepareAxes(int axes) {
7042 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007043 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7044 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007045 }
7046 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007047 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7048 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007050 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7051 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007052 }
7053 }
7054 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007055 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7056 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007058 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007059 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007060 }
7061 }
7062 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007063 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7064 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065 }
7066 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007067 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7068 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007069 }
7070 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007071 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7072 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073 }
7074 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007075 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7076 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007077 }
7078 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007079 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7080 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081 }
7082 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084 }
7085}
7086
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007087void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7088 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091}
7092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007093void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7094 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096}
7097
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007098void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7099 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007101}
7102
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007103void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105}
7106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007107void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109}
7110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007111void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7112 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114}
7115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007116void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007118}
7119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007120void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007122}
7123
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007124void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126}
7127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007128void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007130}
7131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007132void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134}
7135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007136void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7137 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139}
7140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007141void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143}
7144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007145void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147}
7148
Michael Wrightd02c5b62014-02-10 15:10:22 -08007149TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150 addConfigurationProperty("touch.deviceType", "touchScreen");
7151 prepareDisplay(DISPLAY_ORIENTATION_0);
7152 prepareAxes(POSITION);
7153 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007154 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155
arthurhungdcef2dc2020-08-11 14:47:50 +08007156 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157
7158 NotifyMotionArgs motionArgs;
7159
7160 // Two fingers down at once.
7161 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7162 processPosition(mapper, x1, y1);
7163 processMTSync(mapper);
7164 processPosition(mapper, x2, y2);
7165 processMTSync(mapper);
7166 processSync(mapper);
7167
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7169 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7170 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7171 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7172 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7173 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7174 ASSERT_EQ(0, motionArgs.flags);
7175 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7176 ASSERT_EQ(0, motionArgs.buttonState);
7177 ASSERT_EQ(0, motionArgs.edgeFlags);
7178 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7179 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7182 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7183 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7184 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7186
7187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7188 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7189 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7190 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7191 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007192 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007193 ASSERT_EQ(0, motionArgs.flags);
7194 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7195 ASSERT_EQ(0, motionArgs.buttonState);
7196 ASSERT_EQ(0, motionArgs.edgeFlags);
7197 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7198 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7200 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7203 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7205 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7206 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7207 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7208 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7209
7210 // Move.
7211 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7212 processPosition(mapper, x1, y1);
7213 processMTSync(mapper);
7214 processPosition(mapper, x2, y2);
7215 processMTSync(mapper);
7216 processSync(mapper);
7217
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7219 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7220 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7221 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7222 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7224 ASSERT_EQ(0, motionArgs.flags);
7225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7226 ASSERT_EQ(0, motionArgs.buttonState);
7227 ASSERT_EQ(0, motionArgs.edgeFlags);
7228 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7229 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7231 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7234 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7236 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7237 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7238 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7240
7241 // First finger up.
7242 x2 += 15; y2 -= 20;
7243 processPosition(mapper, x2, y2);
7244 processMTSync(mapper);
7245 processSync(mapper);
7246
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7249 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7250 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7251 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007252 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007253 ASSERT_EQ(0, motionArgs.flags);
7254 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7255 ASSERT_EQ(0, motionArgs.buttonState);
7256 ASSERT_EQ(0, motionArgs.edgeFlags);
7257 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7258 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7260 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7263 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7265 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7266 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7267 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7269
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7272 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7273 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7274 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7276 ASSERT_EQ(0, motionArgs.flags);
7277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7278 ASSERT_EQ(0, motionArgs.buttonState);
7279 ASSERT_EQ(0, motionArgs.edgeFlags);
7280 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7281 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7282 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7284 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7285 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7286 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7287 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7288
7289 // Move.
7290 x2 += 20; y2 -= 25;
7291 processPosition(mapper, x2, y2);
7292 processMTSync(mapper);
7293 processSync(mapper);
7294
7295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7297 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7298 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7299 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7301 ASSERT_EQ(0, motionArgs.flags);
7302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7303 ASSERT_EQ(0, motionArgs.buttonState);
7304 ASSERT_EQ(0, motionArgs.edgeFlags);
7305 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7306 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7309 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7310 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7311 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7313
7314 // New finger down.
7315 int32_t x3 = 700, y3 = 300;
7316 processPosition(mapper, x2, y2);
7317 processMTSync(mapper);
7318 processPosition(mapper, x3, y3);
7319 processMTSync(mapper);
7320 processSync(mapper);
7321
7322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7324 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7325 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7326 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007327 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007328 ASSERT_EQ(0, motionArgs.flags);
7329 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7330 ASSERT_EQ(0, motionArgs.buttonState);
7331 ASSERT_EQ(0, motionArgs.edgeFlags);
7332 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7333 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7335 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7338 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7340 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7341 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7342 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7344
7345 // Second finger up.
7346 x3 += 30; y3 -= 20;
7347 processPosition(mapper, x3, y3);
7348 processMTSync(mapper);
7349 processSync(mapper);
7350
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7353 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7354 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7355 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007356 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007357 ASSERT_EQ(0, motionArgs.flags);
7358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7359 ASSERT_EQ(0, motionArgs.buttonState);
7360 ASSERT_EQ(0, motionArgs.edgeFlags);
7361 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7364 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7367 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7369 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7370 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7371 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7372 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7373
7374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7376 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7377 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7378 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7380 ASSERT_EQ(0, motionArgs.flags);
7381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7382 ASSERT_EQ(0, motionArgs.buttonState);
7383 ASSERT_EQ(0, motionArgs.edgeFlags);
7384 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7388 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7389 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7390 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7391 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7392
7393 // Last finger up.
7394 processMTSync(mapper);
7395 processSync(mapper);
7396
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7399 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7400 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7401 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7402 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7403 ASSERT_EQ(0, motionArgs.flags);
7404 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7405 ASSERT_EQ(0, motionArgs.buttonState);
7406 ASSERT_EQ(0, motionArgs.edgeFlags);
7407 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7408 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7411 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7412 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7413 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7415
7416 // Should not have sent any more keys or motions.
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7419}
7420
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007421TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7422 addConfigurationProperty("touch.deviceType", "touchScreen");
7423 prepareDisplay(DISPLAY_ORIENTATION_0);
7424
7425 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7426 /*fuzz*/ 0, /*resolution*/ 10);
7427 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7428 /*fuzz*/ 0, /*resolution*/ 11);
7429 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7430 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7431 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7432 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7433 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7434 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7435 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7436 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7437
7438 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7439
7440 // X and Y axes
7441 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7442 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7443 // Touch major and minor
7444 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7445 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7446 // Tool major and minor
7447 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7448 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7449}
7450
7451TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7452 addConfigurationProperty("touch.deviceType", "touchScreen");
7453 prepareDisplay(DISPLAY_ORIENTATION_0);
7454
7455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7456 /*fuzz*/ 0, /*resolution*/ 10);
7457 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7458 /*fuzz*/ 0, /*resolution*/ 11);
7459
7460 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7461
7462 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7463
7464 // Touch major and minor
7465 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7466 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7467 // Tool major and minor
7468 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7469 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7470}
7471
Michael Wrightd02c5b62014-02-10 15:10:22 -08007472TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007473 addConfigurationProperty("touch.deviceType", "touchScreen");
7474 prepareDisplay(DISPLAY_ORIENTATION_0);
7475 prepareAxes(POSITION | ID);
7476 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007477 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007478
arthurhungdcef2dc2020-08-11 14:47:50 +08007479 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007480
7481 NotifyMotionArgs motionArgs;
7482
7483 // Two fingers down at once.
7484 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7485 processPosition(mapper, x1, y1);
7486 processId(mapper, 1);
7487 processMTSync(mapper);
7488 processPosition(mapper, x2, y2);
7489 processId(mapper, 2);
7490 processMTSync(mapper);
7491 processSync(mapper);
7492
7493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7494 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7495 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7496 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7499 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7500
7501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007502 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007503 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7504 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7506 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7509 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7511 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7512
7513 // Move.
7514 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7515 processPosition(mapper, x1, y1);
7516 processId(mapper, 1);
7517 processMTSync(mapper);
7518 processPosition(mapper, x2, y2);
7519 processId(mapper, 2);
7520 processMTSync(mapper);
7521 processSync(mapper);
7522
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7525 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7526 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7528 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7531 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7533 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7534
7535 // First finger up.
7536 x2 += 15; y2 -= 20;
7537 processPosition(mapper, x2, y2);
7538 processId(mapper, 2);
7539 processMTSync(mapper);
7540 processSync(mapper);
7541
7542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007543 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007544 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7545 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7547 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7550 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7552 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7553
7554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7556 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7557 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7560 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7561
7562 // Move.
7563 x2 += 20; y2 -= 25;
7564 processPosition(mapper, x2, y2);
7565 processId(mapper, 2);
7566 processMTSync(mapper);
7567 processSync(mapper);
7568
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7571 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7572 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7575 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7576
7577 // New finger down.
7578 int32_t x3 = 700, y3 = 300;
7579 processPosition(mapper, x2, y2);
7580 processId(mapper, 2);
7581 processMTSync(mapper);
7582 processPosition(mapper, x3, y3);
7583 processId(mapper, 3);
7584 processMTSync(mapper);
7585 processSync(mapper);
7586
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007588 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7590 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7592 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7595 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7597 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7598
7599 // Second finger up.
7600 x3 += 30; y3 -= 20;
7601 processPosition(mapper, x3, y3);
7602 processId(mapper, 3);
7603 processMTSync(mapper);
7604 processSync(mapper);
7605
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007607 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007608 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7609 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7611 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7614 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7616 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7617
7618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7620 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7621 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7624 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7625
7626 // Last finger up.
7627 processMTSync(mapper);
7628 processSync(mapper);
7629
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7631 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7632 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7636 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7637
7638 // Should not have sent any more keys or motions.
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7641}
7642
7643TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007644 addConfigurationProperty("touch.deviceType", "touchScreen");
7645 prepareDisplay(DISPLAY_ORIENTATION_0);
7646 prepareAxes(POSITION | ID | SLOT);
7647 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007648 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007649
arthurhungdcef2dc2020-08-11 14:47:50 +08007650 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651
7652 NotifyMotionArgs motionArgs;
7653
7654 // Two fingers down at once.
7655 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7656 processPosition(mapper, x1, y1);
7657 processId(mapper, 1);
7658 processSlot(mapper, 1);
7659 processPosition(mapper, x2, y2);
7660 processId(mapper, 2);
7661 processSync(mapper);
7662
7663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7664 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7665 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7666 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7669 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7670
7671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007672 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007673 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7674 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7676 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7679 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7681 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7682
7683 // Move.
7684 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7685 processSlot(mapper, 0);
7686 processPosition(mapper, x1, y1);
7687 processSlot(mapper, 1);
7688 processPosition(mapper, x2, y2);
7689 processSync(mapper);
7690
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7693 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7694 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7696 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7699 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7701 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7702
7703 // First finger up.
7704 x2 += 15; y2 -= 20;
7705 processSlot(mapper, 0);
7706 processId(mapper, -1);
7707 processSlot(mapper, 1);
7708 processPosition(mapper, x2, y2);
7709 processSync(mapper);
7710
7711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007712 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007713 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7714 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7716 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7719 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7721 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7722
7723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7725 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7726 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7727 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7729 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7730
7731 // Move.
7732 x2 += 20; y2 -= 25;
7733 processPosition(mapper, x2, y2);
7734 processSync(mapper);
7735
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7738 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7739 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7742 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7743
7744 // New finger down.
7745 int32_t x3 = 700, y3 = 300;
7746 processPosition(mapper, x2, y2);
7747 processSlot(mapper, 0);
7748 processId(mapper, 3);
7749 processPosition(mapper, x3, y3);
7750 processSync(mapper);
7751
7752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007753 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007754 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7755 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7757 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7760 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7762 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7763
7764 // Second finger up.
7765 x3 += 30; y3 -= 20;
7766 processSlot(mapper, 1);
7767 processId(mapper, -1);
7768 processSlot(mapper, 0);
7769 processPosition(mapper, x3, y3);
7770 processSync(mapper);
7771
7772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007773 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007774 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7775 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7777 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7780 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7782 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7783
7784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7786 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7787 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7790 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7791
7792 // Last finger up.
7793 processId(mapper, -1);
7794 processSync(mapper);
7795
7796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7797 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7798 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7799 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7802 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7803
7804 // Should not have sent any more keys or motions.
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7807}
7808
7809TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810 addConfigurationProperty("touch.deviceType", "touchScreen");
7811 prepareDisplay(DISPLAY_ORIENTATION_0);
7812 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007813 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814
7815 // These calculations are based on the input device calibration documentation.
7816 int32_t rawX = 100;
7817 int32_t rawY = 200;
7818 int32_t rawTouchMajor = 7;
7819 int32_t rawTouchMinor = 6;
7820 int32_t rawToolMajor = 9;
7821 int32_t rawToolMinor = 8;
7822 int32_t rawPressure = 11;
7823 int32_t rawDistance = 0;
7824 int32_t rawOrientation = 3;
7825 int32_t id = 5;
7826
7827 float x = toDisplayX(rawX);
7828 float y = toDisplayY(rawY);
7829 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7830 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7831 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7832 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7833 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7834 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7835 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7836 float distance = float(rawDistance);
7837
7838 processPosition(mapper, rawX, rawY);
7839 processTouchMajor(mapper, rawTouchMajor);
7840 processTouchMinor(mapper, rawTouchMinor);
7841 processToolMajor(mapper, rawToolMajor);
7842 processToolMinor(mapper, rawToolMinor);
7843 processPressure(mapper, rawPressure);
7844 processOrientation(mapper, rawOrientation);
7845 processDistance(mapper, rawDistance);
7846 processId(mapper, id);
7847 processMTSync(mapper);
7848 processSync(mapper);
7849
7850 NotifyMotionArgs args;
7851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7852 ASSERT_EQ(0, args.pointerProperties[0].id);
7853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7854 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7855 orientation, distance));
7856}
7857
7858TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007859 addConfigurationProperty("touch.deviceType", "touchScreen");
7860 prepareDisplay(DISPLAY_ORIENTATION_0);
7861 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7862 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007863 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864
7865 // These calculations are based on the input device calibration documentation.
7866 int32_t rawX = 100;
7867 int32_t rawY = 200;
7868 int32_t rawTouchMajor = 140;
7869 int32_t rawTouchMinor = 120;
7870 int32_t rawToolMajor = 180;
7871 int32_t rawToolMinor = 160;
7872
7873 float x = toDisplayX(rawX);
7874 float y = toDisplayY(rawY);
7875 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7876 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7877 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7878 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7879 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7880
7881 processPosition(mapper, rawX, rawY);
7882 processTouchMajor(mapper, rawTouchMajor);
7883 processTouchMinor(mapper, rawTouchMinor);
7884 processToolMajor(mapper, rawToolMajor);
7885 processToolMinor(mapper, rawToolMinor);
7886 processMTSync(mapper);
7887 processSync(mapper);
7888
7889 NotifyMotionArgs args;
7890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7892 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7893}
7894
7895TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007896 addConfigurationProperty("touch.deviceType", "touchScreen");
7897 prepareDisplay(DISPLAY_ORIENTATION_0);
7898 prepareAxes(POSITION | TOUCH | TOOL);
7899 addConfigurationProperty("touch.size.calibration", "diameter");
7900 addConfigurationProperty("touch.size.scale", "10");
7901 addConfigurationProperty("touch.size.bias", "160");
7902 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007903 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904
7905 // These calculations are based on the input device calibration documentation.
7906 // Note: We only provide a single common touch/tool value because the device is assumed
7907 // not to emit separate values for each pointer (isSummed = 1).
7908 int32_t rawX = 100;
7909 int32_t rawY = 200;
7910 int32_t rawX2 = 150;
7911 int32_t rawY2 = 250;
7912 int32_t rawTouchMajor = 5;
7913 int32_t rawToolMajor = 8;
7914
7915 float x = toDisplayX(rawX);
7916 float y = toDisplayY(rawY);
7917 float x2 = toDisplayX(rawX2);
7918 float y2 = toDisplayY(rawY2);
7919 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7920 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7921 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7922
7923 processPosition(mapper, rawX, rawY);
7924 processTouchMajor(mapper, rawTouchMajor);
7925 processToolMajor(mapper, rawToolMajor);
7926 processMTSync(mapper);
7927 processPosition(mapper, rawX2, rawY2);
7928 processTouchMajor(mapper, rawTouchMajor);
7929 processToolMajor(mapper, rawToolMajor);
7930 processMTSync(mapper);
7931 processSync(mapper);
7932
7933 NotifyMotionArgs args;
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7935 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7936
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007938 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 ASSERT_EQ(size_t(2), args.pointerCount);
7940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7941 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7943 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7944}
7945
7946TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007947 addConfigurationProperty("touch.deviceType", "touchScreen");
7948 prepareDisplay(DISPLAY_ORIENTATION_0);
7949 prepareAxes(POSITION | TOUCH | TOOL);
7950 addConfigurationProperty("touch.size.calibration", "area");
7951 addConfigurationProperty("touch.size.scale", "43");
7952 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007953 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007954
7955 // These calculations are based on the input device calibration documentation.
7956 int32_t rawX = 100;
7957 int32_t rawY = 200;
7958 int32_t rawTouchMajor = 5;
7959 int32_t rawToolMajor = 8;
7960
7961 float x = toDisplayX(rawX);
7962 float y = toDisplayY(rawY);
7963 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7964 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7965 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7966
7967 processPosition(mapper, rawX, rawY);
7968 processTouchMajor(mapper, rawTouchMajor);
7969 processToolMajor(mapper, rawToolMajor);
7970 processMTSync(mapper);
7971 processSync(mapper);
7972
7973 NotifyMotionArgs args;
7974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7976 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7977}
7978
7979TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007980 addConfigurationProperty("touch.deviceType", "touchScreen");
7981 prepareDisplay(DISPLAY_ORIENTATION_0);
7982 prepareAxes(POSITION | PRESSURE);
7983 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7984 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007985 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986
Michael Wrightaa449c92017-12-13 21:21:43 +00007987 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007988 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007989 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7990 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7991 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7992
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 // These calculations are based on the input device calibration documentation.
7994 int32_t rawX = 100;
7995 int32_t rawY = 200;
7996 int32_t rawPressure = 60;
7997
7998 float x = toDisplayX(rawX);
7999 float y = toDisplayY(rawY);
8000 float pressure = float(rawPressure) * 0.01f;
8001
8002 processPosition(mapper, rawX, rawY);
8003 processPressure(mapper, rawPressure);
8004 processMTSync(mapper);
8005 processSync(mapper);
8006
8007 NotifyMotionArgs args;
8008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8010 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8011}
8012
8013TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 addConfigurationProperty("touch.deviceType", "touchScreen");
8015 prepareDisplay(DISPLAY_ORIENTATION_0);
8016 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008017 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008018
8019 NotifyMotionArgs motionArgs;
8020 NotifyKeyArgs keyArgs;
8021
8022 processId(mapper, 1);
8023 processPosition(mapper, 100, 200);
8024 processSync(mapper);
8025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8026 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8027 ASSERT_EQ(0, motionArgs.buttonState);
8028
8029 // press BTN_LEFT, release BTN_LEFT
8030 processKey(mapper, BTN_LEFT, 1);
8031 processSync(mapper);
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8034 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8035
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8038 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8039
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 processKey(mapper, BTN_LEFT, 0);
8041 processSync(mapper);
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008043 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008045
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008048 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008049
8050 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8051 processKey(mapper, BTN_RIGHT, 1);
8052 processKey(mapper, BTN_MIDDLE, 1);
8053 processSync(mapper);
8054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8056 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8057 motionArgs.buttonState);
8058
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8061 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8062
8063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8065 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8066 motionArgs.buttonState);
8067
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 processKey(mapper, BTN_RIGHT, 0);
8069 processSync(mapper);
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008073
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008076 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077
8078 processKey(mapper, BTN_MIDDLE, 0);
8079 processSync(mapper);
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008082 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008083
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008086 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008087
8088 // press BTN_BACK, release BTN_BACK
8089 processKey(mapper, BTN_BACK, 1);
8090 processSync(mapper);
8091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8092 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8093 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008094
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008097 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8098
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8101 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102
8103 processKey(mapper, BTN_BACK, 0);
8104 processSync(mapper);
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008107 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008108
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008111 ASSERT_EQ(0, motionArgs.buttonState);
8112
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8114 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8115 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8116
8117 // press BTN_SIDE, release BTN_SIDE
8118 processKey(mapper, BTN_SIDE, 1);
8119 processSync(mapper);
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8121 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8122 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008123
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008126 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8127
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8130 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131
8132 processKey(mapper, BTN_SIDE, 0);
8133 processSync(mapper);
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008135 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137
8138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008140 ASSERT_EQ(0, motionArgs.buttonState);
8141
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8143 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8144 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8145
8146 // press BTN_FORWARD, release BTN_FORWARD
8147 processKey(mapper, BTN_FORWARD, 1);
8148 processSync(mapper);
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8150 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8151 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008152
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8156
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8158 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8159 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160
8161 processKey(mapper, BTN_FORWARD, 0);
8162 processSync(mapper);
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008164 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008166
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008169 ASSERT_EQ(0, motionArgs.buttonState);
8170
Michael Wrightd02c5b62014-02-10 15:10:22 -08008171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8172 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8173 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8174
8175 // press BTN_EXTRA, release BTN_EXTRA
8176 processKey(mapper, BTN_EXTRA, 1);
8177 processSync(mapper);
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8179 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8180 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008181
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008184 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8185
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8188 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189
8190 processKey(mapper, BTN_EXTRA, 0);
8191 processSync(mapper);
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008194 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008195
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008198 ASSERT_EQ(0, motionArgs.buttonState);
8199
Michael Wrightd02c5b62014-02-10 15:10:22 -08008200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8201 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8202 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8203
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8205
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 // press BTN_STYLUS, release BTN_STYLUS
8207 processKey(mapper, BTN_STYLUS, 1);
8208 processSync(mapper);
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008211 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8212
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8215 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008216
8217 processKey(mapper, BTN_STYLUS, 0);
8218 processSync(mapper);
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008222
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008225 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008226
8227 // press BTN_STYLUS2, release BTN_STYLUS2
8228 processKey(mapper, BTN_STYLUS2, 1);
8229 processSync(mapper);
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008232 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8233
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8236 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237
8238 processKey(mapper, BTN_STYLUS2, 0);
8239 processSync(mapper);
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008243
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008246 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247
8248 // release touch
8249 processId(mapper, -1);
8250 processSync(mapper);
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8252 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8253 ASSERT_EQ(0, motionArgs.buttonState);
8254}
8255
8256TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257 addConfigurationProperty("touch.deviceType", "touchScreen");
8258 prepareDisplay(DISPLAY_ORIENTATION_0);
8259 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008260 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261
8262 NotifyMotionArgs motionArgs;
8263
8264 // default tool type is finger
8265 processId(mapper, 1);
8266 processPosition(mapper, 100, 200);
8267 processSync(mapper);
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8271
8272 // eraser
8273 processKey(mapper, BTN_TOOL_RUBBER, 1);
8274 processSync(mapper);
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8278
8279 // stylus
8280 processKey(mapper, BTN_TOOL_RUBBER, 0);
8281 processKey(mapper, BTN_TOOL_PEN, 1);
8282 processSync(mapper);
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8286
8287 // brush
8288 processKey(mapper, BTN_TOOL_PEN, 0);
8289 processKey(mapper, BTN_TOOL_BRUSH, 1);
8290 processSync(mapper);
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8294
8295 // pencil
8296 processKey(mapper, BTN_TOOL_BRUSH, 0);
8297 processKey(mapper, BTN_TOOL_PENCIL, 1);
8298 processSync(mapper);
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8302
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008303 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008304 processKey(mapper, BTN_TOOL_PENCIL, 0);
8305 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8306 processSync(mapper);
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8310
8311 // mouse
8312 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8313 processKey(mapper, BTN_TOOL_MOUSE, 1);
8314 processSync(mapper);
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8318
8319 // lens
8320 processKey(mapper, BTN_TOOL_MOUSE, 0);
8321 processKey(mapper, BTN_TOOL_LENS, 1);
8322 processSync(mapper);
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8326
8327 // double-tap
8328 processKey(mapper, BTN_TOOL_LENS, 0);
8329 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8330 processSync(mapper);
8331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8334
8335 // triple-tap
8336 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8337 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8338 processSync(mapper);
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8342
8343 // quad-tap
8344 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8345 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8346 processSync(mapper);
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8350
8351 // finger
8352 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8353 processKey(mapper, BTN_TOOL_FINGER, 1);
8354 processSync(mapper);
8355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8358
8359 // stylus trumps finger
8360 processKey(mapper, BTN_TOOL_PEN, 1);
8361 processSync(mapper);
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8365
8366 // eraser trumps stylus
8367 processKey(mapper, BTN_TOOL_RUBBER, 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_ERASER, motionArgs.pointerProperties[0].toolType);
8372
8373 // mouse trumps eraser
8374 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
8379
8380 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8381 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
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_FINGER, motionArgs.pointerProperties[0].toolType);
8386
8387 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8388 processToolType(mapper, MT_TOOL_PEN);
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_STYLUS, motionArgs.pointerProperties[0].toolType);
8393
8394 // back to default tool type
8395 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8396 processKey(mapper, BTN_TOOL_MOUSE, 0);
8397 processKey(mapper, BTN_TOOL_RUBBER, 0);
8398 processKey(mapper, BTN_TOOL_PEN, 0);
8399 processKey(mapper, BTN_TOOL_FINGER, 0);
8400 processSync(mapper);
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8404}
8405
8406TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407 addConfigurationProperty("touch.deviceType", "touchScreen");
8408 prepareDisplay(DISPLAY_ORIENTATION_0);
8409 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008410 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008411 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008412
8413 NotifyMotionArgs motionArgs;
8414
8415 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8416 processId(mapper, 1);
8417 processPosition(mapper, 100, 200);
8418 processSync(mapper);
8419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8420 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8422 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8423
8424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8425 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8427 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8428
8429 // move a little
8430 processPosition(mapper, 150, 250);
8431 processSync(mapper);
8432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8433 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8435 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8436
8437 // down when BTN_TOUCH is pressed, pressure defaults to 1
8438 processKey(mapper, BTN_TOUCH, 1);
8439 processSync(mapper);
8440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8443 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8444
8445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8448 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8449
8450 // up when BTN_TOUCH is released, hover restored
8451 processKey(mapper, BTN_TOUCH, 0);
8452 processSync(mapper);
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8454 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8456 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8457
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8459 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8461 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8462
8463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8464 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8466 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8467
8468 // exit hover when pointer goes away
8469 processId(mapper, -1);
8470 processSync(mapper);
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8472 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8474 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8475}
8476
8477TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008478 addConfigurationProperty("touch.deviceType", "touchScreen");
8479 prepareDisplay(DISPLAY_ORIENTATION_0);
8480 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008481 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008482
8483 NotifyMotionArgs motionArgs;
8484
8485 // initially hovering because pressure is 0
8486 processId(mapper, 1);
8487 processPosition(mapper, 100, 200);
8488 processPressure(mapper, 0);
8489 processSync(mapper);
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8493 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8494
8495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8496 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8498 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8499
8500 // move a little
8501 processPosition(mapper, 150, 250);
8502 processSync(mapper);
8503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8504 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8506 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8507
8508 // down when pressure becomes non-zero
8509 processPressure(mapper, RAW_PRESSURE_MAX);
8510 processSync(mapper);
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8514 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8515
8516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8517 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8519 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8520
8521 // up when pressure becomes 0, hover restored
8522 processPressure(mapper, 0);
8523 processSync(mapper);
8524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8525 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8527 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8528
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8532 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8533
8534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8535 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8537 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8538
8539 // exit hover when pointer goes away
8540 processId(mapper, -1);
8541 processSync(mapper);
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8543 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8545 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8546}
8547
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008548/**
8549 * Set the input device port <--> display port associations, and check that the
8550 * events are routed to the display that matches the display port.
8551 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8552 */
8553TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008554 const std::string usb2 = "USB2";
8555 const uint8_t hdmi1 = 0;
8556 const uint8_t hdmi2 = 1;
8557 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008558 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008559
8560 addConfigurationProperty("touch.deviceType", "touchScreen");
8561 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008562 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008563
8564 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8565 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8566
8567 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8568 // for this input device is specified, and the matching viewport is not present,
8569 // the input device should be disabled (at the mapper level).
8570
8571 // Add viewport for display 2 on hdmi2
8572 prepareSecondaryDisplay(type, hdmi2);
8573 // Send a touch event
8574 processPosition(mapper, 100, 100);
8575 processSync(mapper);
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8577
8578 // Add viewport for display 1 on hdmi1
8579 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8580 // Send a touch event again
8581 processPosition(mapper, 100, 100);
8582 processSync(mapper);
8583
8584 NotifyMotionArgs args;
8585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8586 ASSERT_EQ(DISPLAY_ID, args.displayId);
8587}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008588
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008589TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8590 addConfigurationProperty("touch.deviceType", "touchScreen");
8591 prepareAxes(POSITION);
8592 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8593
8594 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8595
8596 prepareDisplay(DISPLAY_ORIENTATION_0);
8597 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8598
8599 // Send a touch event
8600 processPosition(mapper, 100, 100);
8601 processSync(mapper);
8602
8603 NotifyMotionArgs args;
8604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8605 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8606}
8607
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008608TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008609 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008610 std::shared_ptr<FakePointerController> fakePointerController =
8611 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008612 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008613 fakePointerController->setPosition(100, 200);
8614 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008615 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008616
Garfield Tan888a6a42020-01-09 11:39:16 -08008617 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008618 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008619
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008620 prepareDisplay(DISPLAY_ORIENTATION_0);
8621 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008622 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008623
8624 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008625 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008626
8627 NotifyMotionArgs motionArgs;
8628 processPosition(mapper, 100, 100);
8629 processSync(mapper);
8630
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8632 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8633 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8634}
8635
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008636/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008637 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8638 */
8639TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8640 addConfigurationProperty("touch.deviceType", "touchScreen");
8641 prepareAxes(POSITION);
8642 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8643
8644 prepareDisplay(DISPLAY_ORIENTATION_0);
8645 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8646 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8647 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8648 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8649
8650 NotifyMotionArgs args;
8651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8652 ASSERT_EQ(26, args.readTime);
8653
8654 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8655 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8656 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8657
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8659 ASSERT_EQ(33, args.readTime);
8660}
8661
8662/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008663 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8664 * events should not be delivered to the listener.
8665 */
8666TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8667 addConfigurationProperty("touch.deviceType", "touchScreen");
8668 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8669 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8670 ViewportType::INTERNAL);
8671 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8672 prepareAxes(POSITION);
8673 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8674
8675 NotifyMotionArgs motionArgs;
8676 processPosition(mapper, 100, 100);
8677 processSync(mapper);
8678
8679 mFakeListener->assertNotifyMotionWasNotCalled();
8680}
8681
Garfield Tanc734e4f2021-01-15 20:01:39 -08008682TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8683 addConfigurationProperty("touch.deviceType", "touchScreen");
8684 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8685 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8686 ViewportType::INTERNAL);
8687 std::optional<DisplayViewport> optionalDisplayViewport =
8688 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8689 ASSERT_TRUE(optionalDisplayViewport.has_value());
8690 DisplayViewport displayViewport = *optionalDisplayViewport;
8691
8692 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8693 prepareAxes(POSITION);
8694 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8695
8696 // Finger down
8697 int32_t x = 100, y = 100;
8698 processPosition(mapper, x, y);
8699 processSync(mapper);
8700
8701 NotifyMotionArgs motionArgs;
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8703 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8704
8705 // Deactivate display viewport
8706 displayViewport.isActive = false;
8707 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8708 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8709
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008710 // The ongoing touch should be canceled immediately
8711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8712 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8713
8714 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008715 x += 10, y += 10;
8716 processPosition(mapper, x, y);
8717 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008719
8720 // Reactivate display viewport
8721 displayViewport.isActive = true;
8722 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8723 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8724
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008725 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008726 x += 10, y += 10;
8727 processPosition(mapper, x, y);
8728 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8730 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008731}
8732
Arthur Hung7c645402019-01-25 17:45:42 +08008733TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8734 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008735 prepareAxes(POSITION | ID | SLOT);
8736 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008737 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008738
8739 // Create the second touch screen device, and enable multi fingers.
8740 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008741 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008742 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008743 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008744 std::shared_ptr<InputDevice> device2 =
8745 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008746 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008748 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8749 0 /*flat*/, 0 /*fuzz*/);
8750 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8751 0 /*flat*/, 0 /*fuzz*/);
8752 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8753 0 /*flat*/, 0 /*fuzz*/);
8754 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8755 0 /*flat*/, 0 /*fuzz*/);
8756 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8757 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8758 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008759
8760 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008761 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008762 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8763 device2->reset(ARBITRARY_TIME);
8764
8765 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008766 std::shared_ptr<FakePointerController> fakePointerController =
8767 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008768 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008769
8770 // Setup policy for associated displays and show touches.
8771 const uint8_t hdmi1 = 0;
8772 const uint8_t hdmi2 = 1;
8773 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8774 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8775 mFakePolicy->setShowTouches(true);
8776
8777 // Create displays.
8778 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008779 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008780
8781 // Default device will reconfigure above, need additional reconfiguration for another device.
8782 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008783 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008784
8785 // Two fingers down at default display.
8786 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8787 processPosition(mapper, x1, y1);
8788 processId(mapper, 1);
8789 processSlot(mapper, 1);
8790 processPosition(mapper, x2, y2);
8791 processId(mapper, 2);
8792 processSync(mapper);
8793
8794 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8795 fakePointerController->getSpots().find(DISPLAY_ID);
8796 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8797 ASSERT_EQ(size_t(2), iter->second.size());
8798
8799 // Two fingers down at second display.
8800 processPosition(mapper2, x1, y1);
8801 processId(mapper2, 1);
8802 processSlot(mapper2, 1);
8803 processPosition(mapper2, x2, y2);
8804 processId(mapper2, 2);
8805 processSync(mapper2);
8806
8807 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8808 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8809 ASSERT_EQ(size_t(2), iter->second.size());
8810}
8811
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008812TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008813 prepareAxes(POSITION);
8814 addConfigurationProperty("touch.deviceType", "touchScreen");
8815 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008816 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008817
8818 NotifyMotionArgs motionArgs;
8819 // Unrotated video frame
8820 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8821 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008822 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008823 processPosition(mapper, 100, 200);
8824 processSync(mapper);
8825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8826 ASSERT_EQ(frames, motionArgs.videoFrames);
8827
8828 // Subsequent touch events should not have any videoframes
8829 // This is implemented separately in FakeEventHub,
8830 // but that should match the behaviour of TouchVideoDevice.
8831 processPosition(mapper, 200, 200);
8832 processSync(mapper);
8833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8834 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8835}
8836
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008837TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008838 prepareAxes(POSITION);
8839 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008840 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008841 // Unrotated video frame
8842 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8843 NotifyMotionArgs motionArgs;
8844
8845 // Test all 4 orientations
8846 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008847 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8848 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8849 clearViewports();
8850 prepareDisplay(orientation);
8851 std::vector<TouchVideoFrame> frames{frame};
8852 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8853 processPosition(mapper, 100, 200);
8854 processSync(mapper);
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8856 ASSERT_EQ(frames, motionArgs.videoFrames);
8857 }
8858}
8859
8860TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8861 prepareAxes(POSITION);
8862 addConfigurationProperty("touch.deviceType", "touchScreen");
8863 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8864 // orientation-aware are affected by display rotation.
8865 addConfigurationProperty("touch.orientationAware", "0");
8866 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8867 // Unrotated video frame
8868 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8869 NotifyMotionArgs motionArgs;
8870
8871 // Test all 4 orientations
8872 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008873 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8874 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8875 clearViewports();
8876 prepareDisplay(orientation);
8877 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008878 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008879 processPosition(mapper, 100, 200);
8880 processSync(mapper);
8881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008882 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8883 // compared to the display. This is so that when the window transform (which contains the
8884 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8885 // window's coordinate space.
8886 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008887 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnane74b35f2022-07-19 16:00:50 +08008888
8889 // Release finger.
8890 processSync(mapper);
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008892 }
8893}
8894
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008895TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008896 prepareAxes(POSITION);
8897 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008898 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008899 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8900 // so mix these.
8901 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8902 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8903 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8904 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8905 NotifyMotionArgs motionArgs;
8906
8907 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008908 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008909 processPosition(mapper, 100, 200);
8910 processSync(mapper);
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008912 ASSERT_EQ(frames, motionArgs.videoFrames);
8913}
8914
8915TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8916 prepareAxes(POSITION);
8917 addConfigurationProperty("touch.deviceType", "touchScreen");
8918 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8919 // orientation-aware are affected by display rotation.
8920 addConfigurationProperty("touch.orientationAware", "0");
8921 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8922 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8923 // so mix these.
8924 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8925 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8926 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8927 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8928 NotifyMotionArgs motionArgs;
8929
8930 prepareDisplay(DISPLAY_ORIENTATION_90);
8931 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8932 processPosition(mapper, 100, 200);
8933 processSync(mapper);
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8935 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8936 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8937 // compared to the display. This is so that when the window transform (which contains the
8938 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8939 // window's coordinate space.
8940 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8941 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008942 ASSERT_EQ(frames, motionArgs.videoFrames);
8943}
8944
Arthur Hung9da14732019-09-02 16:16:58 +08008945/**
8946 * If we had defined port associations, but the viewport is not ready, the touch device would be
8947 * expected to be disabled, and it should be enabled after the viewport has found.
8948 */
8949TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008950 constexpr uint8_t hdmi2 = 1;
8951 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008952 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008953
8954 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8955
8956 addConfigurationProperty("touch.deviceType", "touchScreen");
8957 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008958 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008959
8960 ASSERT_EQ(mDevice->isEnabled(), false);
8961
8962 // Add display on hdmi2, the device should be enabled and can receive touch event.
8963 prepareSecondaryDisplay(type, hdmi2);
8964 ASSERT_EQ(mDevice->isEnabled(), true);
8965
8966 // Send a touch event.
8967 processPosition(mapper, 100, 100);
8968 processSync(mapper);
8969
8970 NotifyMotionArgs args;
8971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8972 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8973}
8974
Arthur Hung421eb1c2020-01-16 00:09:42 +08008975TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008976 addConfigurationProperty("touch.deviceType", "touchScreen");
8977 prepareDisplay(DISPLAY_ORIENTATION_0);
8978 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008979 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008980
8981 NotifyMotionArgs motionArgs;
8982
8983 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8984 // finger down
8985 processId(mapper, 1);
8986 processPosition(mapper, x1, y1);
8987 processSync(mapper);
8988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8991
8992 // finger move
8993 processId(mapper, 1);
8994 processPosition(mapper, x2, y2);
8995 processSync(mapper);
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8999
9000 // finger up.
9001 processId(mapper, -1);
9002 processSync(mapper);
9003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9004 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9006
9007 // new finger down
9008 processId(mapper, 1);
9009 processPosition(mapper, x3, y3);
9010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9012 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9014}
9015
9016/**
arthurhungcc7f9802020-04-30 17:55:40 +08009017 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9018 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009019 */
arthurhungcc7f9802020-04-30 17:55:40 +08009020TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009021 addConfigurationProperty("touch.deviceType", "touchScreen");
9022 prepareDisplay(DISPLAY_ORIENTATION_0);
9023 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009025
9026 NotifyMotionArgs motionArgs;
9027
9028 // default tool type is finger
9029 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009030 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009031 processPosition(mapper, x1, y1);
9032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9034 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9036
9037 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9038 processToolType(mapper, MT_TOOL_PALM);
9039 processSync(mapper);
9040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9041 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9042
9043 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009044 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009045 processPosition(mapper, x2, y2);
9046 processSync(mapper);
9047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9048
9049 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009050 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009051 processSync(mapper);
9052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9053
9054 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009055 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009056 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009057 processPosition(mapper, x3, y3);
9058 processSync(mapper);
9059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9060 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9062}
9063
arthurhungbf89a482020-04-17 17:37:55 +08009064/**
arthurhungcc7f9802020-04-30 17:55:40 +08009065 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9066 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009067 */
arthurhungcc7f9802020-04-30 17:55:40 +08009068TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009069 addConfigurationProperty("touch.deviceType", "touchScreen");
9070 prepareDisplay(DISPLAY_ORIENTATION_0);
9071 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9072 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9073
9074 NotifyMotionArgs motionArgs;
9075
9076 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009077 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9078 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009079 processPosition(mapper, x1, y1);
9080 processSync(mapper);
9081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9082 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9084
9085 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009086 processSlot(mapper, SECOND_SLOT);
9087 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009088 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009091 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9093
9094 // If the tool type of the first finger changes to MT_TOOL_PALM,
9095 // we expect to receive ACTION_POINTER_UP with cancel flag.
9096 processSlot(mapper, FIRST_SLOT);
9097 processId(mapper, FIRST_TRACKING_ID);
9098 processToolType(mapper, MT_TOOL_PALM);
9099 processSync(mapper);
9100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009101 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009102 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9103
9104 // The following MOVE events of second finger should be processed.
9105 processSlot(mapper, SECOND_SLOT);
9106 processId(mapper, SECOND_TRACKING_ID);
9107 processPosition(mapper, x2 + 1, y2 + 1);
9108 processSync(mapper);
9109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9111 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9112
9113 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9114 // it. Second finger receive move.
9115 processSlot(mapper, FIRST_SLOT);
9116 processId(mapper, INVALID_TRACKING_ID);
9117 processSync(mapper);
9118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9120 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9121
9122 // Second finger keeps moving.
9123 processSlot(mapper, SECOND_SLOT);
9124 processId(mapper, SECOND_TRACKING_ID);
9125 processPosition(mapper, x2 + 2, y2 + 2);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9129 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9130
9131 // Second finger up.
9132 processId(mapper, INVALID_TRACKING_ID);
9133 processSync(mapper);
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9135 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9136 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9137}
9138
9139/**
9140 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9141 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9142 */
9143TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9144 addConfigurationProperty("touch.deviceType", "touchScreen");
9145 prepareDisplay(DISPLAY_ORIENTATION_0);
9146 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9147 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9148
9149 NotifyMotionArgs motionArgs;
9150
9151 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9152 // First finger down.
9153 processId(mapper, FIRST_TRACKING_ID);
9154 processPosition(mapper, x1, y1);
9155 processSync(mapper);
9156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9157 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9159
9160 // Second finger down.
9161 processSlot(mapper, SECOND_SLOT);
9162 processId(mapper, SECOND_TRACKING_ID);
9163 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009164 processSync(mapper);
9165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009166 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9168
arthurhungcc7f9802020-04-30 17:55:40 +08009169 // If the tool type of the first finger changes to MT_TOOL_PALM,
9170 // we expect to receive ACTION_POINTER_UP with cancel flag.
9171 processSlot(mapper, FIRST_SLOT);
9172 processId(mapper, FIRST_TRACKING_ID);
9173 processToolType(mapper, MT_TOOL_PALM);
9174 processSync(mapper);
9175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009176 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009177 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9178
9179 // Second finger keeps moving.
9180 processSlot(mapper, SECOND_SLOT);
9181 processId(mapper, SECOND_TRACKING_ID);
9182 processPosition(mapper, x2 + 1, y2 + 1);
9183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9186
9187 // second finger becomes palm, receive cancel due to only 1 finger is active.
9188 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009189 processToolType(mapper, MT_TOOL_PALM);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9192 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9193
arthurhungcc7f9802020-04-30 17:55:40 +08009194 // third finger down.
9195 processSlot(mapper, THIRD_SLOT);
9196 processId(mapper, THIRD_TRACKING_ID);
9197 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009198 processPosition(mapper, x3, y3);
9199 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9201 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9202 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009203 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9204
9205 // third finger move
9206 processId(mapper, THIRD_TRACKING_ID);
9207 processPosition(mapper, x3 + 1, y3 + 1);
9208 processSync(mapper);
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9211
9212 // first finger up, third finger receive move.
9213 processSlot(mapper, FIRST_SLOT);
9214 processId(mapper, INVALID_TRACKING_ID);
9215 processSync(mapper);
9216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9218 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9219
9220 // second finger up, third finger receive move.
9221 processSlot(mapper, SECOND_SLOT);
9222 processId(mapper, INVALID_TRACKING_ID);
9223 processSync(mapper);
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9226 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9227
9228 // third finger up.
9229 processSlot(mapper, THIRD_SLOT);
9230 processId(mapper, INVALID_TRACKING_ID);
9231 processSync(mapper);
9232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9233 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9234 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9235}
9236
9237/**
9238 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9239 * and the active finger could still be allowed to receive the events
9240 */
9241TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9242 addConfigurationProperty("touch.deviceType", "touchScreen");
9243 prepareDisplay(DISPLAY_ORIENTATION_0);
9244 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9245 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9246
9247 NotifyMotionArgs motionArgs;
9248
9249 // default tool type is finger
9250 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9251 processId(mapper, FIRST_TRACKING_ID);
9252 processPosition(mapper, x1, y1);
9253 processSync(mapper);
9254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9255 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9257
9258 // Second finger down.
9259 processSlot(mapper, SECOND_SLOT);
9260 processId(mapper, SECOND_TRACKING_ID);
9261 processPosition(mapper, x2, y2);
9262 processSync(mapper);
9263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009264 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009265 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9266
9267 // If the tool type of the second finger changes to MT_TOOL_PALM,
9268 // we expect to receive ACTION_POINTER_UP with cancel flag.
9269 processId(mapper, SECOND_TRACKING_ID);
9270 processToolType(mapper, MT_TOOL_PALM);
9271 processSync(mapper);
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009273 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009274 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9275
9276 // The following MOVE event should be processed.
9277 processSlot(mapper, FIRST_SLOT);
9278 processId(mapper, FIRST_TRACKING_ID);
9279 processPosition(mapper, x1 + 1, y1 + 1);
9280 processSync(mapper);
9281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9283 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9284
9285 // second finger up.
9286 processSlot(mapper, SECOND_SLOT);
9287 processId(mapper, INVALID_TRACKING_ID);
9288 processSync(mapper);
9289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9291
9292 // first finger keep moving
9293 processSlot(mapper, FIRST_SLOT);
9294 processId(mapper, FIRST_TRACKING_ID);
9295 processPosition(mapper, x1 + 2, y1 + 2);
9296 processSync(mapper);
9297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9299
9300 // first finger up.
9301 processId(mapper, INVALID_TRACKING_ID);
9302 processSync(mapper);
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9304 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9305 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009306}
9307
Arthur Hung9ad18942021-06-19 02:04:46 +00009308/**
9309 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9310 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9311 * cause slot be valid again.
9312 */
9313TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9314 addConfigurationProperty("touch.deviceType", "touchScreen");
9315 prepareDisplay(DISPLAY_ORIENTATION_0);
9316 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9317 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9318
9319 NotifyMotionArgs motionArgs;
9320
9321 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9322 // First finger down.
9323 processId(mapper, FIRST_TRACKING_ID);
9324 processPosition(mapper, x1, y1);
9325 processPressure(mapper, RAW_PRESSURE_MAX);
9326 processSync(mapper);
9327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9328 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9329 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9330
9331 // First finger move.
9332 processId(mapper, FIRST_TRACKING_ID);
9333 processPosition(mapper, x1 + 1, y1 + 1);
9334 processPressure(mapper, RAW_PRESSURE_MAX);
9335 processSync(mapper);
9336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9338 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9339
9340 // Second finger down.
9341 processSlot(mapper, SECOND_SLOT);
9342 processId(mapper, SECOND_TRACKING_ID);
9343 processPosition(mapper, x2, y2);
9344 processPressure(mapper, RAW_PRESSURE_MAX);
9345 processSync(mapper);
9346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009347 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009348 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9349
9350 // second finger up with some unexpected data.
9351 processSlot(mapper, SECOND_SLOT);
9352 processId(mapper, INVALID_TRACKING_ID);
9353 processPosition(mapper, x2, y2);
9354 processSync(mapper);
9355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009356 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009357 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9358
9359 // first finger up with some unexpected data.
9360 processSlot(mapper, FIRST_SLOT);
9361 processId(mapper, INVALID_TRACKING_ID);
9362 processPosition(mapper, x2, y2);
9363 processPressure(mapper, RAW_PRESSURE_MAX);
9364 processSync(mapper);
9365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9366 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9367 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9368}
9369
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009370// --- MultiTouchInputMapperTest_ExternalDevice ---
9371
9372class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9373protected:
Chris Yea52ade12020-08-27 16:49:20 -07009374 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009375};
9376
9377/**
9378 * Expect fallback to internal viewport if device is external and external viewport is not present.
9379 */
9380TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9381 prepareAxes(POSITION);
9382 addConfigurationProperty("touch.deviceType", "touchScreen");
9383 prepareDisplay(DISPLAY_ORIENTATION_0);
9384 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9385
9386 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9387
9388 NotifyMotionArgs motionArgs;
9389
9390 // Expect the event to be sent to the internal viewport,
9391 // because an external viewport is not present.
9392 processPosition(mapper, 100, 100);
9393 processSync(mapper);
9394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9395 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9396
9397 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009398 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009399 processPosition(mapper, 100, 100);
9400 processSync(mapper);
9401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9402 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9403}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009404
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009405TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9406 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9407 std::shared_ptr<FakePointerController> fakePointerController =
9408 std::make_shared<FakePointerController>();
9409 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9410 fakePointerController->setPosition(0, 0);
9411 fakePointerController->setButtonState(0);
9412
9413 // prepare device and capture
9414 prepareDisplay(DISPLAY_ORIENTATION_0);
9415 prepareAxes(POSITION | ID | SLOT);
9416 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9417 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9418 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009419 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009420 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9421
9422 // captured touchpad should be a touchpad source
9423 NotifyDeviceResetArgs resetArgs;
9424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9425 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9426
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009427 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009428
9429 const InputDeviceInfo::MotionRange* relRangeX =
9430 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9431 ASSERT_NE(relRangeX, nullptr);
9432 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9433 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9434 const InputDeviceInfo::MotionRange* relRangeY =
9435 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9436 ASSERT_NE(relRangeY, nullptr);
9437 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9438 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9439
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009440 // run captured pointer tests - note that this is unscaled, so input listener events should be
9441 // identical to what the hardware sends (accounting for any
9442 // calibration).
9443 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009444 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009445 processId(mapper, 1);
9446 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9447 processKey(mapper, BTN_TOUCH, 1);
9448 processSync(mapper);
9449
9450 // expect coord[0] to contain initial location of touch 0
9451 NotifyMotionArgs args;
9452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9454 ASSERT_EQ(1U, args.pointerCount);
9455 ASSERT_EQ(0, args.pointerProperties[0].id);
9456 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9457 ASSERT_NO_FATAL_FAILURE(
9458 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9459
9460 // FINGER 1 DOWN
9461 processSlot(mapper, 1);
9462 processId(mapper, 2);
9463 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9464 processSync(mapper);
9465
9466 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009468 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009469 ASSERT_EQ(2U, args.pointerCount);
9470 ASSERT_EQ(0, args.pointerProperties[0].id);
9471 ASSERT_EQ(1, args.pointerProperties[1].id);
9472 ASSERT_NO_FATAL_FAILURE(
9473 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9474 ASSERT_NO_FATAL_FAILURE(
9475 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9476
9477 // FINGER 1 MOVE
9478 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9479 processSync(mapper);
9480
9481 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9482 // from move
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9485 ASSERT_NO_FATAL_FAILURE(
9486 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9487 ASSERT_NO_FATAL_FAILURE(
9488 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9489
9490 // FINGER 0 MOVE
9491 processSlot(mapper, 0);
9492 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9493 processSync(mapper);
9494
9495 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9498 ASSERT_NO_FATAL_FAILURE(
9499 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9500 ASSERT_NO_FATAL_FAILURE(
9501 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9502
9503 // BUTTON DOWN
9504 processKey(mapper, BTN_LEFT, 1);
9505 processSync(mapper);
9506
9507 // touchinputmapper design sends a move before button press
9508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9511 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9512
9513 // BUTTON UP
9514 processKey(mapper, BTN_LEFT, 0);
9515 processSync(mapper);
9516
9517 // touchinputmapper design sends a move after button release
9518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9519 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9522
9523 // FINGER 0 UP
9524 processId(mapper, -1);
9525 processSync(mapper);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9527 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9528
9529 // FINGER 1 MOVE
9530 processSlot(mapper, 1);
9531 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9532 processSync(mapper);
9533
9534 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9537 ASSERT_EQ(1U, args.pointerCount);
9538 ASSERT_EQ(1, args.pointerProperties[0].id);
9539 ASSERT_NO_FATAL_FAILURE(
9540 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9541
9542 // FINGER 1 UP
9543 processId(mapper, -1);
9544 processKey(mapper, BTN_TOUCH, 0);
9545 processSync(mapper);
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9547 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9548
9549 // non captured touchpad should be a mouse source
9550 mFakePolicy->setPointerCapture(false);
9551 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9553 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9554}
9555
9556TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9557 std::shared_ptr<FakePointerController> fakePointerController =
9558 std::make_shared<FakePointerController>();
9559 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9560 fakePointerController->setPosition(0, 0);
9561 fakePointerController->setButtonState(0);
9562
9563 // prepare device and capture
9564 prepareDisplay(DISPLAY_ORIENTATION_0);
9565 prepareAxes(POSITION | ID | SLOT);
9566 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9567 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009568 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009569 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9570 // run uncaptured pointer tests - pushes out generic events
9571 // FINGER 0 DOWN
9572 processId(mapper, 3);
9573 processPosition(mapper, 100, 100);
9574 processKey(mapper, BTN_TOUCH, 1);
9575 processSync(mapper);
9576
9577 // start at (100,100), cursor should be at (0,0) * scale
9578 NotifyMotionArgs args;
9579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9580 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9581 ASSERT_NO_FATAL_FAILURE(
9582 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9583
9584 // FINGER 0 MOVE
9585 processPosition(mapper, 200, 200);
9586 processSync(mapper);
9587
9588 // compute scaling to help with touch position checking
9589 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9590 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9591 float scale =
9592 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9593
9594 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9598 0, 0, 0, 0, 0, 0, 0));
9599}
9600
9601TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9602 std::shared_ptr<FakePointerController> fakePointerController =
9603 std::make_shared<FakePointerController>();
9604
9605 prepareDisplay(DISPLAY_ORIENTATION_0);
9606 prepareAxes(POSITION | ID | SLOT);
9607 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009608 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009609 mFakePolicy->setPointerCapture(false);
9610 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9611
9612 // uncaptured touchpad should be a pointer device
9613 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9614
9615 // captured touchpad should be a touchpad device
9616 mFakePolicy->setPointerCapture(true);
9617 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9618 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9619}
9620
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009621// --- JoystickInputMapperTest ---
9622
9623class JoystickInputMapperTest : public InputMapperTest {
9624protected:
9625 static const int32_t RAW_X_MIN;
9626 static const int32_t RAW_X_MAX;
9627 static const int32_t RAW_Y_MIN;
9628 static const int32_t RAW_Y_MAX;
9629
9630 void SetUp() override {
9631 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9632 }
9633 void prepareAxes() {
9634 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9635 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9636 }
9637
9638 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9640 }
9641
9642 void processSync(JoystickInputMapper& mapper) {
9643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9644 }
9645
9646 void prepareVirtualDisplay(int32_t orientation) {
9647 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9648 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9649 NO_PORT, ViewportType::VIRTUAL);
9650 }
9651};
9652
9653const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9654const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9655const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9656const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9657
9658TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9659 prepareAxes();
9660 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9661
9662 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9663
9664 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9665
9666 // Send an axis event
9667 processAxis(mapper, ABS_X, 100);
9668 processSync(mapper);
9669
9670 NotifyMotionArgs args;
9671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9672 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9673
9674 // Send another axis event
9675 processAxis(mapper, ABS_Y, 100);
9676 processSync(mapper);
9677
9678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9679 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9680}
9681
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009682// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009683
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009684class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009685protected:
9686 static const char* DEVICE_NAME;
9687 static const char* DEVICE_LOCATION;
9688 static const int32_t DEVICE_ID;
9689 static const int32_t DEVICE_GENERATION;
9690 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009691 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009692 static const int32_t EVENTHUB_ID;
9693
9694 std::shared_ptr<FakeEventHub> mFakeEventHub;
9695 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009696 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009697 std::unique_ptr<InstrumentedInputReader> mReader;
9698 std::shared_ptr<InputDevice> mDevice;
9699
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009700 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009701 mFakeEventHub = std::make_unique<FakeEventHub>();
9702 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009703 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009704 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009705 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009706 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9707 }
9708
9709 void SetUp() override { SetUp(DEVICE_CLASSES); }
9710
9711 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009712 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009713 mFakePolicy.clear();
9714 }
9715
9716 void configureDevice(uint32_t changes) {
9717 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9718 mReader->requestRefreshConfiguration(changes);
9719 mReader->loopOnce();
9720 }
9721 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9722 }
9723
9724 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9725 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009726 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009727 InputDeviceIdentifier identifier;
9728 identifier.name = name;
9729 identifier.location = location;
9730 std::shared_ptr<InputDevice> device =
9731 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9732 identifier);
9733 mReader->pushNextDevice(device);
9734 mFakeEventHub->addDevice(eventHubId, name, classes);
9735 mReader->loopOnce();
9736 return device;
9737 }
9738
9739 template <class T, typename... Args>
9740 T& addControllerAndConfigure(Args... args) {
9741 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9742
9743 return controller;
9744 }
9745};
9746
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009747const char* PeripheralControllerTest::DEVICE_NAME = "device";
9748const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9749const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9750const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9751const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009752const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9753 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009754const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009755
9756// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009757class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009758protected:
9759 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009760 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009761 }
9762};
9763
9764TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009765 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009766
9767 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9768 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9769}
9770
9771TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009772 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009773
9774 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9775 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9776}
9777
9778// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009779class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009780protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009781 void SetUp() override {
9782 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9783 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009784};
9785
Chris Ye85758332021-05-16 23:05:17 -07009786TEST_F(LightControllerTest, MonoLight) {
9787 RawLightInfo infoMono = {.id = 1,
9788 .name = "Mono",
9789 .maxBrightness = 255,
9790 .flags = InputLightClass::BRIGHTNESS,
9791 .path = ""};
9792 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009793
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009794 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009795 InputDeviceInfo info;
9796 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009797 std::vector<InputDeviceLightInfo> lights = info.getLights();
9798 ASSERT_EQ(1U, lights.size());
9799 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009800
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009801 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9802 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009803}
9804
9805TEST_F(LightControllerTest, RGBLight) {
9806 RawLightInfo infoRed = {.id = 1,
9807 .name = "red",
9808 .maxBrightness = 255,
9809 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9810 .path = ""};
9811 RawLightInfo infoGreen = {.id = 2,
9812 .name = "green",
9813 .maxBrightness = 255,
9814 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9815 .path = ""};
9816 RawLightInfo infoBlue = {.id = 3,
9817 .name = "blue",
9818 .maxBrightness = 255,
9819 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9820 .path = ""};
9821 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9822 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9823 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9824
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009825 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009826 InputDeviceInfo info;
9827 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009828 std::vector<InputDeviceLightInfo> lights = info.getLights();
9829 ASSERT_EQ(1U, lights.size());
9830 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009831
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009832 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9833 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009834}
9835
9836TEST_F(LightControllerTest, MultiColorRGBLight) {
9837 RawLightInfo infoColor = {.id = 1,
9838 .name = "red",
9839 .maxBrightness = 255,
9840 .flags = InputLightClass::BRIGHTNESS |
9841 InputLightClass::MULTI_INTENSITY |
9842 InputLightClass::MULTI_INDEX,
9843 .path = ""};
9844
9845 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9846
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009847 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009848 InputDeviceInfo info;
9849 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009850 std::vector<InputDeviceLightInfo> lights = info.getLights();
9851 ASSERT_EQ(1U, lights.size());
9852 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009853
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009854 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9855 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009856}
9857
9858TEST_F(LightControllerTest, PlayerIdLight) {
9859 RawLightInfo info1 = {.id = 1,
9860 .name = "player1",
9861 .maxBrightness = 255,
9862 .flags = InputLightClass::BRIGHTNESS,
9863 .path = ""};
9864 RawLightInfo info2 = {.id = 2,
9865 .name = "player2",
9866 .maxBrightness = 255,
9867 .flags = InputLightClass::BRIGHTNESS,
9868 .path = ""};
9869 RawLightInfo info3 = {.id = 3,
9870 .name = "player3",
9871 .maxBrightness = 255,
9872 .flags = InputLightClass::BRIGHTNESS,
9873 .path = ""};
9874 RawLightInfo info4 = {.id = 4,
9875 .name = "player4",
9876 .maxBrightness = 255,
9877 .flags = InputLightClass::BRIGHTNESS,
9878 .path = ""};
9879 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9880 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9881 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9882 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9883
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009884 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009885 InputDeviceInfo info;
9886 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009887 std::vector<InputDeviceLightInfo> lights = info.getLights();
9888 ASSERT_EQ(1U, lights.size());
9889 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009890
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009891 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9892 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9893 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009894}
9895
Michael Wrightd02c5b62014-02-10 15:10:22 -08009896} // namespace android