blob: 03fbf07b167afcf413a1b2d03867870b135ea367 [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
Prabir Pradhan588d6392022-11-02 20:05:13 +0000114MATCHER_P(WithToolType, toolType, "InputEvent with specified tool type") {
115 const auto argToolType = arg.pointerProperties[0].toolType;
116 *result_listener << "expected tool type " << motionToolTypeToString(toolType) << ", but got "
117 << motionToolTypeToString(argToolType);
118 return argToolType == toolType;
119}
120
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121template<typename T>
122static inline T min(T a, T b) {
123 return a < b ? a : b;
124}
125
126static inline float avg(float x, float y) {
127 return (x + y) / 2;
128}
129
Chris Ye3fdbfef2021-01-06 18:45:18 -0800130// Mapping for light color name and the light color
131const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
132 {"green", LightColor::GREEN},
133 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700135static int32_t getInverseRotation(int32_t orientation) {
136 switch (orientation) {
137 case DISPLAY_ORIENTATION_90:
138 return DISPLAY_ORIENTATION_270;
139 case DISPLAY_ORIENTATION_270:
140 return DISPLAY_ORIENTATION_90;
141 default:
142 return orientation;
143 }
144}
145
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800146static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
147 InputDeviceInfo info;
148 mapper.populateDeviceInfo(&info);
149
150 const InputDeviceInfo::MotionRange* motionRange =
151 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
152 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
153}
154
155static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
156 InputDeviceInfo info;
157 mapper.populateDeviceInfo(&info);
158
159 const InputDeviceInfo::MotionRange* motionRange =
160 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
161 ASSERT_EQ(nullptr, motionRange);
162}
163
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164// --- FakePointerController ---
165
166class FakePointerController : public PointerControllerInterface {
167 bool mHaveBounds;
168 float mMinX, mMinY, mMaxX, mMaxY;
169 float mX, mY;
170 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800171 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173public:
174 FakePointerController() :
175 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800176 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177 }
178
Michael Wright17db18e2020-06-26 20:51:44 +0100179 virtual ~FakePointerController() {}
180
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 void setBounds(float minX, float minY, float maxX, float maxY) {
182 mHaveBounds = true;
183 mMinX = minX;
184 mMinY = minY;
185 mMaxX = maxX;
186 mMaxY = maxY;
187 }
188
Chris Yea52ade12020-08-27 16:49:20 -0700189 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190 mX = x;
191 mY = y;
192 }
193
Chris Yea52ade12020-08-27 16:49:20 -0700194 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195
Chris Yea52ade12020-08-27 16:49:20 -0700196 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800197
Chris Yea52ade12020-08-27 16:49:20 -0700198 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199 *outX = mX;
200 *outY = mY;
201 }
202
Chris Yea52ade12020-08-27 16:49:20 -0700203 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800204
Chris Yea52ade12020-08-27 16:49:20 -0700205 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800206 mDisplayId = viewport.displayId;
207 }
208
Arthur Hung7c645402019-01-25 17:45:42 +0800209 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
210 return mSpotsByDisplay;
211 }
212
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213private:
Chris Yea52ade12020-08-27 16:49:20 -0700214 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215 *outMinX = mMinX;
216 *outMinY = mMinY;
217 *outMaxX = mMaxX;
218 *outMaxY = mMaxY;
219 return mHaveBounds;
220 }
221
Chris Yea52ade12020-08-27 16:49:20 -0700222 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223 mX += deltaX;
224 if (mX < mMinX) mX = mMinX;
225 if (mX > mMaxX) mX = mMaxX;
226 mY += deltaY;
227 if (mY < mMinY) mY = mMinY;
228 if (mY > mMaxY) mY = mMaxY;
229 }
230
Chris Yea52ade12020-08-27 16:49:20 -0700231 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232
Chris Yea52ade12020-08-27 16:49:20 -0700233 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234
Chris Yea52ade12020-08-27 16:49:20 -0700235 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800236
Chris Yea52ade12020-08-27 16:49:20 -0700237 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
238 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800239 std::vector<int32_t> newSpots;
240 // Add spots for fingers that are down.
241 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
242 uint32_t id = idBits.clearFirstMarkedBit();
243 newSpots.push_back(id);
244 }
245
246 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247 }
248
Prabir Pradhan197e0862022-07-01 14:28:00 +0000249 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800250
251 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252};
253
254
255// --- FakeInputReaderPolicy ---
256
257class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700258 std::mutex mLock;
259 std::condition_variable mDevicesChangedCondition;
260
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000262 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700263 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
264 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700266 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267
268protected:
Chris Yea52ade12020-08-27 16:49:20 -0700269 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270
271public:
272 FakeInputReaderPolicy() {
273 }
274
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700275 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800276 waitForInputDevices([](bool devicesChanged) {
277 if (!devicesChanged) {
278 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
279 }
280 });
281 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700282
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800283 void assertInputDevicesNotChanged() {
284 waitForInputDevices([](bool devicesChanged) {
285 if (devicesChanged) {
286 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
287 }
288 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700289 }
290
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700291 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100292 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100293 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700294 }
295
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700296 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
297 return mConfig.getDisplayViewportByUniqueId(uniqueId);
298 }
299 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
300 return mConfig.getDisplayViewportByType(type);
301 }
302
303 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
304 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700305 }
306
Prabir Pradhan5632d622021-09-06 07:57:20 -0700307 void addDisplayViewport(DisplayViewport viewport) {
308 mViewports.push_back(std::move(viewport));
309 mConfig.setDisplayViewports(mViewports);
310 }
311
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700312 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000313 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700314 std::optional<uint8_t> physicalPort, ViewportType type) {
315 const bool isRotated =
316 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
317 DisplayViewport v;
318 v.displayId = displayId;
319 v.orientation = orientation;
320 v.logicalLeft = 0;
321 v.logicalTop = 0;
322 v.logicalRight = isRotated ? height : width;
323 v.logicalBottom = isRotated ? width : height;
324 v.physicalLeft = 0;
325 v.physicalTop = 0;
326 v.physicalRight = isRotated ? height : width;
327 v.physicalBottom = isRotated ? width : height;
328 v.deviceWidth = isRotated ? height : width;
329 v.deviceHeight = isRotated ? width : height;
330 v.isActive = isActive;
331 v.uniqueId = uniqueId;
332 v.physicalPort = physicalPort;
333 v.type = type;
334
335 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 }
337
Arthur Hung6cd19a42019-08-30 19:04:12 +0800338 bool updateViewport(const DisplayViewport& viewport) {
339 size_t count = mViewports.size();
340 for (size_t i = 0; i < count; i++) {
341 const DisplayViewport& currentViewport = mViewports[i];
342 if (currentViewport.displayId == viewport.displayId) {
343 mViewports[i] = viewport;
344 mConfig.setDisplayViewports(mViewports);
345 return true;
346 }
347 }
348 // no viewport found.
349 return false;
350 }
351
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100352 void addExcludedDeviceName(const std::string& deviceName) {
353 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354 }
355
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700356 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
357 mConfig.portAssociations.insert({inputPort, displayPort});
358 }
359
Christine Franks1ba71cc2021-04-07 14:37:42 -0700360 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
361 const std::string& displayUniqueId) {
362 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
363 }
364
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000365 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700366
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000367 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700368
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000369 void setPointerController(std::shared_ptr<FakePointerController> controller) {
370 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 }
372
373 const InputReaderConfiguration* getReaderConfiguration() const {
374 return &mConfig;
375 }
376
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800377 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378 return mInputDevices;
379 }
380
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100381 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700382 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700383 return transform;
384 }
385
386 void setTouchAffineTransformation(const TouchAffineTransformation t) {
387 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800388 }
389
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000390 PointerCaptureRequest setPointerCapture(bool enabled) {
391 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
392 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800393 }
394
Arthur Hung7c645402019-01-25 17:45:42 +0800395 void setShowTouches(bool enabled) {
396 mConfig.showTouches = enabled;
397 }
398
Garfield Tan888a6a42020-01-09 11:39:16 -0800399 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
400 mConfig.defaultPointerDisplayId = pointerDisplayId;
401 }
402
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800403 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
404
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000405 void setVelocityControlParams(const VelocityControlParameters& params) {
406 mConfig.pointerVelocityControlParameters = params;
407 mConfig.wheelVelocityControlParameters = params;
408 }
409
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000411 uint32_t mNextPointerCaptureSequenceNumber = 0;
412
Chris Yea52ade12020-08-27 16:49:20 -0700413 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414 *outConfig = mConfig;
415 }
416
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000417 std::shared_ptr<PointerControllerInterface> obtainPointerController(
418 int32_t /*deviceId*/) override {
419 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 }
421
Chris Yea52ade12020-08-27 16:49:20 -0700422 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700423 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700425 mInputDevicesChanged = true;
426 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427 }
428
Chris Yea52ade12020-08-27 16:49:20 -0700429 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
430 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700431 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 }
433
Chris Yea52ade12020-08-27 16:49:20 -0700434 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800435
436 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
437 std::unique_lock<std::mutex> lock(mLock);
438 base::ScopedLockAssertion assumeLocked(mLock);
439
440 const bool devicesChanged =
441 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
442 return mInputDevicesChanged;
443 });
444 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
445 mInputDevicesChanged = false;
446 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447};
448
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449// --- FakeEventHub ---
450
451class FakeEventHub : public EventHubInterface {
452 struct KeyInfo {
453 int32_t keyCode;
454 uint32_t flags;
455 };
456
Chris Yef59a2f42020-10-16 12:55:26 -0700457 struct SensorInfo {
458 InputDeviceSensorType sensorType;
459 int32_t sensorDataIndex;
460 };
461
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462 struct Device {
463 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700464 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465 PropertyMap configuration;
466 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
467 KeyedVector<int, bool> relativeAxes;
468 KeyedVector<int32_t, int32_t> keyCodeStates;
469 KeyedVector<int32_t, int32_t> scanCodeStates;
470 KeyedVector<int32_t, int32_t> switchStates;
471 KeyedVector<int32_t, int32_t> absoluteAxisValue;
472 KeyedVector<int32_t, KeyInfo> keysByScanCode;
473 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
474 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100475 // fake mapping which would normally come from keyCharacterMap
476 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700477 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
478 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800479 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700480 bool enabled;
481
482 status_t enable() {
483 enabled = true;
484 return OK;
485 }
486
487 status_t disable() {
488 enabled = false;
489 return OK;
490 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700492 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493 };
494
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700495 std::mutex mLock;
496 std::condition_variable mEventsCondition;
497
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100499 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000500 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600501 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000502 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800503 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
504 // Simulates a device light brightness, from light id to light brightness.
505 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
506 // Simulates a device light intensities, from light id to light intensities map.
507 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
508 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700510public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511 virtual ~FakeEventHub() {
512 for (size_t i = 0; i < mDevices.size(); i++) {
513 delete mDevices.valueAt(i);
514 }
515 }
516
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 FakeEventHub() { }
518
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700519 void addDevice(int32_t deviceId, const std::string& name,
520 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800521 Device* device = new Device(classes);
522 device->identifier.name = name;
523 mDevices.add(deviceId, device);
524
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000525 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526 }
527
528 void removeDevice(int32_t deviceId) {
529 delete mDevices.valueFor(deviceId);
530 mDevices.removeItem(deviceId);
531
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000532 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533 }
534
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700535 bool isDeviceEnabled(int32_t deviceId) {
536 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700537 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700538 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
539 return false;
540 }
541 return device->enabled;
542 }
543
544 status_t enableDevice(int32_t deviceId) {
545 status_t result;
546 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700547 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700548 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
549 return BAD_VALUE;
550 }
551 if (device->enabled) {
552 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
553 return OK;
554 }
555 result = device->enable();
556 return result;
557 }
558
559 status_t disableDevice(int32_t deviceId) {
560 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700561 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700562 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
563 return BAD_VALUE;
564 }
565 if (!device->enabled) {
566 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
567 return OK;
568 }
569 return device->disable();
570 }
571
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000573 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 }
575
576 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
577 Device* device = getDevice(deviceId);
578 device->configuration.addProperty(key, value);
579 }
580
581 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
582 Device* device = getDevice(deviceId);
583 device->configuration.addAll(configuration);
584 }
585
586 void addAbsoluteAxis(int32_t deviceId, int axis,
587 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
588 Device* device = getDevice(deviceId);
589
590 RawAbsoluteAxisInfo info;
591 info.valid = true;
592 info.minValue = minValue;
593 info.maxValue = maxValue;
594 info.flat = flat;
595 info.fuzz = fuzz;
596 info.resolution = resolution;
597 device->absoluteAxes.add(axis, info);
598 }
599
600 void addRelativeAxis(int32_t deviceId, int32_t axis) {
601 Device* device = getDevice(deviceId);
602 device->relativeAxes.add(axis, true);
603 }
604
605 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
606 Device* device = getDevice(deviceId);
607 device->keyCodeStates.replaceValueFor(keyCode, state);
608 }
609
610 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
611 Device* device = getDevice(deviceId);
612 device->scanCodeStates.replaceValueFor(scanCode, state);
613 }
614
615 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
616 Device* device = getDevice(deviceId);
617 device->switchStates.replaceValueFor(switchCode, state);
618 }
619
620 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
621 Device* device = getDevice(deviceId);
622 device->absoluteAxisValue.replaceValueFor(axis, value);
623 }
624
625 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
626 int32_t keyCode, uint32_t flags) {
627 Device* device = getDevice(deviceId);
628 KeyInfo info;
629 info.keyCode = keyCode;
630 info.flags = flags;
631 if (scanCode) {
632 device->keysByScanCode.add(scanCode, info);
633 }
634 if (usageCode) {
635 device->keysByUsageCode.add(usageCode, info);
636 }
637 }
638
Philip Junker4af3b3d2021-12-14 10:36:55 +0100639 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
640 Device* device = getDevice(deviceId);
641 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
642 }
643
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644 void addLed(int32_t deviceId, int32_t led, bool initialState) {
645 Device* device = getDevice(deviceId);
646 device->leds.add(led, initialState);
647 }
648
Chris Yef59a2f42020-10-16 12:55:26 -0700649 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
650 int32_t sensorDataIndex) {
651 Device* device = getDevice(deviceId);
652 SensorInfo info;
653 info.sensorType = sensorType;
654 info.sensorDataIndex = sensorDataIndex;
655 device->sensorsByAbsCode.emplace(absCode, info);
656 }
657
658 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
659 Device* device = getDevice(deviceId);
660 typename BitArray<MSC_MAX>::Buffer buffer;
661 buffer[mscEvent / 32] = 1 << mscEvent % 32;
662 device->mscBitmask.loadFromBuffer(buffer);
663 }
664
Chris Ye3fdbfef2021-01-06 18:45:18 -0800665 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
666 mRawLightInfos.emplace(rawId, std::move(info));
667 }
668
669 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
670 mLightBrightness.emplace(rawId, brightness);
671 }
672
673 void fakeLightIntensities(int32_t rawId,
674 const std::unordered_map<LightColor, int32_t> intensities) {
675 mLightIntensities.emplace(rawId, std::move(intensities));
676 }
677
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 bool getLedState(int32_t deviceId, int32_t led) {
679 Device* device = getDevice(deviceId);
680 return device->leds.valueFor(led);
681 }
682
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100683 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 return mExcludedDevices;
685 }
686
687 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
688 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800689 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800690 }
691
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000692 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
693 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700694 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800695 RawEvent event;
696 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000697 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800698 event.deviceId = deviceId;
699 event.type = type;
700 event.code = code;
701 event.value = value;
702 mEvents.push_back(event);
703
704 if (type == EV_ABS) {
705 setAbsoluteAxisValue(deviceId, code, value);
706 }
707 }
708
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600709 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
710 std::vector<TouchVideoFrame>> videoFrames) {
711 mVideoFrames = std::move(videoFrames);
712 }
713
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700715 std::unique_lock<std::mutex> lock(mLock);
716 base::ScopedLockAssertion assumeLocked(mLock);
717 const bool queueIsEmpty =
718 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
719 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
720 if (!queueIsEmpty) {
721 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
722 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800723 }
724
725private:
726 Device* getDevice(int32_t deviceId) const {
727 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100728 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 }
730
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700731 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700733 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734 }
735
Chris Yea52ade12020-08-27 16:49:20 -0700736 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 Device* device = getDevice(deviceId);
738 return device ? device->identifier : InputDeviceIdentifier();
739 }
740
Chris Yea52ade12020-08-27 16:49:20 -0700741 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742
Chris Yea52ade12020-08-27 16:49:20 -0700743 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744 Device* device = getDevice(deviceId);
745 if (device) {
746 *outConfiguration = device->configuration;
747 }
748 }
749
Chris Yea52ade12020-08-27 16:49:20 -0700750 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
751 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800753 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 ssize_t index = device->absoluteAxes.indexOfKey(axis);
755 if (index >= 0) {
756 *outAxisInfo = device->absoluteAxes.valueAt(index);
757 return OK;
758 }
759 }
760 outAxisInfo->clear();
761 return -1;
762 }
763
Chris Yea52ade12020-08-27 16:49:20 -0700764 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800765 Device* device = getDevice(deviceId);
766 if (device) {
767 return device->relativeAxes.indexOfKey(axis) >= 0;
768 }
769 return false;
770 }
771
Chris Yea52ade12020-08-27 16:49:20 -0700772 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773
Chris Yef59a2f42020-10-16 12:55:26 -0700774 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
775 Device* device = getDevice(deviceId);
776 if (device) {
777 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
778 }
779 return false;
780 }
781
Chris Yea52ade12020-08-27 16:49:20 -0700782 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
783 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784 Device* device = getDevice(deviceId);
785 if (device) {
786 const KeyInfo* key = getKey(device, scanCode, usageCode);
787 if (key) {
788 if (outKeycode) {
789 *outKeycode = key->keyCode;
790 }
791 if (outFlags) {
792 *outFlags = key->flags;
793 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700794 if (outMetaState) {
795 *outMetaState = metaState;
796 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797 return OK;
798 }
799 }
800 return NAME_NOT_FOUND;
801 }
802
803 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
804 if (usageCode) {
805 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
806 if (index >= 0) {
807 return &device->keysByUsageCode.valueAt(index);
808 }
809 }
810 if (scanCode) {
811 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
812 if (index >= 0) {
813 return &device->keysByScanCode.valueAt(index);
814 }
815 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700816 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 }
818
Chris Yea52ade12020-08-27 16:49:20 -0700819 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820
Chris Yef59a2f42020-10-16 12:55:26 -0700821 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
822 int32_t absCode) {
823 Device* device = getDevice(deviceId);
824 if (!device) {
825 return Errorf("Sensor device not found.");
826 }
827 auto it = device->sensorsByAbsCode.find(absCode);
828 if (it == device->sensorsByAbsCode.end()) {
829 return Errorf("Sensor map not found.");
830 }
831 const SensorInfo& info = it->second;
832 return std::make_pair(info.sensorType, info.sensorDataIndex);
833 }
834
Chris Yea52ade12020-08-27 16:49:20 -0700835 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 mExcludedDevices = devices;
837 }
838
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000839 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
840 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800841
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000842 const size_t filledSize = std::min(mEvents.size(), bufferSize);
843 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
844
845 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700846 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000847 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848 }
849
Chris Yea52ade12020-08-27 16:49:20 -0700850 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600851 auto it = mVideoFrames.find(deviceId);
852 if (it != mVideoFrames.end()) {
853 std::vector<TouchVideoFrame> frames = std::move(it->second);
854 mVideoFrames.erase(deviceId);
855 return frames;
856 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800857 return {};
858 }
859
Chris Yea52ade12020-08-27 16:49:20 -0700860 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 Device* device = getDevice(deviceId);
862 if (device) {
863 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
864 if (index >= 0) {
865 return device->scanCodeStates.valueAt(index);
866 }
867 }
868 return AKEY_STATE_UNKNOWN;
869 }
870
Chris Yea52ade12020-08-27 16:49:20 -0700871 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 Device* device = getDevice(deviceId);
873 if (device) {
874 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
875 if (index >= 0) {
876 return device->keyCodeStates.valueAt(index);
877 }
878 }
879 return AKEY_STATE_UNKNOWN;
880 }
881
Chris Yea52ade12020-08-27 16:49:20 -0700882 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800883 Device* device = getDevice(deviceId);
884 if (device) {
885 ssize_t index = device->switchStates.indexOfKey(sw);
886 if (index >= 0) {
887 return device->switchStates.valueAt(index);
888 }
889 }
890 return AKEY_STATE_UNKNOWN;
891 }
892
Chris Yea52ade12020-08-27 16:49:20 -0700893 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
894 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 Device* device = getDevice(deviceId);
896 if (device) {
897 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
898 if (index >= 0) {
899 *outValue = device->absoluteAxisValue.valueAt(index);
900 return OK;
901 }
902 }
903 *outValue = 0;
904 return -1;
905 }
906
Philip Junker4af3b3d2021-12-14 10:36:55 +0100907 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
908 Device* device = getDevice(deviceId);
909 if (!device) {
910 return AKEYCODE_UNKNOWN;
911 }
912 auto it = device->keyCodeMapping.find(locationKeyCode);
913 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
914 }
915
Chris Yea52ade12020-08-27 16:49:20 -0700916 // Return true if the device has non-empty key layout.
917 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
918 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800919 bool result = false;
920 Device* device = getDevice(deviceId);
921 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700922 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 for (size_t i = 0; i < numCodes; i++) {
924 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
925 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
926 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 }
928 }
929 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
930 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
931 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 }
933 }
934 }
935 }
936 return result;
937 }
938
Chris Yea52ade12020-08-27 16:49:20 -0700939 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 Device* device = getDevice(deviceId);
941 if (device) {
942 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
943 return index >= 0;
944 }
945 return false;
946 }
947
Arthur Hungcb40a002021-08-03 14:31:01 +0000948 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
949 Device* device = getDevice(deviceId);
950 if (!device) {
951 return false;
952 }
953 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
954 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
955 return true;
956 }
957 }
958 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
959 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
960 return true;
961 }
962 }
963 return false;
964 }
965
Chris Yea52ade12020-08-27 16:49:20 -0700966 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967 Device* device = getDevice(deviceId);
968 return device && device->leds.indexOfKey(led) >= 0;
969 }
970
Chris Yea52ade12020-08-27 16:49:20 -0700971 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972 Device* device = getDevice(deviceId);
973 if (device) {
974 ssize_t index = device->leds.indexOfKey(led);
975 if (index >= 0) {
976 device->leds.replaceValueAt(led, on);
977 } else {
978 ADD_FAILURE()
979 << "Attempted to set the state of an LED that the EventHub declared "
980 "was not present. led=" << led;
981 }
982 }
983 }
984
Chris Yea52ade12020-08-27 16:49:20 -0700985 void getVirtualKeyDefinitions(
986 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987 outVirtualKeys.clear();
988
989 Device* device = getDevice(deviceId);
990 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800991 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992 }
993 }
994
Chris Yea52ade12020-08-27 16:49:20 -0700995 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700996 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 }
998
Chris Yea52ade12020-08-27 16:49:20 -0700999 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000 return false;
1001 }
1002
Chris Yea52ade12020-08-27 16:49:20 -07001003 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004
Chris Yea52ade12020-08-27 16:49:20 -07001005 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001006
Chris Ye87143712020-11-10 05:05:58 +00001007 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
1008
Chris Yee2b1e5c2021-03-10 22:45:12 -08001009 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1010 return BATTERY_CAPACITY;
1011 }
Kim Low03ea0352020-11-06 12:45:07 -08001012
Chris Yee2b1e5c2021-03-10 22:45:12 -08001013 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1014 return BATTERY_STATUS;
1015 }
1016
Andy Chen22c330c2022-08-29 20:07:10 -04001017 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) override {
1018 return {DEFAULT_BATTERY};
1019 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001020
1021 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
1022 return std::nullopt;
1023 }
Kim Low03ea0352020-11-06 12:45:07 -08001024
Chris Ye3fdbfef2021-01-06 18:45:18 -08001025 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1026 std::vector<int32_t> ids;
1027 for (const auto& [rawId, info] : mRawLightInfos) {
1028 ids.push_back(rawId);
1029 }
1030 return ids;
1031 }
1032
1033 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1034 auto it = mRawLightInfos.find(lightId);
1035 if (it == mRawLightInfos.end()) {
1036 return std::nullopt;
1037 }
1038 return it->second;
1039 }
1040
1041 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1042 mLightBrightness.emplace(lightId, brightness);
1043 }
1044
1045 void setLightIntensities(int32_t deviceId, int32_t lightId,
1046 std::unordered_map<LightColor, int32_t> intensities) override {
1047 mLightIntensities.emplace(lightId, intensities);
1048 };
1049
1050 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1051 auto lightIt = mLightBrightness.find(lightId);
1052 if (lightIt == mLightBrightness.end()) {
1053 return std::nullopt;
1054 }
1055 return lightIt->second;
1056 }
1057
1058 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1059 int32_t deviceId, int32_t lightId) override {
1060 auto lightIt = mLightIntensities.find(lightId);
1061 if (lightIt == mLightIntensities.end()) {
1062 return std::nullopt;
1063 }
1064 return lightIt->second;
1065 };
1066
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001067 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068 return false;
1069 }
1070
Chris Yea52ade12020-08-27 16:49:20 -07001071 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072
Chris Yea52ade12020-08-27 16:49:20 -07001073 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Chris Yea52ade12020-08-27 16:49:20 -07001075 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076
Chris Yea52ade12020-08-27 16:49:20 -07001077 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078};
1079
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080// --- FakeInputMapper ---
1081
1082class FakeInputMapper : public InputMapper {
1083 uint32_t mSources;
1084 int32_t mKeyboardType;
1085 int32_t mMetaState;
1086 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1087 KeyedVector<int32_t, int32_t> mScanCodeStates;
1088 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001089 // fake mapping which would normally come from keyCharacterMap
1090 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001091 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001093 std::mutex mLock;
1094 std::condition_variable mStateChangedCondition;
1095 bool mConfigureWasCalled GUARDED_BY(mLock);
1096 bool mResetWasCalled GUARDED_BY(mLock);
1097 bool mProcessWasCalled GUARDED_BY(mLock);
1098 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099
Arthur Hungc23540e2018-11-29 20:42:11 +08001100 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001102 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1103 : InputMapper(deviceContext),
1104 mSources(sources),
1105 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001107 mConfigureWasCalled(false),
1108 mResetWasCalled(false),
1109 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110
Chris Yea52ade12020-08-27 16:49:20 -07001111 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112
1113 void setKeyboardType(int32_t keyboardType) {
1114 mKeyboardType = keyboardType;
1115 }
1116
1117 void setMetaState(int32_t metaState) {
1118 mMetaState = metaState;
1119 }
1120
1121 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001122 std::unique_lock<std::mutex> lock(mLock);
1123 base::ScopedLockAssertion assumeLocked(mLock);
1124 const bool configureCalled =
1125 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1126 return mConfigureWasCalled;
1127 });
1128 if (!configureCalled) {
1129 FAIL() << "Expected configure() to have been called.";
1130 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 mConfigureWasCalled = false;
1132 }
1133
1134 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001135 std::unique_lock<std::mutex> lock(mLock);
1136 base::ScopedLockAssertion assumeLocked(mLock);
1137 const bool resetCalled =
1138 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1139 return mResetWasCalled;
1140 });
1141 if (!resetCalled) {
1142 FAIL() << "Expected reset() to have been called.";
1143 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 mResetWasCalled = false;
1145 }
1146
Yi Kong9b14ac62018-07-17 13:48:38 -07001147 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001148 std::unique_lock<std::mutex> lock(mLock);
1149 base::ScopedLockAssertion assumeLocked(mLock);
1150 const bool processCalled =
1151 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1152 return mProcessWasCalled;
1153 });
1154 if (!processCalled) {
1155 FAIL() << "Expected process() to have been called.";
1156 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157 if (outLastEvent) {
1158 *outLastEvent = mLastEvent;
1159 }
1160 mProcessWasCalled = false;
1161 }
1162
1163 void setKeyCodeState(int32_t keyCode, int32_t state) {
1164 mKeyCodeStates.replaceValueFor(keyCode, state);
1165 }
1166
1167 void setScanCodeState(int32_t scanCode, int32_t state) {
1168 mScanCodeStates.replaceValueFor(scanCode, state);
1169 }
1170
1171 void setSwitchState(int32_t switchCode, int32_t state) {
1172 mSwitchStates.replaceValueFor(switchCode, state);
1173 }
1174
1175 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001176 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177 }
1178
Philip Junker4af3b3d2021-12-14 10:36:55 +01001179 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1180 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1181 }
1182
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001184 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185
Chris Yea52ade12020-08-27 16:49:20 -07001186 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 InputMapper::populateDeviceInfo(deviceInfo);
1188
1189 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1190 deviceInfo->setKeyboardType(mKeyboardType);
1191 }
1192 }
1193
Chris Yea52ade12020-08-27 16:49:20 -07001194 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001195 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001197
1198 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001199 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001200 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1201 mViewport = config->getDisplayViewportByPort(*displayPort);
1202 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001203
1204 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 }
1206
Chris Yea52ade12020-08-27 16:49:20 -07001207 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001208 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001210 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 }
1212
Chris Yea52ade12020-08-27 16:49:20 -07001213 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001214 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 mLastEvent = *rawEvent;
1216 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001217 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 }
1219
Chris Yea52ade12020-08-27 16:49:20 -07001220 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1222 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1223 }
1224
Philip Junker4af3b3d2021-12-14 10:36:55 +01001225 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1226 auto it = mKeyCodeMapping.find(locationKeyCode);
1227 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1228 }
1229
Chris Yea52ade12020-08-27 16:49:20 -07001230 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001231 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1232 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1233 }
1234
Chris Yea52ade12020-08-27 16:49:20 -07001235 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001236 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1237 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1238 }
1239
Chris Yea52ade12020-08-27 16:49:20 -07001240 // Return true if the device has non-empty key layout.
1241 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1242 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243 for (size_t i = 0; i < numCodes; i++) {
1244 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1245 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1246 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247 }
1248 }
1249 }
Chris Yea52ade12020-08-27 16:49:20 -07001250 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251 return result;
1252 }
1253
1254 virtual int32_t getMetaState() {
1255 return mMetaState;
1256 }
1257
1258 virtual void fadePointer() {
1259 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001260
1261 virtual std::optional<int32_t> getAssociatedDisplay() {
1262 if (mViewport) {
1263 return std::make_optional(mViewport->displayId);
1264 }
1265 return std::nullopt;
1266 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267};
1268
1269
1270// --- InstrumentedInputReader ---
1271
1272class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001273 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274
1275public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001276 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1277 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001278 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001279 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001281 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001282
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001283 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001284
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001285 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001286 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001287 InputDeviceIdentifier identifier;
1288 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001289 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001291 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001292 }
1293
Prabir Pradhan28efc192019-11-05 01:10:04 +00001294 // Make the protected loopOnce method accessible to tests.
1295 using InputReader::loopOnce;
1296
Michael Wrightd02c5b62014-02-10 15:10:22 -08001297protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001298 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1299 const InputDeviceIdentifier& identifier)
1300 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001301 if (!mNextDevices.empty()) {
1302 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1303 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001304 return device;
1305 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001306 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001307 }
1308
arthurhungdcef2dc2020-08-11 14:47:50 +08001309 // --- FakeInputReaderContext ---
1310 class FakeInputReaderContext : public ContextImpl {
1311 int32_t mGlobalMetaState;
1312 bool mUpdateGlobalMetaStateWasCalled;
1313 int32_t mGeneration;
1314
1315 public:
1316 FakeInputReaderContext(InputReader* reader)
1317 : ContextImpl(reader),
1318 mGlobalMetaState(0),
1319 mUpdateGlobalMetaStateWasCalled(false),
1320 mGeneration(1) {}
1321
1322 virtual ~FakeInputReaderContext() {}
1323
1324 void assertUpdateGlobalMetaStateWasCalled() {
1325 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1326 << "Expected updateGlobalMetaState() to have been called.";
1327 mUpdateGlobalMetaStateWasCalled = false;
1328 }
1329
1330 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1331
1332 uint32_t getGeneration() { return mGeneration; }
1333
1334 void updateGlobalMetaState() override {
1335 mUpdateGlobalMetaStateWasCalled = true;
1336 ContextImpl::updateGlobalMetaState();
1337 }
1338
1339 int32_t getGlobalMetaState() override {
1340 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1341 }
1342
1343 int32_t bumpGeneration() override {
1344 mGeneration = ContextImpl::bumpGeneration();
1345 return mGeneration;
1346 }
1347 } mFakeContext;
1348
Michael Wrightd02c5b62014-02-10 15:10:22 -08001349 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001350
1351public:
1352 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001353};
1354
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355// --- InputReaderPolicyTest ---
1356class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001357protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358 sp<FakeInputReaderPolicy> mFakePolicy;
1359
Chris Yea52ade12020-08-27 16:49:20 -07001360 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1361 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362};
1363
1364/**
1365 * Check that empty set of viewports is an acceptable configuration.
1366 * Also try to get internal viewport two different ways - by type and by uniqueId.
1367 *
1368 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1369 * Such configuration is not currently allowed.
1370 */
1371TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001372 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001373
1374 // We didn't add any viewports yet, so there shouldn't be any.
1375 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001376 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001377 ASSERT_FALSE(internalViewport);
1378
1379 // Add an internal viewport, then clear it
1380 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001381 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001382 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001383
1384 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001385 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001387 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388
1389 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001390 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001392 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393
1394 mFakePolicy->clearViewports();
1395 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001396 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001398 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001399 ASSERT_FALSE(internalViewport);
1400}
1401
1402TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1403 const std::string internalUniqueId = "local:0";
1404 const std::string externalUniqueId = "local:1";
1405 const std::string virtualUniqueId1 = "virtual:2";
1406 const std::string virtualUniqueId2 = "virtual:3";
1407 constexpr int32_t virtualDisplayId1 = 2;
1408 constexpr int32_t virtualDisplayId2 = 3;
1409
1410 // Add an internal viewport
1411 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001412 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1413 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001414 // Add an external viewport
1415 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001416 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1417 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418 // Add an virtual viewport
1419 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001420 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1421 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001422 // Add another virtual viewport
1423 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001424 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1425 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001426
1427 // Check matching by type for internal
1428 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001429 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001430 ASSERT_TRUE(internalViewport);
1431 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1432
1433 // Check matching by type for external
1434 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001435 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001436 ASSERT_TRUE(externalViewport);
1437 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1438
1439 // Check matching by uniqueId for virtual viewport #1
1440 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001441 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001442 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001443 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001444 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1445 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1446
1447 // Check matching by uniqueId for virtual viewport #2
1448 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001449 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001451 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001452 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1453 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1454}
1455
1456
1457/**
1458 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1459 * that lookup works by checking display id.
1460 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1461 */
1462TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1463 const std::string uniqueId1 = "uniqueId1";
1464 const std::string uniqueId2 = "uniqueId2";
1465 constexpr int32_t displayId1 = 2;
1466 constexpr int32_t displayId2 = 3;
1467
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001468 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1469 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001470 for (const ViewportType& type : types) {
1471 mFakePolicy->clearViewports();
1472 // Add a viewport
1473 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001474 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1475 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001476 // Add another viewport
1477 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001478 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1479 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001480
1481 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001482 std::optional<DisplayViewport> viewport1 =
1483 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001484 ASSERT_TRUE(viewport1);
1485 ASSERT_EQ(displayId1, viewport1->displayId);
1486 ASSERT_EQ(type, viewport1->type);
1487
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001488 std::optional<DisplayViewport> viewport2 =
1489 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001490 ASSERT_TRUE(viewport2);
1491 ASSERT_EQ(displayId2, viewport2->displayId);
1492 ASSERT_EQ(type, viewport2->type);
1493
1494 // When there are multiple viewports of the same kind, and uniqueId is not specified
1495 // in the call to getDisplayViewport, then that situation is not supported.
1496 // The viewports can be stored in any order, so we cannot rely on the order, since that
1497 // is just implementation detail.
1498 // However, we can check that it still returns *a* viewport, we just cannot assert
1499 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001500 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001501 ASSERT_TRUE(someViewport);
1502 }
1503}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001505/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001506 * When we have multiple internal displays make sure we always return the default display when
1507 * querying by type.
1508 */
1509TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1510 const std::string uniqueId1 = "uniqueId1";
1511 const std::string uniqueId2 = "uniqueId2";
1512 constexpr int32_t nonDefaultDisplayId = 2;
1513 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1514 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1515
1516 // Add the default display first and ensure it gets returned.
1517 mFakePolicy->clearViewports();
1518 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001519 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001520 ViewportType::INTERNAL);
1521 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001522 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001523 ViewportType::INTERNAL);
1524
1525 std::optional<DisplayViewport> viewport =
1526 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1527 ASSERT_TRUE(viewport);
1528 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1529 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1530
1531 // Add the default display second to make sure order doesn't matter.
1532 mFakePolicy->clearViewports();
1533 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001534 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001535 ViewportType::INTERNAL);
1536 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001538 ViewportType::INTERNAL);
1539
1540 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1541 ASSERT_TRUE(viewport);
1542 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1543 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1544}
1545
1546/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001547 * Check getDisplayViewportByPort
1548 */
1549TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001550 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001551 const std::string uniqueId1 = "uniqueId1";
1552 const std::string uniqueId2 = "uniqueId2";
1553 constexpr int32_t displayId1 = 1;
1554 constexpr int32_t displayId2 = 2;
1555 const uint8_t hdmi1 = 0;
1556 const uint8_t hdmi2 = 1;
1557 const uint8_t hdmi3 = 2;
1558
1559 mFakePolicy->clearViewports();
1560 // Add a viewport that's associated with some display port that's not of interest.
1561 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001562 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1563 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001564 // Add another viewport, connected to HDMI1 port
1565 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001566 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1567 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001568
1569 // Check that correct display viewport was returned by comparing the display ports.
1570 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1571 ASSERT_TRUE(hdmi1Viewport);
1572 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1573 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1574
1575 // Check that we can still get the same viewport using the uniqueId
1576 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1577 ASSERT_TRUE(hdmi1Viewport);
1578 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1579 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1580 ASSERT_EQ(type, hdmi1Viewport->type);
1581
1582 // Check that we cannot find a port with "HDMI2", because we never added one
1583 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1584 ASSERT_FALSE(hdmi2Viewport);
1585}
1586
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587// --- InputReaderTest ---
1588
1589class InputReaderTest : public testing::Test {
1590protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001591 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001593 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001594 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595
Chris Yea52ade12020-08-27 16:49:20 -07001596 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001597 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001599 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001600
Prabir Pradhan28efc192019-11-05 01:10:04 +00001601 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001602 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001603 }
1604
Chris Yea52ade12020-08-27 16:49:20 -07001605 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001606 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001608 }
1609
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001610 void addDevice(int32_t eventHubId, const std::string& name,
1611 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001612 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613
1614 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616 }
1617 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001618 mReader->loopOnce();
1619 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001620 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1621 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622 }
1623
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001624 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001625 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001626 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001627 }
1628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001629 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001630 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001631 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001632 }
1633
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001634 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001635 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001636 ftl::Flags<InputDeviceClass> classes,
1637 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001638 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1640 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001641 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643 return mapper;
1644 }
1645};
1646
Chris Ye98d3f532020-10-01 21:48:59 -07001647TEST_F(InputReaderTest, PolicyGetInputDevices) {
1648 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001649 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001650 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651
1652 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001653 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001655 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001656 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1658 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001659 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660}
1661
Chris Yee7310032020-09-22 15:36:28 -07001662TEST_F(InputReaderTest, GetMergedInputDevices) {
1663 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1664 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1665 // Add two subdevices to device
1666 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1667 // Must add at least one mapper or the device will be ignored!
1668 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1669 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1670
1671 // Push same device instance for next device to be added, so they'll have same identifier.
1672 mReader->pushNextDevice(device);
1673 mReader->pushNextDevice(device);
1674 ASSERT_NO_FATAL_FAILURE(
1675 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1676 ASSERT_NO_FATAL_FAILURE(
1677 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1678
1679 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001680 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001681}
1682
Chris Yee14523a2020-12-19 13:46:00 -08001683TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1684 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1685 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1686 // Add two subdevices to device
1687 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1688 // Must add at least one mapper or the device will be ignored!
1689 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1690 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1691
1692 // Push same device instance for next device to be added, so they'll have same identifier.
1693 mReader->pushNextDevice(device);
1694 mReader->pushNextDevice(device);
1695 // Sensor device is initially disabled
1696 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1697 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1698 nullptr));
1699 // Device is disabled because the only sub device is a sensor device and disabled initially.
1700 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1701 ASSERT_FALSE(device->isEnabled());
1702 ASSERT_NO_FATAL_FAILURE(
1703 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1704 // The merged device is enabled if any sub device is enabled
1705 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1706 ASSERT_TRUE(device->isEnabled());
1707}
1708
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001709TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001711 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 constexpr int32_t eventHubId = 1;
1713 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001715 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001716 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001717 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001718
Yi Kong9b14ac62018-07-17 13:48:38 -07001719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001720
1721 NotifyDeviceResetArgs resetArgs;
1722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001723 ASSERT_EQ(deviceId, resetArgs.deviceId);
1724
1725 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001726 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001727 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001728
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001730 ASSERT_EQ(deviceId, resetArgs.deviceId);
1731 ASSERT_EQ(device->isEnabled(), false);
1732
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001733 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001734 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001737 ASSERT_EQ(device->isEnabled(), false);
1738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001739 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001740 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001742 ASSERT_EQ(deviceId, resetArgs.deviceId);
1743 ASSERT_EQ(device->isEnabled(), true);
1744}
1745
Michael Wrightd02c5b62014-02-10 15:10:22 -08001746TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001747 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001748 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001749 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001750 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001751 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001752 AINPUT_SOURCE_KEYBOARD, nullptr);
1753 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001754
1755 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1756 AINPUT_SOURCE_ANY, AKEYCODE_A))
1757 << "Should return unknown when the device id is >= 0 but unknown.";
1758
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001759 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1760 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1761 << "Should return unknown when the device id is valid but the sources are not "
1762 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001763
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001764 ASSERT_EQ(AKEY_STATE_DOWN,
1765 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1766 AKEYCODE_A))
1767 << "Should return value provided by mapper when device id is valid and the device "
1768 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001769
1770 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1771 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1772 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1773
1774 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1775 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1776 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1777}
1778
Philip Junker4af3b3d2021-12-14 10:36:55 +01001779TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1780 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1781 constexpr int32_t eventHubId = 1;
1782 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1783 InputDeviceClass::KEYBOARD,
1784 AINPUT_SOURCE_KEYBOARD, nullptr);
1785 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1786
1787 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1788 << "Should return unknown when the device with the specified id is not found.";
1789
1790 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1791 << "Should return correct mapping when device id is valid and mapping exists.";
1792
1793 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1794 << "Should return the location key code when device id is valid and there's no "
1795 "mapping.";
1796}
1797
1798TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1799 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1800 constexpr int32_t eventHubId = 1;
1801 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1802 InputDeviceClass::JOYSTICK,
1803 AINPUT_SOURCE_GAMEPAD, nullptr);
1804 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1805
1806 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1807 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1808}
1809
Michael Wrightd02c5b62014-02-10 15:10:22 -08001810TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001811 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001812 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001813 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001814 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001816 AINPUT_SOURCE_KEYBOARD, nullptr);
1817 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818
1819 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1820 AINPUT_SOURCE_ANY, KEY_A))
1821 << "Should return unknown when the device id is >= 0 but unknown.";
1822
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1824 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1825 << "Should return unknown when the device id is valid but the sources are not "
1826 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001828 ASSERT_EQ(AKEY_STATE_DOWN,
1829 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1830 KEY_A))
1831 << "Should return value provided by mapper when device id is valid and the device "
1832 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001833
1834 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1835 AINPUT_SOURCE_TRACKBALL, KEY_A))
1836 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1837
1838 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1839 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1840 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1841}
1842
1843TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001844 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001845 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001846 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001847 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001848 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001849 AINPUT_SOURCE_KEYBOARD, nullptr);
1850 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851
1852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1853 AINPUT_SOURCE_ANY, SW_LID))
1854 << "Should return unknown when the device id is >= 0 but unknown.";
1855
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001856 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1857 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1858 << "Should return unknown when the device id is valid but the sources are not "
1859 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001861 ASSERT_EQ(AKEY_STATE_DOWN,
1862 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1863 SW_LID))
1864 << "Should return value provided by mapper when device id is valid and the device "
1865 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866
1867 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1868 AINPUT_SOURCE_TRACKBALL, SW_LID))
1869 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1870
1871 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1872 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1873 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1874}
1875
1876TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001878 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001879 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001880 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001881 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001882 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001884 mapper.addSupportedKeyCode(AKEYCODE_A);
1885 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886
1887 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1888 uint8_t flags[4] = { 0, 0, 0, 1 };
1889
1890 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1891 << "Should return false when device id is >= 0 but unknown.";
1892 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1893
1894 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001895 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1896 << "Should return false when device id is valid but the sources are not supported by "
1897 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001898 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1899
1900 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001901 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1902 keyCodes, flags))
1903 << "Should return value provided by mapper when device id is valid and the device "
1904 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001905 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1906
1907 flags[3] = 1;
1908 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1909 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1910 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1911
1912 flags[3] = 1;
1913 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1914 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1915 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1916}
1917
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001918TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001919 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001920 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001921
1922 NotifyConfigurationChangedArgs args;
1923
1924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1925 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1926}
1927
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001928TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001929 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001930 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001931 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001932 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001933 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001934 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001935 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001936 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001937
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001938 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001939 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001940 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1941
1942 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001943 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001944 ASSERT_EQ(when, event.when);
1945 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001946 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001947 ASSERT_EQ(EV_KEY, event.type);
1948 ASSERT_EQ(KEY_A, event.code);
1949 ASSERT_EQ(1, event.value);
1950}
1951
Garfield Tan1c7bc862020-01-28 13:24:04 -08001952TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001954 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001955 constexpr int32_t eventHubId = 1;
1956 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001957 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001958 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001959 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001960 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001961
1962 NotifyDeviceResetArgs resetArgs;
1963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001964 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001966 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001967 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001969 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001970 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001972 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001973 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001975 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001976 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001978 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001979 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001981 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001982 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001983}
1984
Garfield Tan1c7bc862020-01-28 13:24:04 -08001985TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1986 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001987 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001988 constexpr int32_t eventHubId = 1;
1989 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1990 // Must add at least one mapper or the device will be ignored!
1991 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001992 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001993 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1994
1995 NotifyDeviceResetArgs resetArgs;
1996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1997 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1998}
1999
Arthur Hungc23540e2018-11-29 20:42:11 +08002000TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002001 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002002 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002003 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002004 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002005 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2006 FakeInputMapper& mapper =
2007 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002008 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002009
2010 const uint8_t hdmi1 = 1;
2011
2012 // Associated touch screen with second display.
2013 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2014
2015 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002016 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002017 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002018 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002019 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002020 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002021 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002022 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002023 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002024 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002025
2026 // Add the device, and make sure all of the callbacks are triggered.
2027 // The device is added after the input port associations are processed since
2028 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002029 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002032 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002033
Arthur Hung2c9a3342019-07-23 14:18:59 +08002034 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002035 ASSERT_EQ(deviceId, device->getId());
2036 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2037 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002038
2039 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002040 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002041 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002042 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002043}
2044
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002045TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2046 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002047 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002048 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2049 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2050 // Must add at least one mapper or the device will be ignored!
2051 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2052 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2053 mReader->pushNextDevice(device);
2054 mReader->pushNextDevice(device);
2055 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2056 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2057
2058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2059
2060 NotifyDeviceResetArgs resetArgs;
2061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2062 ASSERT_EQ(deviceId, resetArgs.deviceId);
2063 ASSERT_TRUE(device->isEnabled());
2064 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2065 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2066
2067 disableDevice(deviceId);
2068 mReader->loopOnce();
2069
2070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2071 ASSERT_EQ(deviceId, resetArgs.deviceId);
2072 ASSERT_FALSE(device->isEnabled());
2073 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2074 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2075
2076 enableDevice(deviceId);
2077 mReader->loopOnce();
2078
2079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2080 ASSERT_EQ(deviceId, resetArgs.deviceId);
2081 ASSERT_TRUE(device->isEnabled());
2082 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2083 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2084}
2085
2086TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2087 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002088 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002089 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2090 // Add two subdevices to device
2091 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2092 FakeInputMapper& mapperDevice1 =
2093 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2094 FakeInputMapper& mapperDevice2 =
2095 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2096 mReader->pushNextDevice(device);
2097 mReader->pushNextDevice(device);
2098 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2099 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2100
2101 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2102 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2103
2104 ASSERT_EQ(AKEY_STATE_DOWN,
2105 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2106 ASSERT_EQ(AKEY_STATE_DOWN,
2107 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2108 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2109 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2110}
2111
Prabir Pradhan7e186182020-11-10 13:56:45 -08002112TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2113 NotifyPointerCaptureChangedArgs args;
2114
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002115 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002116 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2117 mReader->loopOnce();
2118 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002119 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2120 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002121
2122 mFakePolicy->setPointerCapture(false);
2123 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2124 mReader->loopOnce();
2125 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002126 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002127
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002128 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002129 // does not change.
2130 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2131 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002132 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002133}
2134
Chris Ye87143712020-11-10 05:05:58 +00002135class FakeVibratorInputMapper : public FakeInputMapper {
2136public:
2137 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2138 : FakeInputMapper(deviceContext, sources) {}
2139
2140 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2141};
2142
2143TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2144 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002145 ftl::Flags<InputDeviceClass> deviceClass =
2146 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002147 constexpr int32_t eventHubId = 1;
2148 const char* DEVICE_LOCATION = "BLUETOOTH";
2149 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2150 FakeVibratorInputMapper& mapper =
2151 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2152 mReader->pushNextDevice(device);
2153
2154 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2155 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2156
2157 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2158 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2159}
2160
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002161// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002162
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002163class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002164public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002165 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002166
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002167 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002168
Andy Chen22c330c2022-08-29 20:07:10 -04002169 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2170
Chris Yee2b1e5c2021-03-10 22:45:12 -08002171 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2172
2173 void dump(std::string& dump) override {}
2174
2175 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2176 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002177 }
2178
Chris Yee2b1e5c2021-03-10 22:45:12 -08002179 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2180 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002181 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002182
2183 bool setLightColor(int32_t lightId, int32_t color) override {
2184 getDeviceContext().setLightBrightness(lightId, color >> 24);
2185 return true;
2186 }
2187
2188 std::optional<int32_t> getLightColor(int32_t lightId) override {
2189 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2190 if (!result.has_value()) {
2191 return std::nullopt;
2192 }
2193 return result.value() << 24;
2194 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002195
2196 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2197
2198 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2199
2200private:
2201 InputDeviceContext& mDeviceContext;
2202 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2203 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chen22c330c2022-08-29 20:07:10 -04002204 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002205};
2206
Chris Yee2b1e5c2021-03-10 22:45:12 -08002207TEST_F(InputReaderTest, BatteryGetCapacity) {
2208 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002209 ftl::Flags<InputDeviceClass> deviceClass =
2210 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002211 constexpr int32_t eventHubId = 1;
2212 const char* DEVICE_LOCATION = "BLUETOOTH";
2213 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002214 FakePeripheralController& controller =
2215 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002216 mReader->pushNextDevice(device);
2217
2218 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2219
2220 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2221 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2222}
2223
2224TEST_F(InputReaderTest, BatteryGetStatus) {
2225 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002226 ftl::Flags<InputDeviceClass> deviceClass =
2227 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002228 constexpr int32_t eventHubId = 1;
2229 const char* DEVICE_LOCATION = "BLUETOOTH";
2230 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002231 FakePeripheralController& controller =
2232 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002233 mReader->pushNextDevice(device);
2234
2235 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2236
2237 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2238 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2239}
2240
Chris Ye3fdbfef2021-01-06 18:45:18 -08002241TEST_F(InputReaderTest, LightGetColor) {
2242 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002243 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002244 constexpr int32_t eventHubId = 1;
2245 const char* DEVICE_LOCATION = "BLUETOOTH";
2246 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002247 FakePeripheralController& controller =
2248 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002249 mReader->pushNextDevice(device);
2250 RawLightInfo info = {.id = 1,
2251 .name = "Mono",
2252 .maxBrightness = 255,
2253 .flags = InputLightClass::BRIGHTNESS,
2254 .path = ""};
2255 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2256 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2257
2258 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002259
Chris Yee2b1e5c2021-03-10 22:45:12 -08002260 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2261 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002262 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2263 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2264}
2265
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002266// --- InputReaderIntegrationTest ---
2267
2268// These tests create and interact with the InputReader only through its interface.
2269// The InputReader is started during SetUp(), which starts its processing in its own
2270// thread. The tests use linux uinput to emulate input devices.
2271// NOTE: Interacting with the physical device while these tests are running may cause
2272// the tests to fail.
2273class InputReaderIntegrationTest : public testing::Test {
2274protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002275 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002276 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002277 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002278
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002279 std::shared_ptr<FakePointerController> mFakePointerController;
2280
Chris Yea52ade12020-08-27 16:49:20 -07002281 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002282 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002283 mFakePointerController = std::make_shared<FakePointerController>();
2284 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002285 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2286 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002287
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002288 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2289 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002290 ASSERT_EQ(mReader->start(), OK);
2291
2292 // Since this test is run on a real device, all the input devices connected
2293 // to the test device will show up in mReader. We wait for those input devices to
2294 // show up before beginning the tests.
2295 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2296 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2297 }
2298
Chris Yea52ade12020-08-27 16:49:20 -07002299 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002300 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002301 mReader.reset();
2302 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002303 mFakePolicy.clear();
2304 }
2305};
2306
2307TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2308 // An invalid input device that is only used for this test.
2309 class InvalidUinputDevice : public UinputDevice {
2310 public:
2311 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2312
2313 private:
2314 void configureDevice(int fd, uinput_user_dev* device) override {}
2315 };
2316
2317 const size_t numDevices = mFakePolicy->getInputDevices().size();
2318
2319 // UinputDevice does not set any event or key bits, so InputReader should not
2320 // consider it as a valid device.
2321 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2322 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2323 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2324 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2325
2326 invalidDevice.reset();
2327 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2328 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2329 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2330}
2331
2332TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2333 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2334
2335 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2336 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2337 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2338 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2339
2340 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002341 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002342 const auto& it =
2343 std::find_if(inputDevices.begin(), inputDevices.end(),
2344 [&keyboard](const InputDeviceInfo& info) {
2345 return info.getIdentifier().name == keyboard->getName();
2346 });
2347
2348 ASSERT_NE(it, inputDevices.end());
2349 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2350 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2351 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002352
2353 keyboard.reset();
2354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2356 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2357}
2358
2359TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2360 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2361 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2362
2363 NotifyConfigurationChangedArgs configChangedArgs;
2364 ASSERT_NO_FATAL_FAILURE(
2365 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002366 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002367 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2368
2369 NotifyKeyArgs keyArgs;
2370 keyboard->pressAndReleaseHomeKey();
2371 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2372 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002373 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002374 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002375 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002376 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002377 prevTimestamp = keyArgs.eventTime;
2378
2379 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2380 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002381 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002382 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002383 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002384}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002385
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002386/**
2387 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2388 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2389 * are passed to the listener.
2390 */
2391static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2392TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2393 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2394 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2395 NotifyKeyArgs keyArgs;
2396
2397 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2399 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2400 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2401
2402 controller->pressAndReleaseKey(BTN_GEAR_UP);
2403 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2404 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2405 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2406}
2407
Arthur Hungaab25622020-01-16 11:22:11 +08002408// --- TouchProcessTest ---
2409class TouchIntegrationTest : public InputReaderIntegrationTest {
2410protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002411 const std::string UNIQUE_ID = "local:0";
2412
Chris Yea52ade12020-08-27 16:49:20 -07002413 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002414 InputReaderIntegrationTest::SetUp();
2415 // At least add an internal display.
2416 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2417 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002418 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002419
2420 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2421 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2423 }
2424
2425 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2426 int32_t orientation, const std::string& uniqueId,
2427 std::optional<uint8_t> physicalPort,
2428 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002429 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2430 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002431 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2432 }
2433
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002434 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2435 NotifyMotionArgs args;
2436 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2437 EXPECT_EQ(action, args.action);
2438 ASSERT_EQ(points.size(), args.pointerCount);
2439 for (size_t i = 0; i < args.pointerCount; i++) {
2440 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2441 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2442 }
2443 }
2444
Arthur Hungaab25622020-01-16 11:22:11 +08002445 std::unique_ptr<UinputTouchScreen> mDevice;
2446};
2447
2448TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2449 NotifyMotionArgs args;
2450 const Point centerPoint = mDevice->getCenterPoint();
2451
2452 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002453 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002454 mDevice->sendDown(centerPoint);
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_DOWN, args.action);
2458
2459 // ACTION_MOVE
2460 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002461 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2464
2465 // ACTION_UP
2466 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002467 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002468 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2469 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2470}
2471
2472TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2473 NotifyMotionArgs args;
2474 const Point centerPoint = mDevice->getCenterPoint();
2475
2476 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002477 mDevice->sendSlot(FIRST_SLOT);
2478 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002479 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002480 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2482 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2483
2484 // ACTION_POINTER_DOWN (Second slot)
2485 const Point secondPoint = centerPoint + Point(100, 100);
2486 mDevice->sendSlot(SECOND_SLOT);
2487 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002488 mDevice->sendDown(secondPoint);
2489 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_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002492
2493 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002494 mDevice->sendMove(secondPoint + Point(1, 1));
2495 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2498
2499 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002500 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002501 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002502 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002503 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002504
2505 // ACTION_UP
2506 mDevice->sendSlot(FIRST_SLOT);
2507 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002508 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002509 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2510 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2511}
2512
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002513/**
2514 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2515 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2516 * data?
2517 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2518 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2519 * for Pointer 0 only is generated after.
2520 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2521 * events, we will not miss any information.
2522 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2523 * event generated afterwards that contains the newest movement of pointer 0.
2524 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2525 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2526 * losing information about non-palm pointers.
2527 */
2528TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2529 NotifyMotionArgs args;
2530 const Point centerPoint = mDevice->getCenterPoint();
2531
2532 // ACTION_DOWN
2533 mDevice->sendSlot(FIRST_SLOT);
2534 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2535 mDevice->sendDown(centerPoint);
2536 mDevice->sendSync();
2537 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2538
2539 // ACTION_POINTER_DOWN (Second slot)
2540 const Point secondPoint = centerPoint + Point(100, 100);
2541 mDevice->sendSlot(SECOND_SLOT);
2542 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2543 mDevice->sendDown(secondPoint);
2544 mDevice->sendSync();
2545 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2546
2547 // ACTION_MOVE (First slot)
2548 mDevice->sendSlot(FIRST_SLOT);
2549 mDevice->sendMove(centerPoint + Point(5, 5));
2550 // ACTION_POINTER_UP (Second slot)
2551 mDevice->sendSlot(SECOND_SLOT);
2552 mDevice->sendPointerUp();
2553 // Send a single sync for the above 2 pointer updates
2554 mDevice->sendSync();
2555
2556 // First, we should get POINTER_UP for the second pointer
2557 assertReceivedMotion(ACTION_POINTER_1_UP,
2558 {/*first pointer */ centerPoint + Point(5, 5),
2559 /*second pointer*/ secondPoint});
2560
2561 // Next, the MOVE event for the first pointer
2562 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2563}
2564
2565/**
2566 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2567 * move, and then it will go up, all in the same frame.
2568 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2569 * gets sent to the listener.
2570 */
2571TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2572 NotifyMotionArgs args;
2573 const Point centerPoint = mDevice->getCenterPoint();
2574
2575 // ACTION_DOWN
2576 mDevice->sendSlot(FIRST_SLOT);
2577 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2578 mDevice->sendDown(centerPoint);
2579 mDevice->sendSync();
2580 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2581
2582 // ACTION_POINTER_DOWN (Second slot)
2583 const Point secondPoint = centerPoint + Point(100, 100);
2584 mDevice->sendSlot(SECOND_SLOT);
2585 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2586 mDevice->sendDown(secondPoint);
2587 mDevice->sendSync();
2588 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2589
2590 // ACTION_MOVE (First slot)
2591 mDevice->sendSlot(FIRST_SLOT);
2592 mDevice->sendMove(centerPoint + Point(5, 5));
2593 // ACTION_POINTER_UP (Second slot)
2594 mDevice->sendSlot(SECOND_SLOT);
2595 mDevice->sendMove(secondPoint + Point(6, 6));
2596 mDevice->sendPointerUp();
2597 // Send a single sync for the above 2 pointer updates
2598 mDevice->sendSync();
2599
2600 // First, we should get POINTER_UP for the second pointer
2601 // The movement of the second pointer during the liftoff frame is ignored.
2602 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2603 assertReceivedMotion(ACTION_POINTER_1_UP,
2604 {/*first pointer */ centerPoint + Point(5, 5),
2605 /*second pointer*/ secondPoint});
2606
2607 // Next, the MOVE event for the first pointer
2608 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2609}
2610
Arthur Hungaab25622020-01-16 11:22:11 +08002611TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2612 NotifyMotionArgs args;
2613 const Point centerPoint = mDevice->getCenterPoint();
2614
2615 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002616 mDevice->sendSlot(FIRST_SLOT);
2617 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002618 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002619 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002620 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2621 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2622
arthurhungcc7f9802020-04-30 17:55:40 +08002623 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002624 const Point secondPoint = centerPoint + Point(100, 100);
2625 mDevice->sendSlot(SECOND_SLOT);
2626 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2627 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002628 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002629 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002630 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002631
arthurhungcc7f9802020-04-30 17:55:40 +08002632 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002633 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002634 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002635 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2637
arthurhungcc7f9802020-04-30 17:55:40 +08002638 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2639 // a palm event.
2640 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002641 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002642 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002643 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002644 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002645 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002646
arthurhungcc7f9802020-04-30 17:55:40 +08002647 // Send up to second slot, expect first slot send moving.
2648 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002649 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002650 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002652
arthurhungcc7f9802020-04-30 17:55:40 +08002653 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002654 mDevice->sendSlot(FIRST_SLOT);
2655 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002656 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002657
arthurhungcc7f9802020-04-30 17:55:40 +08002658 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2659 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002660}
2661
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663class InputDeviceTest : public testing::Test {
2664protected:
2665 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002666 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667 static const int32_t DEVICE_ID;
2668 static const int32_t DEVICE_GENERATION;
2669 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002670 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002671 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002673 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002675 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002676 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002677 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678
Chris Yea52ade12020-08-27 16:49:20 -07002679 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002680 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002682 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002683 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002684 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 InputDeviceIdentifier identifier;
2686 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002687 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002688 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002689 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002690 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002691 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002692 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693 }
2694
Chris Yea52ade12020-08-27 16:49:20 -07002695 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002696 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698 }
2699};
2700
2701const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002702const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002703const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2705const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002706const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002707 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002708const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709
2710TEST_F(InputDeviceTest, ImmutableProperties) {
2711 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002712 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002713 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002714}
2715
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002716TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2717 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002718}
2719
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2721 // Configuration.
2722 InputReaderConfiguration config;
2723 mDevice->configure(ARBITRARY_TIME, &config, 0);
2724
2725 // Reset.
2726 mDevice->reset(ARBITRARY_TIME);
2727
2728 NotifyDeviceResetArgs resetArgs;
2729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2730 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2731 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2732
2733 // Metadata.
2734 ASSERT_TRUE(mDevice->isIgnored());
2735 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2736
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002737 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002739 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002740 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2741 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2742
2743 // State queries.
2744 ASSERT_EQ(0, mDevice->getMetaState());
2745
2746 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2747 << "Ignored device should return unknown key code state.";
2748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2749 << "Ignored device should return unknown scan code state.";
2750 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2751 << "Ignored device should return unknown switch state.";
2752
2753 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2754 uint8_t flags[2] = { 0, 1 };
2755 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2756 << "Ignored device should never mark any key codes.";
2757 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2758 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2759}
2760
2761TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2762 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002763 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002765 FakeInputMapper& mapper1 =
2766 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2768 mapper1.setMetaState(AMETA_ALT_ON);
2769 mapper1.addSupportedKeyCode(AKEYCODE_A);
2770 mapper1.addSupportedKeyCode(AKEYCODE_B);
2771 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2772 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2773 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2774 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2775 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002777 FakeInputMapper& mapper2 =
2778 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002779 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780
2781 InputReaderConfiguration config;
2782 mDevice->configure(ARBITRARY_TIME, &config, 0);
2783
2784 String8 propertyValue;
2785 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2786 << "Device should have read configuration during configuration phase.";
2787 ASSERT_STREQ("value", propertyValue.string());
2788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002789 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2790 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791
2792 // Reset
2793 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002794 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2795 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796
2797 NotifyDeviceResetArgs resetArgs;
2798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2799 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2800 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2801
2802 // Metadata.
2803 ASSERT_FALSE(mDevice->isIgnored());
2804 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2805
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002806 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002807 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002808 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2810 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2811
2812 // State queries.
2813 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2814 << "Should query mappers and combine meta states.";
2815
2816 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2817 << "Should return unknown key code state when source not supported.";
2818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2819 << "Should return unknown scan code state when source not supported.";
2820 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2821 << "Should return unknown switch state when source not supported.";
2822
2823 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2824 << "Should query mapper when source is supported.";
2825 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2826 << "Should query mapper when source is supported.";
2827 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2828 << "Should query mapper when source is supported.";
2829
2830 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2831 uint8_t flags[4] = { 0, 0, 0, 1 };
2832 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2833 << "Should do nothing when source is unsupported.";
2834 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2835 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2836 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2837 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2838
2839 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2840 << "Should query mapper when source is supported.";
2841 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2842 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2843 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2844 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2845
2846 // Event handling.
2847 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002848 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849 mDevice->process(&event, 1);
2850
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002851 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2852 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002853}
2854
Arthur Hung2c9a3342019-07-23 14:18:59 +08002855// A single input device is associated with a specific display. Check that:
2856// 1. Device is disabled if the viewport corresponding to the associated display is not found
2857// 2. Device is disabled when setEnabled API is called
2858TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002859 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002860
2861 // First Configuration.
2862 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2863
2864 // Device should be enabled by default.
2865 ASSERT_TRUE(mDevice->isEnabled());
2866
2867 // Prepare associated info.
2868 constexpr uint8_t hdmi = 1;
2869 const std::string UNIQUE_ID = "local:1";
2870
2871 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2872 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2873 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2874 // Device should be disabled because it is associated with a specific display via
2875 // input port <-> display port association, but the corresponding display is not found
2876 ASSERT_FALSE(mDevice->isEnabled());
2877
2878 // Prepare displays.
2879 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002880 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2881 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002882 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2883 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2884 ASSERT_TRUE(mDevice->isEnabled());
2885
2886 // Device should be disabled after set disable.
2887 mFakePolicy->addDisabledDevice(mDevice->getId());
2888 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2889 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2890 ASSERT_FALSE(mDevice->isEnabled());
2891
2892 // Device should still be disabled even found the associated display.
2893 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2894 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2895 ASSERT_FALSE(mDevice->isEnabled());
2896}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897
Christine Franks1ba71cc2021-04-07 14:37:42 -07002898TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2899 // Device should be enabled by default.
2900 mFakePolicy->clearViewports();
2901 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2902 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2903 ASSERT_TRUE(mDevice->isEnabled());
2904
2905 // Device should be disabled because it is associated with a specific display, but the
2906 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002907 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002908 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2909 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2910 ASSERT_FALSE(mDevice->isEnabled());
2911
2912 // Device should be enabled when a display is found.
2913 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2914 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2915 NO_PORT, ViewportType::INTERNAL);
2916 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2917 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2918 ASSERT_TRUE(mDevice->isEnabled());
2919
2920 // Device should be disabled after set disable.
2921 mFakePolicy->addDisabledDevice(mDevice->getId());
2922 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2923 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2924 ASSERT_FALSE(mDevice->isEnabled());
2925
2926 // Device should still be disabled even found the associated display.
2927 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2928 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2929 ASSERT_FALSE(mDevice->isEnabled());
2930}
2931
Christine Franks2a2293c2022-01-18 11:51:16 -08002932TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2933 mFakePolicy->clearViewports();
2934 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2935 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2936
Christine Franks2a2293c2022-01-18 11:51:16 -08002937 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2938 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2939 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2940 NO_PORT, ViewportType::INTERNAL);
2941 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2942 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2943 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2944}
2945
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002946/**
2947 * This test reproduces a crash caused by a dangling reference that remains after device is added
2948 * and removed. The reference is accessed in InputDevice::dump(..);
2949 */
2950TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2951 constexpr int32_t TEST_EVENTHUB_ID = 10;
2952 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2953
2954 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
2955 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
2956 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2957 std::string dumpStr, eventHubDevStr;
2958 device.dump(dumpStr, eventHubDevStr);
2959}
2960
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961// --- InputMapperTest ---
2962
2963class InputMapperTest : public testing::Test {
2964protected:
2965 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002966 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967 static const int32_t DEVICE_ID;
2968 static const int32_t DEVICE_GENERATION;
2969 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002970 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002971 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002973 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002975 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002976 std::unique_ptr<InstrumentedInputReader> mReader;
2977 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002979 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002980 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002982 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002983 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002984 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002985 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002986 // Consume the device reset notification generated when adding a new device.
2987 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 }
2989
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002990 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002991 SetUp(DEVICE_CLASSES);
2992 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002993
Chris Yea52ade12020-08-27 16:49:20 -07002994 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002995 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002997 }
2998
2999 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003000 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 }
3002
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003003 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003004 if (!changes ||
3005 (changes &
3006 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3007 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003008 mReader->requestRefreshConfiguration(changes);
3009 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003010 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003011 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhan36690412022-08-05 22:26:56 +00003012 // Loop the reader to flush the input listener queue.
3013 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003014 }
3015
arthurhungdcef2dc2020-08-11 14:47:50 +08003016 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3017 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003018 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003019 InputDeviceIdentifier identifier;
3020 identifier.name = name;
3021 identifier.location = location;
3022 std::shared_ptr<InputDevice> device =
3023 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3024 identifier);
3025 mReader->pushNextDevice(device);
3026 mFakeEventHub->addDevice(eventHubId, name, classes);
3027 mReader->loopOnce();
3028 return device;
3029 }
3030
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003031 template <class T, typename... Args>
3032 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003033 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003034 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003036 mapper.reset(ARBITRARY_TIME);
Prabir Pradhan36690412022-08-05 22:26:56 +00003037 // Loop the reader to flush the input listener queue.
3038 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003039 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040 }
3041
3042 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003043 int32_t orientation, const std::string& uniqueId,
3044 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003045 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3046 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003047 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3048 }
3049
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003050 void clearViewports() {
3051 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052 }
3053
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003054 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3055 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056 RawEvent event;
3057 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003058 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003059 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060 event.type = type;
3061 event.code = code;
3062 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003063 mapper.process(&event);
Prabir Pradhan36690412022-08-05 22:26:56 +00003064 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003065 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066 }
3067
3068 static void assertMotionRange(const InputDeviceInfo& info,
3069 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3070 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003071 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3073 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3074 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3075 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3076 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3077 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3078 }
3079
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003080 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3081 float size, float touchMajor, float touchMinor, float toolMajor,
3082 float toolMinor, float orientation, float distance,
3083 float scaledAxisEpsilon = 1.f) {
3084 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3085 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3087 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003088 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3089 scaledAxisEpsilon);
3090 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3091 scaledAxisEpsilon);
3092 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3093 scaledAxisEpsilon);
3094 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3095 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3097 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3098 }
3099
Michael Wright17db18e2020-06-26 20:51:44 +01003100 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003102 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 ASSERT_NEAR(x, actualX, 1);
3104 ASSERT_NEAR(y, actualY, 1);
3105 }
3106};
3107
3108const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003109const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003110const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3112const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003113const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3114 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003115const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116
3117// --- SwitchInputMapperTest ---
3118
3119class SwitchInputMapperTest : public InputMapperTest {
3120protected:
3121};
3122
3123TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003124 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127}
3128
3129TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003130 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003132 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003133 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003135 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003136 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137}
3138
3139TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003140 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146
3147 NotifySwitchArgs args;
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3149 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003150 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3151 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 args.switchMask);
3153 ASSERT_EQ(uint32_t(0), args.policyFlags);
3154}
3155
Chris Ye87143712020-11-10 05:05:58 +00003156// --- VibratorInputMapperTest ---
3157class VibratorInputMapperTest : public InputMapperTest {
3158protected:
3159 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3160};
3161
3162TEST_F(VibratorInputMapperTest, GetSources) {
3163 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3164
3165 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3166}
3167
3168TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3169 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3170
3171 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3172}
3173
3174TEST_F(VibratorInputMapperTest, Vibrate) {
3175 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003176 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003177 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3178
3179 VibrationElement pattern(2);
3180 VibrationSequence sequence(2);
3181 pattern.duration = std::chrono::milliseconds(200);
3182 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3183 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3184 sequence.addElement(pattern);
3185 pattern.duration = std::chrono::milliseconds(500);
3186 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3187 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3188 sequence.addElement(pattern);
3189
3190 std::vector<int64_t> timings = {0, 1};
3191 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3192
3193 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003194 // Start vibrating
3195 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003196 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003197 // Verify vibrator state listener was notified.
3198 mReader->loopOnce();
3199 NotifyVibratorStateArgs args;
3200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3201 ASSERT_EQ(DEVICE_ID, args.deviceId);
3202 ASSERT_TRUE(args.isOn);
3203 // Stop vibrating
3204 mapper.cancelVibrate(VIBRATION_TOKEN);
3205 ASSERT_FALSE(mapper.isVibrating());
3206 // Verify vibrator state listener was notified.
3207 mReader->loopOnce();
3208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3209 ASSERT_EQ(DEVICE_ID, args.deviceId);
3210 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003211}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212
Chris Yef59a2f42020-10-16 12:55:26 -07003213// --- SensorInputMapperTest ---
3214
3215class SensorInputMapperTest : public InputMapperTest {
3216protected:
3217 static const int32_t ACCEL_RAW_MIN;
3218 static const int32_t ACCEL_RAW_MAX;
3219 static const int32_t ACCEL_RAW_FUZZ;
3220 static const int32_t ACCEL_RAW_FLAT;
3221 static const int32_t ACCEL_RAW_RESOLUTION;
3222
3223 static const int32_t GYRO_RAW_MIN;
3224 static const int32_t GYRO_RAW_MAX;
3225 static const int32_t GYRO_RAW_FUZZ;
3226 static const int32_t GYRO_RAW_FLAT;
3227 static const int32_t GYRO_RAW_RESOLUTION;
3228
3229 static const float GRAVITY_MS2_UNIT;
3230 static const float DEGREE_RADIAN_UNIT;
3231
3232 void prepareAccelAxes();
3233 void prepareGyroAxes();
3234 void setAccelProperties();
3235 void setGyroProperties();
3236 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3237};
3238
3239const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3240const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3241const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3242const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3243const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3244
3245const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3246const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3247const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3248const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3249const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3250
3251const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3252const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3253
3254void SensorInputMapperTest::prepareAccelAxes() {
3255 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3256 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3258 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3259 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3260 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3261}
3262
3263void SensorInputMapperTest::prepareGyroAxes() {
3264 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3265 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3267 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3268 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3269 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3270}
3271
3272void SensorInputMapperTest::setAccelProperties() {
3273 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3274 /* sensorDataIndex */ 0);
3275 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3276 /* sensorDataIndex */ 1);
3277 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3278 /* sensorDataIndex */ 2);
3279 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3280 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3281 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3282 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3283 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3284}
3285
3286void SensorInputMapperTest::setGyroProperties() {
3287 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3288 /* sensorDataIndex */ 0);
3289 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3290 /* sensorDataIndex */ 1);
3291 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3292 /* sensorDataIndex */ 2);
3293 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3294 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3295 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3296 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3297 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3298}
3299
3300TEST_F(SensorInputMapperTest, GetSources) {
3301 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3302
3303 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3304}
3305
3306TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3307 setAccelProperties();
3308 prepareAccelAxes();
3309 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3310
3311 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3312 std::chrono::microseconds(10000),
3313 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003314 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003320
3321 NotifySensorArgs args;
3322 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3323 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3324 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3325
3326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3327 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3328 ASSERT_EQ(args.deviceId, DEVICE_ID);
3329 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3330 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3331 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3332 ASSERT_EQ(args.values, values);
3333 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3334}
3335
3336TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3337 setGyroProperties();
3338 prepareGyroAxes();
3339 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3340
3341 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3342 std::chrono::microseconds(10000),
3343 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003344 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003350
3351 NotifySensorArgs args;
3352 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3353 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3354 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3355
3356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3357 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3358 ASSERT_EQ(args.deviceId, DEVICE_ID);
3359 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3360 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3361 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3362 ASSERT_EQ(args.values, values);
3363 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3364}
3365
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366// --- KeyboardInputMapperTest ---
3367
3368class KeyboardInputMapperTest : public InputMapperTest {
3369protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003370 const std::string UNIQUE_ID = "local:0";
3371
3372 void prepareDisplay(int32_t orientation);
3373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003374 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003375 int32_t originalKeyCode, int32_t rotatedKeyCode,
3376 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377};
3378
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003379/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3380 * orientation.
3381 */
3382void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003383 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3384 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003385}
3386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003387void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003388 int32_t originalScanCode, int32_t originalKeyCode,
3389 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390 NotifyKeyArgs args;
3391
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3394 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3395 ASSERT_EQ(originalScanCode, args.scanCode);
3396 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003397 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3402 ASSERT_EQ(originalScanCode, args.scanCode);
3403 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003404 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405}
3406
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003408 KeyboardInputMapper& mapper =
3409 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3410 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003412 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413}
3414
3415TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3416 const int32_t USAGE_A = 0x070004;
3417 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003418 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3419 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003420 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3421 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3422 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003424 KeyboardInputMapper& mapper =
3425 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3426 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003427 // Initial metastate is AMETA_NONE.
3428 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429
3430 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 NotifyKeyArgs args;
3433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3434 ASSERT_EQ(DEVICE_ID, args.deviceId);
3435 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3436 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3437 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3438 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3439 ASSERT_EQ(KEY_HOME, args.scanCode);
3440 ASSERT_EQ(AMETA_NONE, args.metaState);
3441 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3442 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3443 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3444
3445 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003446 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3448 ASSERT_EQ(DEVICE_ID, args.deviceId);
3449 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3450 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3451 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3452 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3453 ASSERT_EQ(KEY_HOME, args.scanCode);
3454 ASSERT_EQ(AMETA_NONE, args.metaState);
3455 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3456 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3458
3459 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3463 ASSERT_EQ(DEVICE_ID, args.deviceId);
3464 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3465 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3466 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3467 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3468 ASSERT_EQ(0, args.scanCode);
3469 ASSERT_EQ(AMETA_NONE, args.metaState);
3470 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3471 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3472 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3473
3474 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3476 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3478 ASSERT_EQ(DEVICE_ID, args.deviceId);
3479 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3480 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3481 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3482 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3483 ASSERT_EQ(0, args.scanCode);
3484 ASSERT_EQ(AMETA_NONE, args.metaState);
3485 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3486 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3487 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3488
3489 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3493 ASSERT_EQ(DEVICE_ID, args.deviceId);
3494 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3495 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3496 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3497 ASSERT_EQ(0, args.keyCode);
3498 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3499 ASSERT_EQ(AMETA_NONE, args.metaState);
3500 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3501 ASSERT_EQ(0U, args.policyFlags);
3502 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3503
3504 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3506 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3508 ASSERT_EQ(DEVICE_ID, args.deviceId);
3509 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3510 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3511 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3512 ASSERT_EQ(0, args.keyCode);
3513 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3514 ASSERT_EQ(AMETA_NONE, args.metaState);
3515 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3516 ASSERT_EQ(0U, args.policyFlags);
3517 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3518}
3519
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003520/**
3521 * Ensure that the readTime is set to the time when the EV_KEY is received.
3522 */
3523TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3524 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3525
3526 KeyboardInputMapper& mapper =
3527 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3528 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3529 NotifyKeyArgs args;
3530
3531 // Key down
3532 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3534 ASSERT_EQ(12, args.readTime);
3535
3536 // Key up
3537 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3539 ASSERT_EQ(15, args.readTime);
3540}
3541
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003543 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3544 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003545 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3546 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3547 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003549 KeyboardInputMapper& mapper =
3550 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3551 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552
Arthur Hung95f68612022-04-07 14:08:22 +08003553 // Initial metastate is AMETA_NONE.
3554 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555
3556 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558 NotifyKeyArgs args;
3559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3560 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003561 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003562 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
3564 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003565 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003568 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569
3570 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003571 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3573 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003575
3576 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003577 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3579 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003580 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003581 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003582}
3583
3584TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003585 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3586 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3587 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3588 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003590 KeyboardInputMapper& mapper =
3591 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3592 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003594 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3596 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3597 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3598 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3599 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3600 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3601 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3602 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3603}
3604
3605TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003606 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3607 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3608 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3609 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003612 KeyboardInputMapper& mapper =
3613 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3614 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003616 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003617 ASSERT_NO_FATAL_FAILURE(
3618 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3619 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3620 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3622 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3624 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003626 clearViewports();
3627 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003628 ASSERT_NO_FATAL_FAILURE(
3629 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3630 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3631 AKEYCODE_DPAD_UP, DISPLAY_ID));
3632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3633 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3635 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003637 clearViewports();
3638 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003639 ASSERT_NO_FATAL_FAILURE(
3640 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3641 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3642 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3644 AKEYCODE_DPAD_UP, DISPLAY_ID));
3645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3646 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003648 clearViewports();
3649 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003650 ASSERT_NO_FATAL_FAILURE(
3651 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3652 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3653 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3654 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3655 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3656 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3657 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658
3659 // Special case: if orientation changes while key is down, we still emit the same keycode
3660 // in the key up as we did in the key down.
3661 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003662 clearViewports();
3663 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3666 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3667 ASSERT_EQ(KEY_UP, args.scanCode);
3668 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3669
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003670 clearViewports();
3671 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3674 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3675 ASSERT_EQ(KEY_UP, args.scanCode);
3676 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3677}
3678
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003679TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3680 // If the keyboard is not orientation aware,
3681 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003682 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003684 KeyboardInputMapper& mapper =
3685 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3686 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003687 NotifyKeyArgs args;
3688
3689 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3694 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3695
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003696 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3701 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3702}
3703
3704TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3705 // If the keyboard is orientation aware,
3706 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003707 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003708
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003709 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003710 KeyboardInputMapper& mapper =
3711 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3712 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003713 NotifyKeyArgs args;
3714
3715 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3716 // ^--- already checked by the previous test
3717
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003718 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003719 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3724 ASSERT_EQ(DISPLAY_ID, args.displayId);
3725
3726 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003727 clearViewports();
3728 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003729 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3734 ASSERT_EQ(newDisplayId, args.displayId);
3735}
3736
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003738 KeyboardInputMapper& mapper =
3739 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3740 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003742 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003743 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003745 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003746 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747}
3748
Philip Junker4af3b3d2021-12-14 10:36:55 +01003749TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3750 KeyboardInputMapper& mapper =
3751 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3752 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3753
3754 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3755 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3756 << "If a mapping is available, the result is equal to the mapping";
3757
3758 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3759 << "If no mapping is available, the result is the key location";
3760}
3761
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003763 KeyboardInputMapper& mapper =
3764 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3765 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003767 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003768 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003770 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772}
3773
3774TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003775 KeyboardInputMapper& mapper =
3776 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3777 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003779 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780
3781 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3782 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003783 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784 ASSERT_TRUE(flags[0]);
3785 ASSERT_FALSE(flags[1]);
3786}
3787
3788TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003789 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3790 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3791 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3792 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3793 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3794 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 KeyboardInputMapper& mapper =
3797 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3798 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003799 // Initial metastate is AMETA_NONE.
3800 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801
3802 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3804 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3805 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806
3807 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003810 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3811 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3812 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003813 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814
3815 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003818 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3819 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3820 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003821 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822
3823 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003826 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3827 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3828 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003829 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830
3831 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003834 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3835 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3836 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003837 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838
3839 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003842 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3843 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3844 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003845 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846
3847 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003850 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3851 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3852 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003853 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854}
3855
Chris Yea52ade12020-08-27 16:49:20 -07003856TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3857 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3858 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3859 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3860 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3861
3862 KeyboardInputMapper& mapper =
3863 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3864 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3865
Chris Yea52ade12020-08-27 16:49:20 -07003866 // Meta state should be AMETA_NONE after reset
3867 mapper.reset(ARBITRARY_TIME);
3868 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3869 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3870 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3871 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3872
3873 NotifyKeyArgs args;
3874 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3877 ASSERT_EQ(AMETA_NONE, args.metaState);
3878 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3879 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3880 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3881
3882 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003883 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3885 ASSERT_EQ(AMETA_NONE, args.metaState);
3886 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3887 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3888 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3889}
3890
Arthur Hung2c9a3342019-07-23 14:18:59 +08003891TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3892 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003893 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3894 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3895 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3896 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003897
3898 // keyboard 2.
3899 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003900 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003901 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003902 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003903 std::shared_ptr<InputDevice> device2 =
3904 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003905 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003906
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003907 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3908 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3909 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3910 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003911
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003912 KeyboardInputMapper& mapper =
3913 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3914 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003916 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003917 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003918 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003919 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3920 device2->reset(ARBITRARY_TIME);
3921
3922 // Prepared displays and associated info.
3923 constexpr uint8_t hdmi1 = 0;
3924 constexpr uint8_t hdmi2 = 1;
3925 const std::string SECONDARY_UNIQUE_ID = "local:1";
3926
3927 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3928 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3929
3930 // No associated display viewport found, should disable the device.
3931 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3932 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3933 ASSERT_FALSE(device2->isEnabled());
3934
3935 // Prepare second display.
3936 constexpr int32_t newDisplayId = 2;
3937 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003938 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003939 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003940 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003941 // Default device will reconfigure above, need additional reconfiguration for another device.
3942 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3943 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3944
3945 // Device should be enabled after the associated display is found.
3946 ASSERT_TRUE(mDevice->isEnabled());
3947 ASSERT_TRUE(device2->isEnabled());
3948
3949 // Test pad key events
3950 ASSERT_NO_FATAL_FAILURE(
3951 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3952 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3953 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3954 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3955 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3956 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3957 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3958
3959 ASSERT_NO_FATAL_FAILURE(
3960 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3961 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3962 AKEYCODE_DPAD_RIGHT, newDisplayId));
3963 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3964 AKEYCODE_DPAD_DOWN, newDisplayId));
3965 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3966 AKEYCODE_DPAD_LEFT, newDisplayId));
3967}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968
arthurhungc903df12020-08-11 15:08:42 +08003969TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3970 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3971 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3972 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3973 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3974 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3975 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3976
3977 KeyboardInputMapper& mapper =
3978 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3979 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003980 // Initial metastate is AMETA_NONE.
3981 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003982
3983 // Initialization should have turned all of the lights off.
3984 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3985 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3986 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3987
3988 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003991 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3992 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3993
3994 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003997 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3998 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3999
4000 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004003 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4004 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4005
4006 mFakeEventHub->removeDevice(EVENTHUB_ID);
4007 mReader->loopOnce();
4008
4009 // keyboard 2 should default toggle keys.
4010 const std::string USB2 = "USB2";
4011 const std::string DEVICE_NAME2 = "KEYBOARD2";
4012 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4013 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4014 std::shared_ptr<InputDevice> device2 =
4015 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004016 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004017 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4018 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4019 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4020 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4021 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4022 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4023
arthurhung6fe95782020-10-05 22:41:16 +08004024 KeyboardInputMapper& mapper2 =
4025 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4026 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004027 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4028 device2->reset(ARBITRARY_TIME);
4029
4030 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4031 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4032 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004033 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4034 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004035}
4036
Arthur Hungcb40a002021-08-03 14:31:01 +00004037TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4038 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4039 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4040 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4041
4042 // Suppose we have two mappers. (DPAD + KEYBOARD)
4043 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4044 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4045 KeyboardInputMapper& mapper =
4046 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4047 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004048 // Initial metastate is AMETA_NONE.
4049 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004050
4051 mReader->toggleCapsLockState(DEVICE_ID);
4052 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4053}
4054
Arthur Hungfb3cc112022-04-13 07:39:50 +00004055TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4056 // keyboard 1.
4057 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4058 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4059 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4060 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4061 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4062 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4063
4064 KeyboardInputMapper& mapper1 =
4065 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4066 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4067
4068 // keyboard 2.
4069 const std::string USB2 = "USB2";
4070 const std::string DEVICE_NAME2 = "KEYBOARD2";
4071 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4072 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4073 std::shared_ptr<InputDevice> device2 =
4074 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4075 ftl::Flags<InputDeviceClass>(0));
4076 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4077 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4078 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4079 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4080 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4081 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4082
4083 KeyboardInputMapper& mapper2 =
4084 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4085 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4086 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4087 device2->reset(ARBITRARY_TIME);
4088
Arthur Hung95f68612022-04-07 14:08:22 +08004089 // Initial metastate is AMETA_NONE.
4090 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4091 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4092
4093 // Toggle num lock on and off.
4094 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4095 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004096 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4097 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4098 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4099
4100 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4101 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4102 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4103 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4104 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4105
4106 // Toggle caps lock on and off.
4107 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4108 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4109 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4110 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4111 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4112
4113 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4114 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4115 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4116 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4117 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4118
4119 // Toggle scroll lock on and off.
4120 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4121 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4122 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4123 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4124 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4125
4126 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4127 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4128 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4129 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4130 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4131}
4132
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004133// --- KeyboardInputMapperTest_ExternalDevice ---
4134
4135class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4136protected:
Chris Yea52ade12020-08-27 16:49:20 -07004137 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004138};
4139
4140TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004141 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4142 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004143
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004144 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4145 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4146 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4147 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004149 KeyboardInputMapper& mapper =
4150 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4151 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004152
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004154 NotifyKeyArgs args;
4155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4156 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4157
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4160 ASSERT_EQ(uint32_t(0), args.policyFlags);
4161
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4164 ASSERT_EQ(uint32_t(0), args.policyFlags);
4165
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004166 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4168 ASSERT_EQ(uint32_t(0), args.policyFlags);
4169
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4172 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4173
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004174 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4176 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4177}
4178
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004179TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004180 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004181
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004182 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4183 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4184 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004185
Powei Fengd041c5d2019-05-03 17:11:33 -07004186 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004187 KeyboardInputMapper& mapper =
4188 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4189 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004190
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004192 NotifyKeyArgs args;
4193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4194 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4195
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004196 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4198 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4199
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4202 ASSERT_EQ(uint32_t(0), args.policyFlags);
4203
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004204 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4206 ASSERT_EQ(uint32_t(0), args.policyFlags);
4207
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4210 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4211
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004212 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4214 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4215}
4216
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217// --- CursorInputMapperTest ---
4218
4219class CursorInputMapperTest : public InputMapperTest {
4220protected:
4221 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4222
Michael Wright17db18e2020-06-26 20:51:44 +01004223 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224
Chris Yea52ade12020-08-27 16:49:20 -07004225 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 InputMapperTest::SetUp();
4227
Michael Wright17db18e2020-06-26 20:51:44 +01004228 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004229 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 }
4231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004232 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4233 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004234
4235 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004236 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4237 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4238 }
4239
4240 void prepareSecondaryDisplay() {
4241 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4242 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4243 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004244 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004245
4246 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4247 float pressure) {
4248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4249 0.0f, 0.0f, 0.0f, EPSILON));
4250 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251};
4252
4253const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4254
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004255void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4256 int32_t originalY, int32_t rotatedX,
4257 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258 NotifyMotionArgs args;
4259
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004265 ASSERT_NO_FATAL_FAILURE(
4266 assertCursorPointerCoords(args.pointerCoords[0],
4267 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4268 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269}
4270
4271TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004273 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004275 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276}
4277
4278TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004280 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004282 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283}
4284
4285TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004287 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288
4289 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004290 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291
4292 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004293 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4294 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4296 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4297
4298 // When the bounds are set, then there should be a valid motion range.
4299 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4300
4301 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004302 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303
4304 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4305 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4306 1, 800 - 1, 0.0f, 0.0f));
4307 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4308 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4309 2, 480 - 1, 0.0f, 0.0f));
4310 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4311 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4312 0.0f, 1.0f, 0.0f, 0.0f));
4313}
4314
4315TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004317 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318
4319 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004320 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
4322 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4323 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4324 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4325 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4326 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4327 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4328 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4329 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4330 0.0f, 1.0f, 0.0f, 0.0f));
4331}
4332
4333TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004335 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336
arthurhungdcef2dc2020-08-11 14:47:50 +08004337 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338
4339 NotifyMotionArgs args;
4340
4341 // Button press.
4342 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4346 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4347 ASSERT_EQ(DEVICE_ID, args.deviceId);
4348 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4349 ASSERT_EQ(uint32_t(0), args.policyFlags);
4350 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4351 ASSERT_EQ(0, args.flags);
4352 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4353 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4354 ASSERT_EQ(0, args.edgeFlags);
4355 ASSERT_EQ(uint32_t(1), args.pointerCount);
4356 ASSERT_EQ(0, args.pointerProperties[0].id);
4357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004358 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4360 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4361 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4362
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4364 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4365 ASSERT_EQ(DEVICE_ID, args.deviceId);
4366 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4367 ASSERT_EQ(uint32_t(0), args.policyFlags);
4368 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4369 ASSERT_EQ(0, args.flags);
4370 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4371 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4372 ASSERT_EQ(0, args.edgeFlags);
4373 ASSERT_EQ(uint32_t(1), args.pointerCount);
4374 ASSERT_EQ(0, args.pointerProperties[0].id);
4375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004377 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4378 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4379 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4380
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004382 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4383 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4385 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4386 ASSERT_EQ(DEVICE_ID, args.deviceId);
4387 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4388 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4390 ASSERT_EQ(0, args.flags);
4391 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4392 ASSERT_EQ(0, args.buttonState);
4393 ASSERT_EQ(0, args.edgeFlags);
4394 ASSERT_EQ(uint32_t(1), args.pointerCount);
4395 ASSERT_EQ(0, args.pointerProperties[0].id);
4396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004397 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4399 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4400 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4401
4402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4403 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4404 ASSERT_EQ(DEVICE_ID, args.deviceId);
4405 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4406 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4408 ASSERT_EQ(0, args.flags);
4409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4410 ASSERT_EQ(0, args.buttonState);
4411 ASSERT_EQ(0, args.edgeFlags);
4412 ASSERT_EQ(uint32_t(1), args.pointerCount);
4413 ASSERT_EQ(0, args.pointerProperties[0].id);
4414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4417 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4418 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4419}
4420
4421TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004423 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424
4425 NotifyMotionArgs args;
4426
4427 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004432 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4433 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4434 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435
4436 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004441 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4442 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443}
4444
4445TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004447 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448
4449 NotifyMotionArgs args;
4450
4451 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004456 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004460 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004467 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004468
4469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004471 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472}
4473
4474TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004476 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477
4478 NotifyMotionArgs args;
4479
4480 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4486 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004487 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4488 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4489 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004493 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4494 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4495 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004496
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004503 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4504 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4505 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506
4507 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004511 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004512 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004513
4514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004516 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517}
4518
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004519TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004520 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004522 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4523 // need to be rotated.
4524 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004525 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004527 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4536}
4537
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004538TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004539 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004541 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4542 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004543 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004545 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004546 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004547 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4548 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4553 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4554 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4555
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004556 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004557 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004558 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4559 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4561 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4564 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4565 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004567 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004568 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4570 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4571 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4575 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4576 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4577
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004578 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004579 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004580 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4581 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4582 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4583 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4584 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4585 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4586 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4587 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588}
4589
4590TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004592 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593
4594 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4595 mFakePointerController->setPosition(100, 200);
4596 mFakePointerController->setButtonState(0);
4597
4598 NotifyMotionArgs motionArgs;
4599 NotifyKeyArgs keyArgs;
4600
4601 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004602 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4606 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4607 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004608 ASSERT_NO_FATAL_FAILURE(
4609 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4613 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4614 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004615 ASSERT_NO_FATAL_FAILURE(
4616 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004617
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004618 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622 ASSERT_EQ(0, motionArgs.buttonState);
4623 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004624 ASSERT_NO_FATAL_FAILURE(
4625 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626
4627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004628 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 ASSERT_EQ(0, motionArgs.buttonState);
4630 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004631 ASSERT_NO_FATAL_FAILURE(
4632 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633
4634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004636 ASSERT_EQ(0, motionArgs.buttonState);
4637 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004638 ASSERT_NO_FATAL_FAILURE(
4639 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640
4641 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4646 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4647 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4648 motionArgs.buttonState);
4649 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4650 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004651 ASSERT_NO_FATAL_FAILURE(
4652 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4655 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4656 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4657 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4658 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004659 ASSERT_NO_FATAL_FAILURE(
4660 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661
4662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4664 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4665 motionArgs.buttonState);
4666 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4667 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(
4669 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004670
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4676 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004677 ASSERT_NO_FATAL_FAILURE(
4678 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679
4680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004682 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4683 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004684 ASSERT_NO_FATAL_FAILURE(
4685 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004690 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4691 ASSERT_EQ(0, motionArgs.buttonState);
4692 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004693 ASSERT_NO_FATAL_FAILURE(
4694 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697
4698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 ASSERT_EQ(0, motionArgs.buttonState);
4700 ASSERT_EQ(0, mFakePointerController->getButtonState());
4701 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004702 ASSERT_NO_FATAL_FAILURE(
4703 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004704
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4706 ASSERT_EQ(0, motionArgs.buttonState);
4707 ASSERT_EQ(0, mFakePointerController->getButtonState());
4708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004709 ASSERT_NO_FATAL_FAILURE(
4710 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
4712 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4716 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4717 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004718
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004720 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4722 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004723 ASSERT_NO_FATAL_FAILURE(
4724 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004725
4726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4728 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4729 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004730 ASSERT_NO_FATAL_FAILURE(
4731 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4734 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004736 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737 ASSERT_EQ(0, motionArgs.buttonState);
4738 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004739 ASSERT_NO_FATAL_FAILURE(
4740 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004741
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004744 ASSERT_EQ(0, motionArgs.buttonState);
4745 ASSERT_EQ(0, mFakePointerController->getButtonState());
4746
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004747 ASSERT_NO_FATAL_FAILURE(
4748 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4750 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4751 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4752
4753 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4757 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4758 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004759
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004761 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4763 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004764 ASSERT_NO_FATAL_FAILURE(
4765 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004766
4767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4768 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4769 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4770 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004771 ASSERT_NO_FATAL_FAILURE(
4772 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004777 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778 ASSERT_EQ(0, motionArgs.buttonState);
4779 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004780 ASSERT_NO_FATAL_FAILURE(
4781 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004782
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4784 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4785 ASSERT_EQ(0, motionArgs.buttonState);
4786 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004787 ASSERT_NO_FATAL_FAILURE(
4788 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004789
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4791 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4792 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4793
4794 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4798 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4799 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004800
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4804 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004805 ASSERT_NO_FATAL_FAILURE(
4806 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004807
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4810 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4811 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004812 ASSERT_NO_FATAL_FAILURE(
4813 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004818 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 ASSERT_EQ(0, motionArgs.buttonState);
4820 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004821 ASSERT_NO_FATAL_FAILURE(
4822 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004823
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4825 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4826 ASSERT_EQ(0, motionArgs.buttonState);
4827 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004828 ASSERT_NO_FATAL_FAILURE(
4829 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004830
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4832 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4833 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4834
4835 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4839 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4840 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004841
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004843 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4845 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004846 ASSERT_NO_FATAL_FAILURE(
4847 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004848
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4850 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4851 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4852 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004853 ASSERT_NO_FATAL_FAILURE(
4854 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004859 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860 ASSERT_EQ(0, motionArgs.buttonState);
4861 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004862 ASSERT_NO_FATAL_FAILURE(
4863 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004864
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4866 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4867 ASSERT_EQ(0, motionArgs.buttonState);
4868 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004869 ASSERT_NO_FATAL_FAILURE(
4870 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004871
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4873 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4874 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4875}
4876
4877TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004879 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880
4881 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4882 mFakePointerController->setPosition(100, 200);
4883 mFakePointerController->setButtonState(0);
4884
4885 NotifyMotionArgs args;
4886
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004891 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4892 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4894 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 +01004895 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004896}
4897
4898TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004899 addConfigurationProperty("cursor.mode", "pointer");
4900 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004901 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004902
4903 NotifyDeviceResetArgs resetArgs;
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4905 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4906 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4907
4908 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4909 mFakePointerController->setPosition(100, 200);
4910 mFakePointerController->setButtonState(0);
4911
4912 NotifyMotionArgs args;
4913
4914 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4919 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4922 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 +01004923 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004924
4925 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4929 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4930 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4932 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4934 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4937 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4938
4939 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004940 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4941 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4943 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4946 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4948 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4949 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4951 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4952
4953 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4958 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4961 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 +01004962 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004963
4964 // Disable pointer capture and check that the device generation got bumped
4965 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004966 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004967 mFakePolicy->setPointerCapture(false);
4968 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004969 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004970
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004972 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4973
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4978 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4981 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 +01004982 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983}
4984
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004985/**
4986 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4987 * pointer acceleration or speed processing should not be applied.
4988 */
4989TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4990 addConfigurationProperty("cursor.mode", "pointer");
4991 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4992 100.f /*high threshold*/, 10.f /*acceleration*/);
4993 mFakePolicy->setVelocityControlParams(testParams);
4994 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4995
4996 NotifyDeviceResetArgs resetArgs;
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4998 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4999 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5000
5001 NotifyMotionArgs args;
5002
5003 // Move and verify scale is applied.
5004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5008 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5009 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5010 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5011 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5012 ASSERT_GT(relX, 10);
5013 ASSERT_GT(relY, 20);
5014
5015 // Enable Pointer Capture
5016 mFakePolicy->setPointerCapture(true);
5017 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5018 NotifyPointerCaptureChangedArgs captureArgs;
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5020 ASSERT_TRUE(captureArgs.request.enable);
5021
5022 // Move and verify scale is not applied.
5023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5027 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5029 ASSERT_EQ(10, args.pointerCoords[0].getX());
5030 ASSERT_EQ(20, args.pointerCoords[0].getY());
5031}
5032
Prabir Pradhan258e2b92022-06-24 18:37:04 +00005033TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5034 addConfigurationProperty("cursor.mode", "pointer");
5035 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5036
5037 NotifyDeviceResetArgs resetArgs;
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5039 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5040 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5041
5042 // Ensure the display is rotated.
5043 prepareDisplay(DISPLAY_ORIENTATION_90);
5044
5045 NotifyMotionArgs args;
5046
5047 // Verify that the coordinates are rotated.
5048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5052 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5053 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5054 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5055 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5056
5057 // Enable Pointer Capture.
5058 mFakePolicy->setPointerCapture(true);
5059 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5060 NotifyPointerCaptureChangedArgs captureArgs;
5061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5062 ASSERT_TRUE(captureArgs.request.enable);
5063
5064 // Move and verify rotation is not applied.
5065 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);
5068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5069 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5071 ASSERT_EQ(10, args.pointerCoords[0].getX());
5072 ASSERT_EQ(20, args.pointerCoords[0].getY());
5073}
5074
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005075TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005076 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005077
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005078 // Set up the default display.
5079 prepareDisplay(DISPLAY_ORIENTATION_90);
5080
5081 // Set up the secondary display as the display on which the pointer should be shown.
5082 // The InputDevice is not associated with any display.
5083 prepareSecondaryDisplay();
5084 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005085 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5086
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005087 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005088 mFakePointerController->setPosition(100, 200);
5089 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005090
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005091 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5096 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5097 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005098 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005099}
5100
5101TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5102 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5103
5104 // Set up the default display.
5105 prepareDisplay(DISPLAY_ORIENTATION_90);
5106
5107 // Set up the secondary display as the display on which the pointer should be shown,
5108 // and associate the InputDevice with the secondary display.
5109 prepareSecondaryDisplay();
5110 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5111 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5112 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5113
5114 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5115 mFakePointerController->setPosition(100, 200);
5116 mFakePointerController->setButtonState(0);
5117
5118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5122 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5123 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
5124 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5125}
5126
5127TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5128 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5129
5130 // Set up the default display as the display on which the pointer should be shown.
5131 prepareDisplay(DISPLAY_ORIENTATION_90);
5132 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5133
5134 // Associate the InputDevice with the secondary display.
5135 prepareSecondaryDisplay();
5136 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5137 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5138
5139 // The mapper should not generate any events because it is associated with a display that is
5140 // different from the pointer display.
5141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005145}
5146
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147// --- TouchInputMapperTest ---
5148
5149class TouchInputMapperTest : public InputMapperTest {
5150protected:
5151 static const int32_t RAW_X_MIN;
5152 static const int32_t RAW_X_MAX;
5153 static const int32_t RAW_Y_MIN;
5154 static const int32_t RAW_Y_MAX;
5155 static const int32_t RAW_TOUCH_MIN;
5156 static const int32_t RAW_TOUCH_MAX;
5157 static const int32_t RAW_TOOL_MIN;
5158 static const int32_t RAW_TOOL_MAX;
5159 static const int32_t RAW_PRESSURE_MIN;
5160 static const int32_t RAW_PRESSURE_MAX;
5161 static const int32_t RAW_ORIENTATION_MIN;
5162 static const int32_t RAW_ORIENTATION_MAX;
5163 static const int32_t RAW_DISTANCE_MIN;
5164 static const int32_t RAW_DISTANCE_MAX;
5165 static const int32_t RAW_TILT_MIN;
5166 static const int32_t RAW_TILT_MAX;
5167 static const int32_t RAW_ID_MIN;
5168 static const int32_t RAW_ID_MAX;
5169 static const int32_t RAW_SLOT_MIN;
5170 static const int32_t RAW_SLOT_MAX;
5171 static const float X_PRECISION;
5172 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005173 static const float X_PRECISION_VIRTUAL;
5174 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175
5176 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005177 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178
5179 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5180
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005181 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005182 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005183
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184 enum Axes {
5185 POSITION = 1 << 0,
5186 TOUCH = 1 << 1,
5187 TOOL = 1 << 2,
5188 PRESSURE = 1 << 3,
5189 ORIENTATION = 1 << 4,
5190 MINOR = 1 << 5,
5191 ID = 1 << 6,
5192 DISTANCE = 1 << 7,
5193 TILT = 1 << 8,
5194 SLOT = 1 << 9,
5195 TOOL_TYPE = 1 << 10,
5196 };
5197
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005198 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5199 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005200 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005202 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203 int32_t toRawX(float displayX);
5204 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005205 int32_t toRotatedRawX(float displayX);
5206 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005207 float toCookedX(float rawX, float rawY);
5208 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005210 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005212 float toDisplayY(int32_t rawY, int32_t displayHeight);
5213
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214};
5215
5216const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5217const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5218const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5219const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5220const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5221const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5222const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5223const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005224const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5225const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5227const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5228const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5229const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5230const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5231const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5232const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5233const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5234const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5235const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5236const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5237const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005238const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5239 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5240const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5241 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005242const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5243 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244
5245const float TouchInputMapperTest::GEOMETRIC_SCALE =
5246 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5247 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5248
5249const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5250 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5251 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5252};
5253
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005254void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005255 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5256 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005257}
5258
5259void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5260 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5261 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005262}
5263
Santos Cordonfa5cf462017-04-05 10:37:00 -07005264void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005265 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5266 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5267 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005268}
5269
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005271 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5272 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5273 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5274 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275}
5276
Jason Gerecke489fda82012-09-07 17:19:40 -07005277void TouchInputMapperTest::prepareLocationCalibration() {
5278 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5279}
5280
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281int32_t TouchInputMapperTest::toRawX(float displayX) {
5282 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5283}
5284
5285int32_t TouchInputMapperTest::toRawY(float displayY) {
5286 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5287}
5288
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005289int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5290 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5291}
5292
5293int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5294 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5295}
5296
Jason Gerecke489fda82012-09-07 17:19:40 -07005297float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5298 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5299 return rawX;
5300}
5301
5302float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5303 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5304 return rawY;
5305}
5306
Michael Wrightd02c5b62014-02-10 15:10:22 -08005307float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005308 return toDisplayX(rawX, DISPLAY_WIDTH);
5309}
5310
5311float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5312 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313}
5314
5315float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005316 return toDisplayY(rawY, DISPLAY_HEIGHT);
5317}
5318
5319float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5320 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321}
5322
5323
5324// --- SingleTouchInputMapperTest ---
5325
5326class SingleTouchInputMapperTest : public TouchInputMapperTest {
5327protected:
5328 void prepareButtons();
5329 void prepareAxes(int axes);
5330
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005331 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5332 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5333 void processUp(SingleTouchInputMapper& mappery);
5334 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5335 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5336 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5337 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5338 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5339 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340};
5341
5342void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005343 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344}
5345
5346void SingleTouchInputMapperTest::prepareAxes(int axes) {
5347 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005348 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5349 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350 }
5351 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005352 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5353 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 }
5355 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005356 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5357 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358 }
5359 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005360 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5361 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362 }
5363 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005364 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5365 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366 }
5367}
5368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005369void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373}
5374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005378}
5379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005380void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382}
5383
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386}
5387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005388void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5389 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391}
5392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395}
5396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005397void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5398 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401}
5402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005403void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5404 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406}
5407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005408void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410}
5411
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 prepareButtons();
5414 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005415 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005417 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418}
5419
5420TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005421 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5422 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 prepareButtons();
5424 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005425 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428}
5429
5430TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 prepareButtons();
5432 prepareAxes(POSITION);
5433 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005434 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005436 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437}
5438
5439TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 prepareButtons();
5441 prepareAxes(POSITION);
5442 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005443 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005445 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446}
5447
5448TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449 addConfigurationProperty("touch.deviceType", "touchScreen");
5450 prepareDisplay(DISPLAY_ORIENTATION_0);
5451 prepareButtons();
5452 prepareAxes(POSITION);
5453 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005454 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455
5456 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005457 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458
5459 // Virtual key is down.
5460 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5461 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5462 processDown(mapper, x, y);
5463 processSync(mapper);
5464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005466 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467
5468 // Virtual key is up.
5469 processUp(mapper);
5470 processSync(mapper);
5471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5472
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005473 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474}
5475
5476TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 addConfigurationProperty("touch.deviceType", "touchScreen");
5478 prepareDisplay(DISPLAY_ORIENTATION_0);
5479 prepareButtons();
5480 prepareAxes(POSITION);
5481 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005482 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483
5484 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005485 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486
5487 // Virtual key is down.
5488 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5489 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5490 processDown(mapper, x, y);
5491 processSync(mapper);
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5493
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005494 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495
5496 // Virtual key is up.
5497 processUp(mapper);
5498 processSync(mapper);
5499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5500
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005501 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502}
5503
5504TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 addConfigurationProperty("touch.deviceType", "touchScreen");
5506 prepareDisplay(DISPLAY_ORIENTATION_0);
5507 prepareButtons();
5508 prepareAxes(POSITION);
5509 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005510 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511
5512 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5513 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005514 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515 ASSERT_TRUE(flags[0]);
5516 ASSERT_FALSE(flags[1]);
5517}
5518
5519TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520 addConfigurationProperty("touch.deviceType", "touchScreen");
5521 prepareDisplay(DISPLAY_ORIENTATION_0);
5522 prepareButtons();
5523 prepareAxes(POSITION);
5524 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005525 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526
arthurhungdcef2dc2020-08-11 14:47:50 +08005527 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528
5529 NotifyKeyArgs args;
5530
5531 // Press virtual key.
5532 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5533 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5534 processDown(mapper, x, y);
5535 processSync(mapper);
5536
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5538 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5539 ASSERT_EQ(DEVICE_ID, args.deviceId);
5540 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5541 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5542 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5543 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5544 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5545 ASSERT_EQ(KEY_HOME, args.scanCode);
5546 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5547 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5548
5549 // Release virtual key.
5550 processUp(mapper);
5551 processSync(mapper);
5552
5553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5554 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5555 ASSERT_EQ(DEVICE_ID, args.deviceId);
5556 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5557 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5558 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5559 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5560 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5561 ASSERT_EQ(KEY_HOME, args.scanCode);
5562 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5563 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5564
5565 // Should not have sent any motions.
5566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5567}
5568
5569TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005570 addConfigurationProperty("touch.deviceType", "touchScreen");
5571 prepareDisplay(DISPLAY_ORIENTATION_0);
5572 prepareButtons();
5573 prepareAxes(POSITION);
5574 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005575 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576
arthurhungdcef2dc2020-08-11 14:47:50 +08005577 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578
5579 NotifyKeyArgs keyArgs;
5580
5581 // Press virtual key.
5582 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5583 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5584 processDown(mapper, x, y);
5585 processSync(mapper);
5586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5588 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5589 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5590 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5591 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5592 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5593 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5594 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5595 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5596 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5597 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5598
5599 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5600 // into the display area.
5601 y -= 100;
5602 processMove(mapper, x, y);
5603 processSync(mapper);
5604
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5606 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5607 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5608 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5609 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5610 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5611 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5612 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5613 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5614 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5615 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5616 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5617
5618 NotifyMotionArgs motionArgs;
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5620 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5621 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5622 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5623 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5624 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5625 ASSERT_EQ(0, motionArgs.flags);
5626 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5627 ASSERT_EQ(0, motionArgs.buttonState);
5628 ASSERT_EQ(0, motionArgs.edgeFlags);
5629 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5630 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5633 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5634 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5635 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5636 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5637
5638 // Keep moving out of bounds. Should generate a pointer move.
5639 y -= 50;
5640 processMove(mapper, x, y);
5641 processSync(mapper);
5642
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5645 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5646 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5647 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5649 ASSERT_EQ(0, motionArgs.flags);
5650 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5651 ASSERT_EQ(0, motionArgs.buttonState);
5652 ASSERT_EQ(0, motionArgs.edgeFlags);
5653 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5654 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5657 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5658 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5659 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5660 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5661
5662 // Release out of bounds. Should generate a pointer up.
5663 processUp(mapper);
5664 processSync(mapper);
5665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5667 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5668 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5669 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5670 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5672 ASSERT_EQ(0, motionArgs.flags);
5673 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5674 ASSERT_EQ(0, motionArgs.buttonState);
5675 ASSERT_EQ(0, motionArgs.edgeFlags);
5676 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5677 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5680 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5681 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5682 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5684
5685 // Should not have sent any more keys or motions.
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5688}
5689
5690TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691 addConfigurationProperty("touch.deviceType", "touchScreen");
5692 prepareDisplay(DISPLAY_ORIENTATION_0);
5693 prepareButtons();
5694 prepareAxes(POSITION);
5695 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005696 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697
arthurhungdcef2dc2020-08-11 14:47:50 +08005698 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699
5700 NotifyMotionArgs motionArgs;
5701
5702 // Initially go down out of bounds.
5703 int32_t x = -10;
5704 int32_t y = -10;
5705 processDown(mapper, x, y);
5706 processSync(mapper);
5707
5708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5709
5710 // Move into the display area. Should generate a pointer down.
5711 x = 50;
5712 y = 75;
5713 processMove(mapper, x, y);
5714 processSync(mapper);
5715
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5717 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5718 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5719 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5720 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5721 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5722 ASSERT_EQ(0, motionArgs.flags);
5723 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5724 ASSERT_EQ(0, motionArgs.buttonState);
5725 ASSERT_EQ(0, motionArgs.edgeFlags);
5726 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5727 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5730 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5731 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5732 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5733 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5734
5735 // Release. Should generate a pointer up.
5736 processUp(mapper);
5737 processSync(mapper);
5738
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5740 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5741 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5742 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5743 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5744 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5745 ASSERT_EQ(0, motionArgs.flags);
5746 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5747 ASSERT_EQ(0, motionArgs.buttonState);
5748 ASSERT_EQ(0, motionArgs.edgeFlags);
5749 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5750 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5753 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5754 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5755 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5756 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5757
5758 // Should not have sent any more keys or motions.
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5761}
5762
Santos Cordonfa5cf462017-04-05 10:37:00 -07005763TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005764 addConfigurationProperty("touch.deviceType", "touchScreen");
5765 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5766
5767 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5768 prepareButtons();
5769 prepareAxes(POSITION);
5770 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005771 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005772
arthurhungdcef2dc2020-08-11 14:47:50 +08005773 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005774
5775 NotifyMotionArgs motionArgs;
5776
5777 // Down.
5778 int32_t x = 100;
5779 int32_t y = 125;
5780 processDown(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_DOWN, 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 // Move.
5805 x += 50;
5806 y += 75;
5807 processMove(mapper, x, y);
5808 processSync(mapper);
5809
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5811 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5812 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5813 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5814 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5815 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5817 ASSERT_EQ(0, motionArgs.flags);
5818 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5819 ASSERT_EQ(0, motionArgs.buttonState);
5820 ASSERT_EQ(0, motionArgs.edgeFlags);
5821 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5822 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5825 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5826 1, 0, 0, 0, 0, 0, 0, 0));
5827 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5828 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5830
5831 // Up.
5832 processUp(mapper);
5833 processSync(mapper);
5834
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5837 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5838 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5839 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5840 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5841 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5842 ASSERT_EQ(0, motionArgs.flags);
5843 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5844 ASSERT_EQ(0, motionArgs.buttonState);
5845 ASSERT_EQ(0, motionArgs.edgeFlags);
5846 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5847 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5850 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5851 1, 0, 0, 0, 0, 0, 0, 0));
5852 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5853 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5854 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5855
5856 // Should not have sent any more keys or motions.
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5859}
5860
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 addConfigurationProperty("touch.deviceType", "touchScreen");
5863 prepareDisplay(DISPLAY_ORIENTATION_0);
5864 prepareButtons();
5865 prepareAxes(POSITION);
5866 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005867 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868
arthurhungdcef2dc2020-08-11 14:47:50 +08005869 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870
5871 NotifyMotionArgs motionArgs;
5872
5873 // Down.
5874 int32_t x = 100;
5875 int32_t y = 125;
5876 processDown(mapper, x, y);
5877 processSync(mapper);
5878
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5880 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5881 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5882 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5883 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5884 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5885 ASSERT_EQ(0, motionArgs.flags);
5886 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5887 ASSERT_EQ(0, motionArgs.buttonState);
5888 ASSERT_EQ(0, motionArgs.edgeFlags);
5889 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5890 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5893 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5894 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5895 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5896 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5897
5898 // Move.
5899 x += 50;
5900 y += 75;
5901 processMove(mapper, x, y);
5902 processSync(mapper);
5903
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5906 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5907 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5908 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5910 ASSERT_EQ(0, motionArgs.flags);
5911 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5912 ASSERT_EQ(0, motionArgs.buttonState);
5913 ASSERT_EQ(0, motionArgs.edgeFlags);
5914 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5915 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5918 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5919 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5920 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5922
5923 // Up.
5924 processUp(mapper);
5925 processSync(mapper);
5926
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5928 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5929 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5930 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5931 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5932 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5933 ASSERT_EQ(0, motionArgs.flags);
5934 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5935 ASSERT_EQ(0, motionArgs.buttonState);
5936 ASSERT_EQ(0, motionArgs.edgeFlags);
5937 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5938 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5939 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5941 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5942 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5943 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5944 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5945
5946 // Should not have sent any more keys or motions.
5947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5949}
5950
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005951TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952 addConfigurationProperty("touch.deviceType", "touchScreen");
5953 prepareButtons();
5954 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005955 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5956 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005957 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958
5959 NotifyMotionArgs args;
5960
5961 // Rotation 90.
5962 prepareDisplay(DISPLAY_ORIENTATION_90);
5963 processDown(mapper, toRawX(50), toRawY(75));
5964 processSync(mapper);
5965
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5967 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5968 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5969
5970 processUp(mapper);
5971 processSync(mapper);
5972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5973}
5974
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005975TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976 addConfigurationProperty("touch.deviceType", "touchScreen");
5977 prepareButtons();
5978 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005979 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5980 // orientation-aware are affected by display rotation.
5981 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005982 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983
5984 NotifyMotionArgs args;
5985
5986 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005987 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988 prepareDisplay(DISPLAY_ORIENTATION_0);
5989 processDown(mapper, toRawX(50), toRawY(75));
5990 processSync(mapper);
5991
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5993 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5994 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5995
5996 processUp(mapper);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5999
6000 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006001 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006003 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004 processSync(mapper);
6005
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6007 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6008 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6009
6010 processUp(mapper);
6011 processSync(mapper);
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6013
6014 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006015 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006016 prepareDisplay(DISPLAY_ORIENTATION_180);
6017 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6018 processSync(mapper);
6019
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6021 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6022 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6023
6024 processUp(mapper);
6025 processSync(mapper);
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6027
6028 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006029 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006031 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032 processSync(mapper);
6033
6034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6035 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6036 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6037
6038 processUp(mapper);
6039 processSync(mapper);
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6041}
6042
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006043TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6044 addConfigurationProperty("touch.deviceType", "touchScreen");
6045 prepareButtons();
6046 prepareAxes(POSITION);
6047 addConfigurationProperty("touch.orientationAware", "1");
6048 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6049 clearViewports();
6050 prepareDisplay(DISPLAY_ORIENTATION_0);
6051 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6052 NotifyMotionArgs args;
6053
6054 // Orientation 0.
6055 processDown(mapper, toRawX(50), toRawY(75));
6056 processSync(mapper);
6057
6058 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6059 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6060 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6061
6062 processUp(mapper);
6063 processSync(mapper);
6064 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6065}
6066
6067TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6068 addConfigurationProperty("touch.deviceType", "touchScreen");
6069 prepareButtons();
6070 prepareAxes(POSITION);
6071 addConfigurationProperty("touch.orientationAware", "1");
6072 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6073 clearViewports();
6074 prepareDisplay(DISPLAY_ORIENTATION_0);
6075 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6076 NotifyMotionArgs args;
6077
6078 // Orientation 90.
6079 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6080 processSync(mapper);
6081
6082 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6083 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6084 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6085
6086 processUp(mapper);
6087 processSync(mapper);
6088 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6089}
6090
6091TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6092 addConfigurationProperty("touch.deviceType", "touchScreen");
6093 prepareButtons();
6094 prepareAxes(POSITION);
6095 addConfigurationProperty("touch.orientationAware", "1");
6096 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6097 clearViewports();
6098 prepareDisplay(DISPLAY_ORIENTATION_0);
6099 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6100 NotifyMotionArgs args;
6101
6102 // Orientation 180.
6103 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6104 processSync(mapper);
6105
6106 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6107 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6108 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6109
6110 processUp(mapper);
6111 processSync(mapper);
6112 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6113}
6114
6115TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6116 addConfigurationProperty("touch.deviceType", "touchScreen");
6117 prepareButtons();
6118 prepareAxes(POSITION);
6119 addConfigurationProperty("touch.orientationAware", "1");
6120 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6121 clearViewports();
6122 prepareDisplay(DISPLAY_ORIENTATION_0);
6123 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6124 NotifyMotionArgs args;
6125
6126 // Orientation 270.
6127 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
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
6139TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6140 addConfigurationProperty("touch.deviceType", "touchScreen");
6141 prepareButtons();
6142 prepareAxes(POSITION);
6143 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6144 // orientation-aware are affected by display rotation.
6145 addConfigurationProperty("touch.orientationAware", "0");
6146 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6147 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6148
6149 NotifyMotionArgs args;
6150
6151 // Orientation 90, Rotation 0.
6152 clearViewports();
6153 prepareDisplay(DISPLAY_ORIENTATION_0);
6154 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6155 processSync(mapper);
6156
6157 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6158 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6159 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6160
6161 processUp(mapper);
6162 processSync(mapper);
6163 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6164
6165 // Orientation 90, Rotation 90.
6166 clearViewports();
6167 prepareDisplay(DISPLAY_ORIENTATION_90);
6168 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6169 processSync(mapper);
6170
6171 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6172 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6173 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6174
6175 processUp(mapper);
6176 processSync(mapper);
6177 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6178
6179 // Orientation 90, Rotation 180.
6180 clearViewports();
6181 prepareDisplay(DISPLAY_ORIENTATION_180);
6182 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6183 processSync(mapper);
6184
6185 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6186 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6187 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6188
6189 processUp(mapper);
6190 processSync(mapper);
6191 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6192
6193 // Orientation 90, Rotation 270.
6194 clearViewports();
6195 prepareDisplay(DISPLAY_ORIENTATION_270);
6196 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6197 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6198 processSync(mapper);
6199
6200 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6201 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6202 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6203
6204 processUp(mapper);
6205 processSync(mapper);
6206 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6207}
6208
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 addConfigurationProperty("touch.deviceType", "touchScreen");
6211 prepareDisplay(DISPLAY_ORIENTATION_0);
6212 prepareButtons();
6213 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006214 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215
6216 // These calculations are based on the input device calibration documentation.
6217 int32_t rawX = 100;
6218 int32_t rawY = 200;
6219 int32_t rawPressure = 10;
6220 int32_t rawToolMajor = 12;
6221 int32_t rawDistance = 2;
6222 int32_t rawTiltX = 30;
6223 int32_t rawTiltY = 110;
6224
6225 float x = toDisplayX(rawX);
6226 float y = toDisplayY(rawY);
6227 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6228 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6229 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6230 float distance = float(rawDistance);
6231
6232 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6233 float tiltScale = M_PI / 180;
6234 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6235 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6236 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6237 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6238
6239 processDown(mapper, rawX, rawY);
6240 processPressure(mapper, rawPressure);
6241 processToolMajor(mapper, rawToolMajor);
6242 processDistance(mapper, rawDistance);
6243 processTilt(mapper, rawTiltX, rawTiltY);
6244 processSync(mapper);
6245
6246 NotifyMotionArgs args;
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6249 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6250 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6251}
6252
Jason Gerecke489fda82012-09-07 17:19:40 -07006253TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006254 addConfigurationProperty("touch.deviceType", "touchScreen");
6255 prepareDisplay(DISPLAY_ORIENTATION_0);
6256 prepareLocationCalibration();
6257 prepareButtons();
6258 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006259 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006260
6261 int32_t rawX = 100;
6262 int32_t rawY = 200;
6263
6264 float x = toDisplayX(toCookedX(rawX, rawY));
6265 float y = toDisplayY(toCookedY(rawX, rawY));
6266
6267 processDown(mapper, rawX, rawY);
6268 processSync(mapper);
6269
6270 NotifyMotionArgs args;
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6273 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6274}
6275
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277 addConfigurationProperty("touch.deviceType", "touchScreen");
6278 prepareDisplay(DISPLAY_ORIENTATION_0);
6279 prepareButtons();
6280 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006281 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282
6283 NotifyMotionArgs motionArgs;
6284 NotifyKeyArgs keyArgs;
6285
6286 processDown(mapper, 100, 200);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6290 ASSERT_EQ(0, motionArgs.buttonState);
6291
6292 // press BTN_LEFT, release BTN_LEFT
6293 processKey(mapper, BTN_LEFT, 1);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6298
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6301 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6302
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 processKey(mapper, BTN_LEFT, 0);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006308
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006311 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312
6313 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6314 processKey(mapper, BTN_RIGHT, 1);
6315 processKey(mapper, BTN_MIDDLE, 1);
6316 processSync(mapper);
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6319 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6320 motionArgs.buttonState);
6321
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6324 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6325
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6328 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6329 motionArgs.buttonState);
6330
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 processKey(mapper, BTN_RIGHT, 0);
6332 processSync(mapper);
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006334 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006336
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340
6341 processKey(mapper, BTN_MIDDLE, 0);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006350
6351 // press BTN_BACK, release BTN_BACK
6352 processKey(mapper, BTN_BACK, 1);
6353 processSync(mapper);
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6355 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6356 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006357
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6361
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6364 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365
6366 processKey(mapper, BTN_BACK, 0);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006371
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006374 ASSERT_EQ(0, motionArgs.buttonState);
6375
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6377 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6378 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6379
6380 // press BTN_SIDE, release BTN_SIDE
6381 processKey(mapper, BTN_SIDE, 1);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6384 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6385 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006386
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6390
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6393 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394
6395 processKey(mapper, BTN_SIDE, 0);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006403 ASSERT_EQ(0, motionArgs.buttonState);
6404
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6406 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6407 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6408
6409 // press BTN_FORWARD, release BTN_FORWARD
6410 processKey(mapper, BTN_FORWARD, 1);
6411 processSync(mapper);
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6413 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6414 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006418 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6419
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6422 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423
6424 processKey(mapper, BTN_FORWARD, 0);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006432 ASSERT_EQ(0, motionArgs.buttonState);
6433
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6435 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6436 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6437
6438 // press BTN_EXTRA, release BTN_EXTRA
6439 processKey(mapper, BTN_EXTRA, 1);
6440 processSync(mapper);
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6442 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6443 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006444
Michael Wrightd02c5b62014-02-10 15:10:22 -08006445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006447 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006452
6453 processKey(mapper, BTN_EXTRA, 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);
6462
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6464 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6465 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6466
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6468
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469 // press BTN_STYLUS, release BTN_STYLUS
6470 processKey(mapper, BTN_STYLUS, 1);
6471 processSync(mapper);
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006474 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6475
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6478 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006479
6480 processKey(mapper, BTN_STYLUS, 0);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006484 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006485
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006488 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006489
6490 // press BTN_STYLUS2, release BTN_STYLUS2
6491 processKey(mapper, BTN_STYLUS2, 1);
6492 processSync(mapper);
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006495 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6496
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6499 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500
6501 processKey(mapper, BTN_STYLUS2, 0);
6502 processSync(mapper);
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006505 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006506
6507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006509 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006510
6511 // release touch
6512 processUp(mapper);
6513 processSync(mapper);
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6516 ASSERT_EQ(0, motionArgs.buttonState);
6517}
6518
6519TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520 addConfigurationProperty("touch.deviceType", "touchScreen");
6521 prepareDisplay(DISPLAY_ORIENTATION_0);
6522 prepareButtons();
6523 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006524 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525
6526 NotifyMotionArgs motionArgs;
6527
6528 // default tool type is finger
6529 processDown(mapper, 100, 200);
6530 processSync(mapper);
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6534
6535 // eraser
6536 processKey(mapper, BTN_TOOL_RUBBER, 1);
6537 processSync(mapper);
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6541
6542 // stylus
6543 processKey(mapper, BTN_TOOL_RUBBER, 0);
6544 processKey(mapper, BTN_TOOL_PEN, 1);
6545 processSync(mapper);
6546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6549
6550 // brush
6551 processKey(mapper, BTN_TOOL_PEN, 0);
6552 processKey(mapper, BTN_TOOL_BRUSH, 1);
6553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6557
6558 // pencil
6559 processKey(mapper, BTN_TOOL_BRUSH, 0);
6560 processKey(mapper, BTN_TOOL_PENCIL, 1);
6561 processSync(mapper);
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6565
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006566 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006567 processKey(mapper, BTN_TOOL_PENCIL, 0);
6568 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6573
6574 // mouse
6575 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6576 processKey(mapper, BTN_TOOL_MOUSE, 1);
6577 processSync(mapper);
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6581
6582 // lens
6583 processKey(mapper, BTN_TOOL_MOUSE, 0);
6584 processKey(mapper, BTN_TOOL_LENS, 1);
6585 processSync(mapper);
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6589
6590 // double-tap
6591 processKey(mapper, BTN_TOOL_LENS, 0);
6592 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6593 processSync(mapper);
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6597
6598 // triple-tap
6599 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6600 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6601 processSync(mapper);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6605
6606 // quad-tap
6607 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6608 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6613
6614 // finger
6615 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6616 processKey(mapper, BTN_TOOL_FINGER, 1);
6617 processSync(mapper);
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6621
6622 // stylus trumps finger
6623 processKey(mapper, BTN_TOOL_PEN, 1);
6624 processSync(mapper);
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6628
6629 // eraser trumps stylus
6630 processKey(mapper, BTN_TOOL_RUBBER, 1);
6631 processSync(mapper);
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6635
6636 // mouse trumps eraser
6637 processKey(mapper, BTN_TOOL_MOUSE, 1);
6638 processSync(mapper);
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6642
6643 // back to default tool type
6644 processKey(mapper, BTN_TOOL_MOUSE, 0);
6645 processKey(mapper, BTN_TOOL_RUBBER, 0);
6646 processKey(mapper, BTN_TOOL_PEN, 0);
6647 processKey(mapper, BTN_TOOL_FINGER, 0);
6648 processSync(mapper);
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6652}
6653
6654TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006655 addConfigurationProperty("touch.deviceType", "touchScreen");
6656 prepareDisplay(DISPLAY_ORIENTATION_0);
6657 prepareButtons();
6658 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006659 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006660 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006661
6662 NotifyMotionArgs motionArgs;
6663
6664 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6665 processKey(mapper, BTN_TOOL_FINGER, 1);
6666 processMove(mapper, 100, 200);
6667 processSync(mapper);
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6671 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6672
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6676 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6677
6678 // move a little
6679 processMove(mapper, 150, 250);
6680 processSync(mapper);
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6684 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6685
6686 // down when BTN_TOUCH is pressed, pressure defaults to 1
6687 processKey(mapper, BTN_TOUCH, 1);
6688 processSync(mapper);
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6692 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6693
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6698
6699 // up when BTN_TOUCH is released, hover restored
6700 processKey(mapper, BTN_TOUCH, 0);
6701 processSync(mapper);
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6703 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6705 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6706
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6710 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6711
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6715 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6716
6717 // exit hover when pointer goes away
6718 processKey(mapper, BTN_TOOL_FINGER, 0);
6719 processSync(mapper);
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6721 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6723 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6724}
6725
6726TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006727 addConfigurationProperty("touch.deviceType", "touchScreen");
6728 prepareDisplay(DISPLAY_ORIENTATION_0);
6729 prepareButtons();
6730 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006731 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006732
6733 NotifyMotionArgs motionArgs;
6734
6735 // initially hovering because pressure is 0
6736 processDown(mapper, 100, 200);
6737 processPressure(mapper, 0);
6738 processSync(mapper);
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6742 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6743
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6745 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6747 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6748
6749 // move a little
6750 processMove(mapper, 150, 250);
6751 processSync(mapper);
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6755 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6756
6757 // down when pressure is non-zero
6758 processPressure(mapper, RAW_PRESSURE_MAX);
6759 processSync(mapper);
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6763 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6764
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6768 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6769
6770 // up when pressure becomes 0, hover restored
6771 processPressure(mapper, 0);
6772 processSync(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6774 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6776 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6777
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6779 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6781 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6782
6783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6784 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6786 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6787
6788 // exit hover when pointer goes away
6789 processUp(mapper);
6790 processSync(mapper);
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6794 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6795}
6796
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006797TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6798 addConfigurationProperty("touch.deviceType", "touchScreen");
6799 prepareDisplay(DISPLAY_ORIENTATION_0);
6800 prepareButtons();
6801 prepareAxes(POSITION | PRESSURE);
6802 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6803 NotifyMotionArgs motionArgs;
6804
6805 // Set the initial state for the touch pointer.
6806 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6807 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6808 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6809 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6810
6811 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
6812 // state by reading the current axis values.
6813 mapper.reset(ARBITRARY_TIME);
6814
6815 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6816 // the recreated touch state to generate a down event.
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6819 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6820
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6822}
6823
Prabir Pradhan588d6392022-11-02 20:05:13 +00006824TEST_F(SingleTouchInputMapperTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
6825 std::shared_ptr<FakePointerController> fakePointerController =
6826 std::make_shared<FakePointerController>();
6827 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
6828 fakePointerController->setPosition(100, 200);
6829 fakePointerController->setButtonState(0);
6830 mFakePolicy->setPointerController(fakePointerController);
6831
6832 addConfigurationProperty("touch.deviceType", "pointer");
6833 prepareDisplay(DISPLAY_ORIENTATION_0);
6834 prepareButtons();
6835 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6836 prepareAxes(POSITION);
6837 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6839
6840 // Start a stylus gesture.
6841 processKey(mapper, BTN_TOOL_PEN, 1);
6842 processDown(mapper, 100, 200);
6843 processSync(mapper);
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6845 AllOf(WithAction(AMOTION_EVENT_ACTION_DOWN),
6846 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
6847 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6848 // TODO(b/257078296): Pointer mode generates extra event.
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6850 AllOf(WithAction(AMOTION_EVENT_ACTION_MOVE),
6851 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
6852 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6854
6855 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
6856 // gesture should be disabled.
6857 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6858 viewport->isActive = false;
6859 mFakePolicy->updateViewport(*viewport);
6860 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6862 AllOf(WithAction(AMOTION_EVENT_ACTION_CANCEL),
6863 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
6864 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6865 // TODO(b/257078296): Pointer mode generates extra event.
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6867 AllOf(WithAction(AMOTION_EVENT_ACTION_CANCEL),
6868 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
6869 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6871}
6872
lilinnan687e58f2022-07-19 16:00:50 +08006873TEST_F(SingleTouchInputMapperTest,
6874 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6875 addConfigurationProperty("touch.deviceType", "touchScreen");
6876 prepareDisplay(DISPLAY_ORIENTATION_0);
6877 prepareButtons();
6878 prepareAxes(POSITION);
6879 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6880 NotifyMotionArgs motionArgs;
6881
6882 // Down.
6883 int32_t x = 100;
6884 int32_t y = 200;
6885 processDown(mapper, x, y);
6886 processSync(mapper);
6887
6888 // We should receive a down event
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6890 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6891
6892 // Change display id
6893 clearViewports();
6894 prepareSecondaryDisplay(ViewportType::INTERNAL);
6895
6896 // We should receive a cancel event
6897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6898 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6899 // Then receive reset called
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6901}
6902
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006903TEST_F(SingleTouchInputMapperTest,
6904 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6905 addConfigurationProperty("touch.deviceType", "touchScreen");
6906 prepareDisplay(DISPLAY_ORIENTATION_0);
6907 prepareButtons();
6908 prepareAxes(POSITION);
6909 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6911 NotifyMotionArgs motionArgs;
6912
6913 // Start a new gesture.
6914 processDown(mapper, 100, 200);
6915 processSync(mapper);
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6917 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6918
6919 // Make the viewport inactive. This will put the device in disabled mode.
6920 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6921 viewport->isActive = false;
6922 mFakePolicy->updateViewport(*viewport);
6923 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6924
6925 // We should receive a cancel event for the ongoing gesture.
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6927 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6928 // Then we should be notified that the device was reset.
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6930
6931 // No events are generated while the viewport is inactive.
6932 processMove(mapper, 101, 201);
6933 processSync(mapper);
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006934 processUp(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006935 processSync(mapper);
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6937
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006938 // Start a new gesture while the viewport is still inactive.
6939 processDown(mapper, 300, 400);
6940 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6941 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6942 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6943 processSync(mapper);
6944
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006945 // Make the viewport active again. The device should resume processing events.
6946 viewport->isActive = true;
6947 mFakePolicy->updateViewport(*viewport);
6948 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6949
6950 // The device is reset because it changes back to direct mode, without generating any events.
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6953
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006954 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006955 processSync(mapper);
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6957 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6958
6959 // No more events.
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6962}
6963
Prabir Pradhan5632d622021-09-06 07:57:20 -07006964// --- TouchDisplayProjectionTest ---
6965
6966class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6967public:
6968 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6969 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6970 // rotated equivalent of the given un-rotated physical display bounds.
6971 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6972 uint32_t inverseRotationFlags;
6973 auto width = DISPLAY_WIDTH;
6974 auto height = DISPLAY_HEIGHT;
6975 switch (orientation) {
6976 case DISPLAY_ORIENTATION_90:
6977 inverseRotationFlags = ui::Transform::ROT_270;
6978 std::swap(width, height);
6979 break;
6980 case DISPLAY_ORIENTATION_180:
6981 inverseRotationFlags = ui::Transform::ROT_180;
6982 break;
6983 case DISPLAY_ORIENTATION_270:
6984 inverseRotationFlags = ui::Transform::ROT_90;
6985 std::swap(width, height);
6986 break;
6987 case DISPLAY_ORIENTATION_0:
6988 inverseRotationFlags = ui::Transform::ROT_0;
6989 break;
6990 default:
6991 FAIL() << "Invalid orientation: " << orientation;
6992 }
6993
6994 const ui::Transform rotation(inverseRotationFlags, width, height);
6995 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6996
6997 std::optional<DisplayViewport> internalViewport =
6998 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6999 DisplayViewport& v = *internalViewport;
7000 v.displayId = DISPLAY_ID;
7001 v.orientation = orientation;
7002
7003 v.logicalLeft = 0;
7004 v.logicalTop = 0;
7005 v.logicalRight = 100;
7006 v.logicalBottom = 100;
7007
7008 v.physicalLeft = rotatedPhysicalDisplay.left;
7009 v.physicalTop = rotatedPhysicalDisplay.top;
7010 v.physicalRight = rotatedPhysicalDisplay.right;
7011 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7012
7013 v.deviceWidth = width;
7014 v.deviceHeight = height;
7015
7016 v.isActive = true;
7017 v.uniqueId = UNIQUE_ID;
7018 v.type = ViewportType::INTERNAL;
7019 mFakePolicy->updateViewport(v);
7020 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7021 }
7022
7023 void assertReceivedMove(const Point& point) {
7024 NotifyMotionArgs motionArgs;
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7027 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7029 1, 0, 0, 0, 0, 0, 0, 0));
7030 }
7031};
7032
7033TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7034 addConfigurationProperty("touch.deviceType", "touchScreen");
7035 prepareDisplay(DISPLAY_ORIENTATION_0);
7036
7037 prepareButtons();
7038 prepareAxes(POSITION);
7039 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7040
7041 NotifyMotionArgs motionArgs;
7042
7043 // Configure the DisplayViewport such that the logical display maps to a subsection of
7044 // the display panel called the physical display. Here, the physical display is bounded by the
7045 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7046 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7047 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7048 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7049
7050 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7051 DISPLAY_ORIENTATION_270}) {
7052 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7053
7054 // Touches outside the physical display should be ignored, and should not generate any
7055 // events. Ensure touches at the following points that lie outside of the physical display
7056 // area do not generate any events.
7057 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7058 processDown(mapper, toRawX(point.x), toRawY(point.y));
7059 processSync(mapper);
7060 processUp(mapper);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7063 << "Unexpected event generated for touch outside physical display at point: "
7064 << point.x << ", " << point.y;
7065 }
7066 }
7067}
7068
7069TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7070 addConfigurationProperty("touch.deviceType", "touchScreen");
7071 prepareDisplay(DISPLAY_ORIENTATION_0);
7072
7073 prepareButtons();
7074 prepareAxes(POSITION);
7075 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7076
7077 NotifyMotionArgs motionArgs;
7078
7079 // Configure the DisplayViewport such that the logical display maps to a subsection of
7080 // the display panel called the physical display. Here, the physical display is bounded by the
7081 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7082 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7083
7084 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7085 DISPLAY_ORIENTATION_270}) {
7086 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7087
7088 // Touches that start outside the physical display should be ignored until it enters the
7089 // physical display bounds, at which point it should generate a down event. Start a touch at
7090 // the point (5, 100), which is outside the physical display bounds.
7091 static const Point kOutsidePoint{5, 100};
7092 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7093 processSync(mapper);
7094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7095
7096 // Move the touch into the physical display area. This should generate a pointer down.
7097 processMove(mapper, toRawX(11), toRawY(21));
7098 processSync(mapper);
7099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7100 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7101 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7102 ASSERT_NO_FATAL_FAILURE(
7103 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7104
7105 // Move the touch inside the physical display area. This should generate a pointer move.
7106 processMove(mapper, toRawX(69), toRawY(159));
7107 processSync(mapper);
7108 assertReceivedMove({69, 159});
7109
7110 // Move outside the physical display area. Since the pointer is already down, this should
7111 // now continue generating events.
7112 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7113 processSync(mapper);
7114 assertReceivedMove(kOutsidePoint);
7115
7116 // Release. This should generate a pointer up.
7117 processUp(mapper);
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7120 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7122 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7123
7124 // Ensure no more events were generated.
7125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7127 }
7128}
7129
Michael Wrightd02c5b62014-02-10 15:10:22 -08007130// --- MultiTouchInputMapperTest ---
7131
7132class MultiTouchInputMapperTest : public TouchInputMapperTest {
7133protected:
7134 void prepareAxes(int axes);
7135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007136 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7137 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7138 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7139 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7140 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7141 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7142 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7143 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7144 void processId(MultiTouchInputMapper& mapper, int32_t id);
7145 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7146 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7147 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7148 void processMTSync(MultiTouchInputMapper& mapper);
7149 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150};
7151
7152void MultiTouchInputMapperTest::prepareAxes(int axes) {
7153 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007154 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7155 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156 }
7157 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007158 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7159 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007161 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7162 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163 }
7164 }
7165 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007166 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7167 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007169 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007170 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171 }
7172 }
7173 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007174 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7175 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176 }
7177 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007178 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7179 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180 }
7181 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007182 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7183 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184 }
7185 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007186 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7187 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188 }
7189 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007190 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7191 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007192 }
7193 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007194 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195 }
7196}
7197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007198void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7199 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202}
7203
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007204void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7205 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007207}
7208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007209void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7210 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007212}
7213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007214void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216}
7217
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007218void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007220}
7221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007222void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7223 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007225}
7226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007227void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229}
7230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007231void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233}
7234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007235void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237}
7238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007239void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241}
7242
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007243void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007245}
7246
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007247void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7248 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007250}
7251
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007252void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007254}
7255
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007256void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007258}
7259
Michael Wrightd02c5b62014-02-10 15:10:22 -08007260TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007261 addConfigurationProperty("touch.deviceType", "touchScreen");
7262 prepareDisplay(DISPLAY_ORIENTATION_0);
7263 prepareAxes(POSITION);
7264 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007265 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007266
arthurhungdcef2dc2020-08-11 14:47:50 +08007267 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007268
7269 NotifyMotionArgs motionArgs;
7270
7271 // Two fingers down at once.
7272 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7273 processPosition(mapper, x1, y1);
7274 processMTSync(mapper);
7275 processPosition(mapper, x2, y2);
7276 processMTSync(mapper);
7277 processSync(mapper);
7278
7279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7280 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7281 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7282 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7283 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7284 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7285 ASSERT_EQ(0, motionArgs.flags);
7286 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7287 ASSERT_EQ(0, motionArgs.buttonState);
7288 ASSERT_EQ(0, motionArgs.edgeFlags);
7289 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7290 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7293 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7294 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7295 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7297
7298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7299 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7300 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7301 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7302 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007303 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304 ASSERT_EQ(0, motionArgs.flags);
7305 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7306 ASSERT_EQ(0, motionArgs.buttonState);
7307 ASSERT_EQ(0, motionArgs.edgeFlags);
7308 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7309 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7311 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7314 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7316 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7317 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7318 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7320
7321 // Move.
7322 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7323 processPosition(mapper, x1, y1);
7324 processMTSync(mapper);
7325 processPosition(mapper, x2, y2);
7326 processMTSync(mapper);
7327 processSync(mapper);
7328
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7330 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7331 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7332 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7333 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7335 ASSERT_EQ(0, motionArgs.flags);
7336 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7337 ASSERT_EQ(0, motionArgs.buttonState);
7338 ASSERT_EQ(0, motionArgs.edgeFlags);
7339 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7342 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7345 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7347 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7348 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7349 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7351
7352 // First finger up.
7353 x2 += 15; y2 -= 20;
7354 processPosition(mapper, x2, y2);
7355 processMTSync(mapper);
7356 processSync(mapper);
7357
7358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7359 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7360 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7361 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7362 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007363 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007364 ASSERT_EQ(0, motionArgs.flags);
7365 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7366 ASSERT_EQ(0, motionArgs.buttonState);
7367 ASSERT_EQ(0, motionArgs.edgeFlags);
7368 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7369 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7371 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7374 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7376 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7377 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7378 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7380
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7383 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7384 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7385 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7387 ASSERT_EQ(0, motionArgs.flags);
7388 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7389 ASSERT_EQ(0, motionArgs.buttonState);
7390 ASSERT_EQ(0, motionArgs.edgeFlags);
7391 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7392 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7395 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7396 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7397 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7399
7400 // Move.
7401 x2 += 20; y2 -= 25;
7402 processPosition(mapper, x2, y2);
7403 processMTSync(mapper);
7404 processSync(mapper);
7405
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7407 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7408 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7409 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7410 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7412 ASSERT_EQ(0, motionArgs.flags);
7413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7414 ASSERT_EQ(0, motionArgs.buttonState);
7415 ASSERT_EQ(0, motionArgs.edgeFlags);
7416 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7417 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7420 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7421 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7422 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7424
7425 // New finger down.
7426 int32_t x3 = 700, y3 = 300;
7427 processPosition(mapper, x2, y2);
7428 processMTSync(mapper);
7429 processPosition(mapper, x3, y3);
7430 processMTSync(mapper);
7431 processSync(mapper);
7432
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7434 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7435 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7436 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7437 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007438 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439 ASSERT_EQ(0, motionArgs.flags);
7440 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7441 ASSERT_EQ(0, motionArgs.buttonState);
7442 ASSERT_EQ(0, motionArgs.edgeFlags);
7443 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7444 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7446 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7449 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7451 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7452 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7453 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7455
7456 // Second finger up.
7457 x3 += 30; y3 -= 20;
7458 processPosition(mapper, x3, y3);
7459 processMTSync(mapper);
7460 processSync(mapper);
7461
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7463 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7464 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7465 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7466 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007467 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007468 ASSERT_EQ(0, motionArgs.flags);
7469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7470 ASSERT_EQ(0, motionArgs.buttonState);
7471 ASSERT_EQ(0, motionArgs.edgeFlags);
7472 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7475 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7478 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7480 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7481 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7482 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7484
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7486 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7487 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7488 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7489 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7491 ASSERT_EQ(0, motionArgs.flags);
7492 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7493 ASSERT_EQ(0, motionArgs.buttonState);
7494 ASSERT_EQ(0, motionArgs.edgeFlags);
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(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7500 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7501 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7502 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7503
7504 // Last finger up.
7505 processMTSync(mapper);
7506 processSync(mapper);
7507
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7509 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7510 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7511 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7512 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7513 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7514 ASSERT_EQ(0, motionArgs.flags);
7515 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7516 ASSERT_EQ(0, motionArgs.buttonState);
7517 ASSERT_EQ(0, motionArgs.edgeFlags);
7518 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7519 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7522 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7523 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7524 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7525 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7526
7527 // Should not have sent any more keys or motions.
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7530}
7531
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007532TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7533 addConfigurationProperty("touch.deviceType", "touchScreen");
7534 prepareDisplay(DISPLAY_ORIENTATION_0);
7535
7536 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7537 /*fuzz*/ 0, /*resolution*/ 10);
7538 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7539 /*fuzz*/ 0, /*resolution*/ 11);
7540 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7541 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7542 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7543 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7544 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7545 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7546 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7547 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7548
7549 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7550
7551 // X and Y axes
7552 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7553 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7554 // Touch major and minor
7555 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7556 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7557 // Tool major and minor
7558 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7559 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7560}
7561
7562TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7563 addConfigurationProperty("touch.deviceType", "touchScreen");
7564 prepareDisplay(DISPLAY_ORIENTATION_0);
7565
7566 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7567 /*fuzz*/ 0, /*resolution*/ 10);
7568 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7569 /*fuzz*/ 0, /*resolution*/ 11);
7570
7571 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7572
7573 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7574
7575 // Touch major and minor
7576 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7577 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7578 // Tool major and minor
7579 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7580 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7581}
7582
Michael Wrightd02c5b62014-02-10 15:10:22 -08007583TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007584 addConfigurationProperty("touch.deviceType", "touchScreen");
7585 prepareDisplay(DISPLAY_ORIENTATION_0);
7586 prepareAxes(POSITION | ID);
7587 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007588 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589
arthurhungdcef2dc2020-08-11 14:47:50 +08007590 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007591
7592 NotifyMotionArgs motionArgs;
7593
7594 // Two fingers down at once.
7595 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7596 processPosition(mapper, x1, y1);
7597 processId(mapper, 1);
7598 processMTSync(mapper);
7599 processPosition(mapper, x2, y2);
7600 processId(mapper, 2);
7601 processMTSync(mapper);
7602 processSync(mapper);
7603
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7606 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7607 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7610 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7611
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007613 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007614 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7615 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7617 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7620 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7622 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7623
7624 // Move.
7625 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7626 processPosition(mapper, x1, y1);
7627 processId(mapper, 1);
7628 processMTSync(mapper);
7629 processPosition(mapper, x2, y2);
7630 processId(mapper, 2);
7631 processMTSync(mapper);
7632 processSync(mapper);
7633
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7636 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7637 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7639 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7642 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7644 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7645
7646 // First finger up.
7647 x2 += 15; y2 -= 20;
7648 processPosition(mapper, x2, y2);
7649 processId(mapper, 2);
7650 processMTSync(mapper);
7651 processSync(mapper);
7652
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007654 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007655 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7656 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7658 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7661 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7663 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7664
7665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7667 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7668 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7671 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7672
7673 // Move.
7674 x2 += 20; y2 -= 25;
7675 processPosition(mapper, x2, y2);
7676 processId(mapper, 2);
7677 processMTSync(mapper);
7678 processSync(mapper);
7679
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7682 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7683 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7686 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7687
7688 // New finger down.
7689 int32_t x3 = 700, y3 = 300;
7690 processPosition(mapper, x2, y2);
7691 processId(mapper, 2);
7692 processMTSync(mapper);
7693 processPosition(mapper, x3, y3);
7694 processId(mapper, 3);
7695 processMTSync(mapper);
7696 processSync(mapper);
7697
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007699 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007700 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7701 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7703 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7706 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7708 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7709
7710 // Second finger up.
7711 x3 += 30; y3 -= 20;
7712 processPosition(mapper, x3, y3);
7713 processId(mapper, 3);
7714 processMTSync(mapper);
7715 processSync(mapper);
7716
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007718 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007719 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7720 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7722 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7725 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7727 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7728
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7731 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7732 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7735 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7736
7737 // Last finger up.
7738 processMTSync(mapper);
7739 processSync(mapper);
7740
7741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7742 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7743 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7744 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7747 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7748
7749 // Should not have sent any more keys or motions.
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7752}
7753
7754TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007755 addConfigurationProperty("touch.deviceType", "touchScreen");
7756 prepareDisplay(DISPLAY_ORIENTATION_0);
7757 prepareAxes(POSITION | ID | SLOT);
7758 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007759 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007760
arthurhungdcef2dc2020-08-11 14:47:50 +08007761 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007762
7763 NotifyMotionArgs motionArgs;
7764
7765 // Two fingers down at once.
7766 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7767 processPosition(mapper, x1, y1);
7768 processId(mapper, 1);
7769 processSlot(mapper, 1);
7770 processPosition(mapper, x2, y2);
7771 processId(mapper, 2);
7772 processSync(mapper);
7773
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7776 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7780 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7781
7782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007783 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007784 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7785 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7787 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7790 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7792 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7793
7794 // Move.
7795 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7796 processSlot(mapper, 0);
7797 processPosition(mapper, x1, y1);
7798 processSlot(mapper, 1);
7799 processPosition(mapper, x2, y2);
7800 processSync(mapper);
7801
7802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7804 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7805 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7807 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7810 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7812 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7813
7814 // First finger up.
7815 x2 += 15; y2 -= 20;
7816 processSlot(mapper, 0);
7817 processId(mapper, -1);
7818 processSlot(mapper, 1);
7819 processPosition(mapper, x2, y2);
7820 processSync(mapper);
7821
7822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007823 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007824 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7825 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7827 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7830 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7832 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7833
7834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7836 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7837 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7840 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7841
7842 // Move.
7843 x2 += 20; y2 -= 25;
7844 processPosition(mapper, x2, y2);
7845 processSync(mapper);
7846
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7849 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7850 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7853 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7854
7855 // New finger down.
7856 int32_t x3 = 700, y3 = 300;
7857 processPosition(mapper, x2, y2);
7858 processSlot(mapper, 0);
7859 processId(mapper, 3);
7860 processPosition(mapper, x3, y3);
7861 processSync(mapper);
7862
7863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007864 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007865 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7866 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7868 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7871 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7873 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7874
7875 // Second finger up.
7876 x3 += 30; y3 -= 20;
7877 processSlot(mapper, 1);
7878 processId(mapper, -1);
7879 processSlot(mapper, 0);
7880 processPosition(mapper, x3, y3);
7881 processSync(mapper);
7882
7883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007884 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7886 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7888 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7891 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7893 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7894
7895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7897 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7898 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7901 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7902
7903 // Last finger up.
7904 processId(mapper, -1);
7905 processSync(mapper);
7906
7907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7908 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7909 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7910 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7913 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7914
7915 // Should not have sent any more keys or motions.
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7918}
7919
7920TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921 addConfigurationProperty("touch.deviceType", "touchScreen");
7922 prepareDisplay(DISPLAY_ORIENTATION_0);
7923 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007924 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925
7926 // These calculations are based on the input device calibration documentation.
7927 int32_t rawX = 100;
7928 int32_t rawY = 200;
7929 int32_t rawTouchMajor = 7;
7930 int32_t rawTouchMinor = 6;
7931 int32_t rawToolMajor = 9;
7932 int32_t rawToolMinor = 8;
7933 int32_t rawPressure = 11;
7934 int32_t rawDistance = 0;
7935 int32_t rawOrientation = 3;
7936 int32_t id = 5;
7937
7938 float x = toDisplayX(rawX);
7939 float y = toDisplayY(rawY);
7940 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7941 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7942 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7943 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7944 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7945 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7946 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7947 float distance = float(rawDistance);
7948
7949 processPosition(mapper, rawX, rawY);
7950 processTouchMajor(mapper, rawTouchMajor);
7951 processTouchMinor(mapper, rawTouchMinor);
7952 processToolMajor(mapper, rawToolMajor);
7953 processToolMinor(mapper, rawToolMinor);
7954 processPressure(mapper, rawPressure);
7955 processOrientation(mapper, rawOrientation);
7956 processDistance(mapper, rawDistance);
7957 processId(mapper, id);
7958 processMTSync(mapper);
7959 processSync(mapper);
7960
7961 NotifyMotionArgs args;
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7963 ASSERT_EQ(0, args.pointerProperties[0].id);
7964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7965 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7966 orientation, distance));
7967}
7968
7969TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 addConfigurationProperty("touch.deviceType", "touchScreen");
7971 prepareDisplay(DISPLAY_ORIENTATION_0);
7972 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7973 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007974 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975
7976 // These calculations are based on the input device calibration documentation.
7977 int32_t rawX = 100;
7978 int32_t rawY = 200;
7979 int32_t rawTouchMajor = 140;
7980 int32_t rawTouchMinor = 120;
7981 int32_t rawToolMajor = 180;
7982 int32_t rawToolMinor = 160;
7983
7984 float x = toDisplayX(rawX);
7985 float y = toDisplayY(rawY);
7986 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7987 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7988 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7989 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7990 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7991
7992 processPosition(mapper, rawX, rawY);
7993 processTouchMajor(mapper, rawTouchMajor);
7994 processTouchMinor(mapper, rawTouchMinor);
7995 processToolMajor(mapper, rawToolMajor);
7996 processToolMinor(mapper, rawToolMinor);
7997 processMTSync(mapper);
7998 processSync(mapper);
7999
8000 NotifyMotionArgs args;
8001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8003 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8004}
8005
8006TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007 addConfigurationProperty("touch.deviceType", "touchScreen");
8008 prepareDisplay(DISPLAY_ORIENTATION_0);
8009 prepareAxes(POSITION | TOUCH | TOOL);
8010 addConfigurationProperty("touch.size.calibration", "diameter");
8011 addConfigurationProperty("touch.size.scale", "10");
8012 addConfigurationProperty("touch.size.bias", "160");
8013 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008014 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015
8016 // These calculations are based on the input device calibration documentation.
8017 // Note: We only provide a single common touch/tool value because the device is assumed
8018 // not to emit separate values for each pointer (isSummed = 1).
8019 int32_t rawX = 100;
8020 int32_t rawY = 200;
8021 int32_t rawX2 = 150;
8022 int32_t rawY2 = 250;
8023 int32_t rawTouchMajor = 5;
8024 int32_t rawToolMajor = 8;
8025
8026 float x = toDisplayX(rawX);
8027 float y = toDisplayY(rawY);
8028 float x2 = toDisplayX(rawX2);
8029 float y2 = toDisplayY(rawY2);
8030 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8031 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8032 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8033
8034 processPosition(mapper, rawX, rawY);
8035 processTouchMajor(mapper, rawTouchMajor);
8036 processToolMajor(mapper, rawToolMajor);
8037 processMTSync(mapper);
8038 processPosition(mapper, rawX2, rawY2);
8039 processTouchMajor(mapper, rawTouchMajor);
8040 processToolMajor(mapper, rawToolMajor);
8041 processMTSync(mapper);
8042 processSync(mapper);
8043
8044 NotifyMotionArgs args;
8045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8046 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8047
8048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008049 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008050 ASSERT_EQ(size_t(2), args.pointerCount);
8051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8052 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8054 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8055}
8056
8057TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058 addConfigurationProperty("touch.deviceType", "touchScreen");
8059 prepareDisplay(DISPLAY_ORIENTATION_0);
8060 prepareAxes(POSITION | TOUCH | TOOL);
8061 addConfigurationProperty("touch.size.calibration", "area");
8062 addConfigurationProperty("touch.size.scale", "43");
8063 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008064 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065
8066 // These calculations are based on the input device calibration documentation.
8067 int32_t rawX = 100;
8068 int32_t rawY = 200;
8069 int32_t rawTouchMajor = 5;
8070 int32_t rawToolMajor = 8;
8071
8072 float x = toDisplayX(rawX);
8073 float y = toDisplayY(rawY);
8074 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8075 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8076 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8077
8078 processPosition(mapper, rawX, rawY);
8079 processTouchMajor(mapper, rawTouchMajor);
8080 processToolMajor(mapper, rawToolMajor);
8081 processMTSync(mapper);
8082 processSync(mapper);
8083
8084 NotifyMotionArgs args;
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8087 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8088}
8089
8090TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091 addConfigurationProperty("touch.deviceType", "touchScreen");
8092 prepareDisplay(DISPLAY_ORIENTATION_0);
8093 prepareAxes(POSITION | PRESSURE);
8094 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8095 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008096 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097
Michael Wrightaa449c92017-12-13 21:21:43 +00008098 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008099 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008100 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8101 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8102 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8103
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104 // These calculations are based on the input device calibration documentation.
8105 int32_t rawX = 100;
8106 int32_t rawY = 200;
8107 int32_t rawPressure = 60;
8108
8109 float x = toDisplayX(rawX);
8110 float y = toDisplayY(rawY);
8111 float pressure = float(rawPressure) * 0.01f;
8112
8113 processPosition(mapper, rawX, rawY);
8114 processPressure(mapper, rawPressure);
8115 processMTSync(mapper);
8116 processSync(mapper);
8117
8118 NotifyMotionArgs args;
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8121 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8122}
8123
8124TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 addConfigurationProperty("touch.deviceType", "touchScreen");
8126 prepareDisplay(DISPLAY_ORIENTATION_0);
8127 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008128 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129
8130 NotifyMotionArgs motionArgs;
8131 NotifyKeyArgs keyArgs;
8132
8133 processId(mapper, 1);
8134 processPosition(mapper, 100, 200);
8135 processSync(mapper);
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8137 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8138 ASSERT_EQ(0, motionArgs.buttonState);
8139
8140 // press BTN_LEFT, release BTN_LEFT
8141 processKey(mapper, BTN_LEFT, 1);
8142 processSync(mapper);
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8145 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8146
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8149 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8150
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151 processKey(mapper, BTN_LEFT, 0);
8152 processSync(mapper);
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008156
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160
8161 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8162 processKey(mapper, BTN_RIGHT, 1);
8163 processKey(mapper, BTN_MIDDLE, 1);
8164 processSync(mapper);
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8167 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8168 motionArgs.buttonState);
8169
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8171 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8172 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8173
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8176 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8177 motionArgs.buttonState);
8178
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 processKey(mapper, BTN_RIGHT, 0);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008184
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008187 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008188
8189 processKey(mapper, BTN_MIDDLE, 0);
8190 processSync(mapper);
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008192 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008194
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008197 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198
8199 // press BTN_BACK, release BTN_BACK
8200 processKey(mapper, BTN_BACK, 1);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8203 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8204 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008205
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008208 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8209
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8212 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213
8214 processKey(mapper, BTN_BACK, 0);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008218 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008219
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008222 ASSERT_EQ(0, motionArgs.buttonState);
8223
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8225 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8226 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8227
8228 // press BTN_SIDE, release BTN_SIDE
8229 processKey(mapper, BTN_SIDE, 1);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8232 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8233 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008234
Michael Wrightd02c5b62014-02-10 15:10:22 -08008235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008237 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8238
8239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8241 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242
8243 processKey(mapper, BTN_SIDE, 0);
8244 processSync(mapper);
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008248
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008251 ASSERT_EQ(0, motionArgs.buttonState);
8252
Michael Wrightd02c5b62014-02-10 15:10:22 -08008253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8254 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8255 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8256
8257 // press BTN_FORWARD, release BTN_FORWARD
8258 processKey(mapper, BTN_FORWARD, 1);
8259 processSync(mapper);
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8261 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8262 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008263
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008266 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8267
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8269 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8270 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008271
8272 processKey(mapper, BTN_FORWARD, 0);
8273 processSync(mapper);
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008276 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008277
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008280 ASSERT_EQ(0, motionArgs.buttonState);
8281
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8283 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8284 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8285
8286 // press BTN_EXTRA, release BTN_EXTRA
8287 processKey(mapper, BTN_EXTRA, 1);
8288 processSync(mapper);
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8290 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8291 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008292
Michael Wrightd02c5b62014-02-10 15:10:22 -08008293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008295 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8296
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8299 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300
8301 processKey(mapper, BTN_EXTRA, 0);
8302 processSync(mapper);
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008305 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008306
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008309 ASSERT_EQ(0, motionArgs.buttonState);
8310
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8312 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8313 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8314
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8316
Michael Wrightd02c5b62014-02-10 15:10:22 -08008317 // press BTN_STYLUS, release BTN_STYLUS
8318 processKey(mapper, BTN_STYLUS, 1);
8319 processSync(mapper);
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008322 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8323
8324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8326 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008327
8328 processKey(mapper, BTN_STYLUS, 0);
8329 processSync(mapper);
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008332 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008333
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008336 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337
8338 // press BTN_STYLUS2, release BTN_STYLUS2
8339 processKey(mapper, BTN_STYLUS2, 1);
8340 processSync(mapper);
8341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008343 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8344
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8347 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008348
8349 processKey(mapper, BTN_STYLUS2, 0);
8350 processSync(mapper);
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008354
8355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008357 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358
8359 // release touch
8360 processId(mapper, -1);
8361 processSync(mapper);
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8363 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8364 ASSERT_EQ(0, motionArgs.buttonState);
8365}
8366
8367TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008368 addConfigurationProperty("touch.deviceType", "touchScreen");
8369 prepareDisplay(DISPLAY_ORIENTATION_0);
8370 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008371 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372
8373 NotifyMotionArgs motionArgs;
8374
8375 // default tool type is finger
8376 processId(mapper, 1);
8377 processPosition(mapper, 100, 200);
8378 processSync(mapper);
8379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8380 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8382
8383 // eraser
8384 processKey(mapper, BTN_TOOL_RUBBER, 1);
8385 processSync(mapper);
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8389
8390 // stylus
8391 processKey(mapper, BTN_TOOL_RUBBER, 0);
8392 processKey(mapper, BTN_TOOL_PEN, 1);
8393 processSync(mapper);
8394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8397
8398 // brush
8399 processKey(mapper, BTN_TOOL_PEN, 0);
8400 processKey(mapper, BTN_TOOL_BRUSH, 1);
8401 processSync(mapper);
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8405
8406 // pencil
8407 processKey(mapper, BTN_TOOL_BRUSH, 0);
8408 processKey(mapper, BTN_TOOL_PENCIL, 1);
8409 processSync(mapper);
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8413
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008414 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008415 processKey(mapper, BTN_TOOL_PENCIL, 0);
8416 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8417 processSync(mapper);
8418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8421
8422 // mouse
8423 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8424 processKey(mapper, BTN_TOOL_MOUSE, 1);
8425 processSync(mapper);
8426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8429
8430 // lens
8431 processKey(mapper, BTN_TOOL_MOUSE, 0);
8432 processKey(mapper, BTN_TOOL_LENS, 1);
8433 processSync(mapper);
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8437
8438 // double-tap
8439 processKey(mapper, BTN_TOOL_LENS, 0);
8440 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8441 processSync(mapper);
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8445
8446 // triple-tap
8447 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8448 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8449 processSync(mapper);
8450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8453
8454 // quad-tap
8455 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8456 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8457 processSync(mapper);
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8461
8462 // finger
8463 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8464 processKey(mapper, BTN_TOOL_FINGER, 1);
8465 processSync(mapper);
8466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8469
8470 // stylus trumps finger
8471 processKey(mapper, BTN_TOOL_PEN, 1);
8472 processSync(mapper);
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8476
8477 // eraser trumps stylus
8478 processKey(mapper, BTN_TOOL_RUBBER, 1);
8479 processSync(mapper);
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8483
8484 // mouse trumps eraser
8485 processKey(mapper, BTN_TOOL_MOUSE, 1);
8486 processSync(mapper);
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8490
8491 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8492 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8493 processSync(mapper);
8494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8497
8498 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8499 processToolType(mapper, MT_TOOL_PEN);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8504
8505 // back to default tool type
8506 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8507 processKey(mapper, BTN_TOOL_MOUSE, 0);
8508 processKey(mapper, BTN_TOOL_RUBBER, 0);
8509 processKey(mapper, BTN_TOOL_PEN, 0);
8510 processKey(mapper, BTN_TOOL_FINGER, 0);
8511 processSync(mapper);
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8515}
8516
8517TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008518 addConfigurationProperty("touch.deviceType", "touchScreen");
8519 prepareDisplay(DISPLAY_ORIENTATION_0);
8520 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008521 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008522 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008523
8524 NotifyMotionArgs motionArgs;
8525
8526 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8527 processId(mapper, 1);
8528 processPosition(mapper, 100, 200);
8529 processSync(mapper);
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8533 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8534
8535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8536 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8538 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8539
8540 // move a little
8541 processPosition(mapper, 150, 250);
8542 processSync(mapper);
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8546 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8547
8548 // down when BTN_TOUCH is pressed, pressure defaults to 1
8549 processKey(mapper, BTN_TOUCH, 1);
8550 processSync(mapper);
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8554 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8555
8556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8557 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8559 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8560
8561 // up when BTN_TOUCH is released, hover restored
8562 processKey(mapper, BTN_TOUCH, 0);
8563 processSync(mapper);
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8565 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8567 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8568
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8572 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8573
8574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8577 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8578
8579 // exit hover when pointer goes away
8580 processId(mapper, -1);
8581 processSync(mapper);
8582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8585 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8586}
8587
8588TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008589 addConfigurationProperty("touch.deviceType", "touchScreen");
8590 prepareDisplay(DISPLAY_ORIENTATION_0);
8591 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008592 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008593
8594 NotifyMotionArgs motionArgs;
8595
8596 // initially hovering because pressure is 0
8597 processId(mapper, 1);
8598 processPosition(mapper, 100, 200);
8599 processPressure(mapper, 0);
8600 processSync(mapper);
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8604 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8605
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8607 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8609 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8610
8611 // move a little
8612 processPosition(mapper, 150, 250);
8613 processSync(mapper);
8614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8617 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8618
8619 // down when pressure becomes non-zero
8620 processPressure(mapper, RAW_PRESSURE_MAX);
8621 processSync(mapper);
8622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8625 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8626
8627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8628 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8630 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8631
8632 // up when pressure becomes 0, hover restored
8633 processPressure(mapper, 0);
8634 processSync(mapper);
8635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8636 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8638 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8639
8640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8641 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8643 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8644
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8648 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8649
8650 // exit hover when pointer goes away
8651 processId(mapper, -1);
8652 processSync(mapper);
8653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8654 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8656 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8657}
8658
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008659/**
8660 * Set the input device port <--> display port associations, and check that the
8661 * events are routed to the display that matches the display port.
8662 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8663 */
8664TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008665 const std::string usb2 = "USB2";
8666 const uint8_t hdmi1 = 0;
8667 const uint8_t hdmi2 = 1;
8668 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008669 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008670
8671 addConfigurationProperty("touch.deviceType", "touchScreen");
8672 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008673 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008674
8675 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8676 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8677
8678 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8679 // for this input device is specified, and the matching viewport is not present,
8680 // the input device should be disabled (at the mapper level).
8681
8682 // Add viewport for display 2 on hdmi2
8683 prepareSecondaryDisplay(type, hdmi2);
8684 // Send a touch event
8685 processPosition(mapper, 100, 100);
8686 processSync(mapper);
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8688
8689 // Add viewport for display 1 on hdmi1
8690 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8691 // Send a touch event again
8692 processPosition(mapper, 100, 100);
8693 processSync(mapper);
8694
8695 NotifyMotionArgs args;
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8697 ASSERT_EQ(DISPLAY_ID, args.displayId);
8698}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008699
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008700TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8701 addConfigurationProperty("touch.deviceType", "touchScreen");
8702 prepareAxes(POSITION);
8703 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8704
8705 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8706
8707 prepareDisplay(DISPLAY_ORIENTATION_0);
8708 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8709
8710 // Send a touch event
8711 processPosition(mapper, 100, 100);
8712 processSync(mapper);
8713
8714 NotifyMotionArgs args;
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8716 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8717}
8718
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008719TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008720 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008721 std::shared_ptr<FakePointerController> fakePointerController =
8722 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008723 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008724 fakePointerController->setPosition(100, 200);
8725 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008726 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008727
Garfield Tan888a6a42020-01-09 11:39:16 -08008728 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008729 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008730
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008731 prepareDisplay(DISPLAY_ORIENTATION_0);
8732 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008733 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008734
8735 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008736 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008737
8738 NotifyMotionArgs motionArgs;
8739 processPosition(mapper, 100, 100);
8740 processSync(mapper);
8741
8742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8744 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8745}
8746
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008747/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008748 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8749 */
8750TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8751 addConfigurationProperty("touch.deviceType", "touchScreen");
8752 prepareAxes(POSITION);
8753 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8754
8755 prepareDisplay(DISPLAY_ORIENTATION_0);
8756 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8757 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8758 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8759 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8760
8761 NotifyMotionArgs args;
8762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8763 ASSERT_EQ(26, args.readTime);
8764
8765 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8766 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8767 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8768
8769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8770 ASSERT_EQ(33, args.readTime);
8771}
8772
8773/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008774 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8775 * events should not be delivered to the listener.
8776 */
8777TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8778 addConfigurationProperty("touch.deviceType", "touchScreen");
8779 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8780 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8781 ViewportType::INTERNAL);
8782 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8783 prepareAxes(POSITION);
8784 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8785
8786 NotifyMotionArgs motionArgs;
8787 processPosition(mapper, 100, 100);
8788 processSync(mapper);
8789
8790 mFakeListener->assertNotifyMotionWasNotCalled();
8791}
8792
Garfield Tanc734e4f2021-01-15 20:01:39 -08008793TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8794 addConfigurationProperty("touch.deviceType", "touchScreen");
8795 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8796 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8797 ViewportType::INTERNAL);
8798 std::optional<DisplayViewport> optionalDisplayViewport =
8799 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8800 ASSERT_TRUE(optionalDisplayViewport.has_value());
8801 DisplayViewport displayViewport = *optionalDisplayViewport;
8802
8803 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8804 prepareAxes(POSITION);
8805 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8806
8807 // Finger down
8808 int32_t x = 100, y = 100;
8809 processPosition(mapper, x, y);
8810 processSync(mapper);
8811
8812 NotifyMotionArgs motionArgs;
8813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8814 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8815
8816 // Deactivate display viewport
8817 displayViewport.isActive = false;
8818 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8819 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8820
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008821 // The ongoing touch should be canceled immediately
8822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8823 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8824
8825 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008826 x += 10, y += 10;
8827 processPosition(mapper, x, y);
8828 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008830
8831 // Reactivate display viewport
8832 displayViewport.isActive = true;
8833 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8834 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8835
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008836 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008837 x += 10, y += 10;
8838 processPosition(mapper, x, y);
8839 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8841 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008842}
8843
Arthur Hung7c645402019-01-25 17:45:42 +08008844TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8845 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008846 prepareAxes(POSITION | ID | SLOT);
8847 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008848 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008849
8850 // Create the second touch screen device, and enable multi fingers.
8851 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008852 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008853 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008854 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008855 std::shared_ptr<InputDevice> device2 =
8856 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008857 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008858
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008859 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8860 0 /*flat*/, 0 /*fuzz*/);
8861 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8862 0 /*flat*/, 0 /*fuzz*/);
8863 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8864 0 /*flat*/, 0 /*fuzz*/);
8865 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8866 0 /*flat*/, 0 /*fuzz*/);
8867 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8868 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8869 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008870
8871 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008872 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008873 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8874 device2->reset(ARBITRARY_TIME);
8875
8876 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008877 std::shared_ptr<FakePointerController> fakePointerController =
8878 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008879 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008880
8881 // Setup policy for associated displays and show touches.
8882 const uint8_t hdmi1 = 0;
8883 const uint8_t hdmi2 = 1;
8884 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8885 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8886 mFakePolicy->setShowTouches(true);
8887
8888 // Create displays.
8889 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008890 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008891
8892 // Default device will reconfigure above, need additional reconfiguration for another device.
8893 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008894 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8895 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008896
8897 // Two fingers down at default display.
8898 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8899 processPosition(mapper, x1, y1);
8900 processId(mapper, 1);
8901 processSlot(mapper, 1);
8902 processPosition(mapper, x2, y2);
8903 processId(mapper, 2);
8904 processSync(mapper);
8905
8906 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8907 fakePointerController->getSpots().find(DISPLAY_ID);
8908 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8909 ASSERT_EQ(size_t(2), iter->second.size());
8910
8911 // Two fingers down at second display.
8912 processPosition(mapper2, x1, y1);
8913 processId(mapper2, 1);
8914 processSlot(mapper2, 1);
8915 processPosition(mapper2, x2, y2);
8916 processId(mapper2, 2);
8917 processSync(mapper2);
8918
8919 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8920 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8921 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008922
8923 // Disable the show touches configuration and ensure the spots are cleared.
8924 mFakePolicy->setShowTouches(false);
8925 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8926 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8927
8928 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008929}
8930
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008931TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008932 prepareAxes(POSITION);
8933 addConfigurationProperty("touch.deviceType", "touchScreen");
8934 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008935 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008936
8937 NotifyMotionArgs motionArgs;
8938 // Unrotated video frame
8939 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8940 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008941 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008942 processPosition(mapper, 100, 200);
8943 processSync(mapper);
8944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8945 ASSERT_EQ(frames, motionArgs.videoFrames);
8946
8947 // Subsequent touch events should not have any videoframes
8948 // This is implemented separately in FakeEventHub,
8949 // but that should match the behaviour of TouchVideoDevice.
8950 processPosition(mapper, 200, 200);
8951 processSync(mapper);
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8953 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8954}
8955
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008956TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008957 prepareAxes(POSITION);
8958 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008960 // Unrotated video frame
8961 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8962 NotifyMotionArgs motionArgs;
8963
8964 // Test all 4 orientations
8965 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008966 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8967 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8968 clearViewports();
8969 prepareDisplay(orientation);
8970 std::vector<TouchVideoFrame> frames{frame};
8971 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8972 processPosition(mapper, 100, 200);
8973 processSync(mapper);
8974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8975 ASSERT_EQ(frames, motionArgs.videoFrames);
8976 }
8977}
8978
8979TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8980 prepareAxes(POSITION);
8981 addConfigurationProperty("touch.deviceType", "touchScreen");
8982 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8983 // orientation-aware are affected by display rotation.
8984 addConfigurationProperty("touch.orientationAware", "0");
8985 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8986 // Unrotated video frame
8987 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8988 NotifyMotionArgs motionArgs;
8989
8990 // Test all 4 orientations
8991 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008992 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8993 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8994 clearViewports();
8995 prepareDisplay(orientation);
8996 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008997 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008998 processPosition(mapper, 100, 200);
8999 processSync(mapper);
9000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009001 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9002 // compared to the display. This is so that when the window transform (which contains the
9003 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9004 // window's coordinate space.
9005 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009006 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009007
9008 // Release finger.
9009 processSync(mapper);
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009011 }
9012}
9013
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009014TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009015 prepareAxes(POSITION);
9016 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009017 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009018 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9019 // so mix these.
9020 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9021 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9022 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9023 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9024 NotifyMotionArgs motionArgs;
9025
9026 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009027 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009028 processPosition(mapper, 100, 200);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009031 ASSERT_EQ(frames, motionArgs.videoFrames);
9032}
9033
9034TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9035 prepareAxes(POSITION);
9036 addConfigurationProperty("touch.deviceType", "touchScreen");
9037 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9038 // orientation-aware are affected by display rotation.
9039 addConfigurationProperty("touch.orientationAware", "0");
9040 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9041 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9042 // so mix these.
9043 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9044 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9045 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9046 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9047 NotifyMotionArgs motionArgs;
9048
9049 prepareDisplay(DISPLAY_ORIENTATION_90);
9050 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9051 processPosition(mapper, 100, 200);
9052 processSync(mapper);
9053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9054 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9055 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9056 // compared to the display. This is so that when the window transform (which contains the
9057 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9058 // window's coordinate space.
9059 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9060 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009061 ASSERT_EQ(frames, motionArgs.videoFrames);
9062}
9063
Arthur Hung9da14732019-09-02 16:16:58 +08009064/**
9065 * If we had defined port associations, but the viewport is not ready, the touch device would be
9066 * expected to be disabled, and it should be enabled after the viewport has found.
9067 */
9068TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009069 constexpr uint8_t hdmi2 = 1;
9070 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009071 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009072
9073 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9074
9075 addConfigurationProperty("touch.deviceType", "touchScreen");
9076 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009077 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009078
9079 ASSERT_EQ(mDevice->isEnabled(), false);
9080
9081 // Add display on hdmi2, the device should be enabled and can receive touch event.
9082 prepareSecondaryDisplay(type, hdmi2);
9083 ASSERT_EQ(mDevice->isEnabled(), true);
9084
9085 // Send a touch event.
9086 processPosition(mapper, 100, 100);
9087 processSync(mapper);
9088
9089 NotifyMotionArgs args;
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9091 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9092}
9093
Arthur Hung421eb1c2020-01-16 00:09:42 +08009094TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009095 addConfigurationProperty("touch.deviceType", "touchScreen");
9096 prepareDisplay(DISPLAY_ORIENTATION_0);
9097 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009098 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009099
9100 NotifyMotionArgs motionArgs;
9101
9102 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9103 // finger down
9104 processId(mapper, 1);
9105 processPosition(mapper, x1, y1);
9106 processSync(mapper);
9107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9108 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9110
9111 // finger move
9112 processId(mapper, 1);
9113 processPosition(mapper, x2, y2);
9114 processSync(mapper);
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9118
9119 // finger up.
9120 processId(mapper, -1);
9121 processSync(mapper);
9122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9123 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9125
9126 // new finger down
9127 processId(mapper, 1);
9128 processPosition(mapper, x3, y3);
9129 processSync(mapper);
9130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9131 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9133}
9134
9135/**
arthurhungcc7f9802020-04-30 17:55:40 +08009136 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9137 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009138 */
arthurhungcc7f9802020-04-30 17:55:40 +08009139TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009140 addConfigurationProperty("touch.deviceType", "touchScreen");
9141 prepareDisplay(DISPLAY_ORIENTATION_0);
9142 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009143 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009144
9145 NotifyMotionArgs motionArgs;
9146
9147 // default tool type is finger
9148 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009149 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009150 processPosition(mapper, x1, y1);
9151 processSync(mapper);
9152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9153 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9155
9156 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9157 processToolType(mapper, MT_TOOL_PALM);
9158 processSync(mapper);
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9160 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9161
9162 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009163 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009164 processPosition(mapper, x2, y2);
9165 processSync(mapper);
9166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9167
9168 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009169 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009170 processSync(mapper);
9171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9172
9173 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009174 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009175 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009176 processPosition(mapper, x3, y3);
9177 processSync(mapper);
9178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9179 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9181}
9182
arthurhungbf89a482020-04-17 17:37:55 +08009183/**
arthurhungcc7f9802020-04-30 17:55:40 +08009184 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9185 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009186 */
arthurhungcc7f9802020-04-30 17:55:40 +08009187TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009188 addConfigurationProperty("touch.deviceType", "touchScreen");
9189 prepareDisplay(DISPLAY_ORIENTATION_0);
9190 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9191 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9192
9193 NotifyMotionArgs motionArgs;
9194
9195 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009196 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9197 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009198 processPosition(mapper, x1, y1);
9199 processSync(mapper);
9200 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);
9203
9204 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009205 processSlot(mapper, SECOND_SLOT);
9206 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009207 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009208 processSync(mapper);
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009210 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9212
9213 // If the tool type of the first finger changes to MT_TOOL_PALM,
9214 // we expect to receive ACTION_POINTER_UP with cancel flag.
9215 processSlot(mapper, FIRST_SLOT);
9216 processId(mapper, FIRST_TRACKING_ID);
9217 processToolType(mapper, MT_TOOL_PALM);
9218 processSync(mapper);
9219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009220 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009221 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9222
9223 // The following MOVE events of second finger should be processed.
9224 processSlot(mapper, SECOND_SLOT);
9225 processId(mapper, SECOND_TRACKING_ID);
9226 processPosition(mapper, x2 + 1, y2 + 1);
9227 processSync(mapper);
9228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9230 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9231
9232 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9233 // it. Second finger receive move.
9234 processSlot(mapper, FIRST_SLOT);
9235 processId(mapper, INVALID_TRACKING_ID);
9236 processSync(mapper);
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9239 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9240
9241 // Second finger keeps moving.
9242 processSlot(mapper, SECOND_SLOT);
9243 processId(mapper, SECOND_TRACKING_ID);
9244 processPosition(mapper, x2 + 2, y2 + 2);
9245 processSync(mapper);
9246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9248 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9249
9250 // Second finger up.
9251 processId(mapper, INVALID_TRACKING_ID);
9252 processSync(mapper);
9253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9254 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9255 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9256}
9257
9258/**
9259 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9260 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9261 */
9262TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9263 addConfigurationProperty("touch.deviceType", "touchScreen");
9264 prepareDisplay(DISPLAY_ORIENTATION_0);
9265 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9266 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9267
9268 NotifyMotionArgs motionArgs;
9269
9270 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9271 // First finger down.
9272 processId(mapper, FIRST_TRACKING_ID);
9273 processPosition(mapper, x1, y1);
9274 processSync(mapper);
9275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9276 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9278
9279 // Second finger down.
9280 processSlot(mapper, SECOND_SLOT);
9281 processId(mapper, SECOND_TRACKING_ID);
9282 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009283 processSync(mapper);
9284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009285 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9287
arthurhungcc7f9802020-04-30 17:55:40 +08009288 // If the tool type of the first finger changes to MT_TOOL_PALM,
9289 // we expect to receive ACTION_POINTER_UP with cancel flag.
9290 processSlot(mapper, FIRST_SLOT);
9291 processId(mapper, FIRST_TRACKING_ID);
9292 processToolType(mapper, MT_TOOL_PALM);
9293 processSync(mapper);
9294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009295 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009296 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9297
9298 // Second finger keeps moving.
9299 processSlot(mapper, SECOND_SLOT);
9300 processId(mapper, SECOND_TRACKING_ID);
9301 processPosition(mapper, x2 + 1, y2 + 1);
9302 processSync(mapper);
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9305
9306 // second finger becomes palm, receive cancel due to only 1 finger is active.
9307 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009308 processToolType(mapper, MT_TOOL_PALM);
9309 processSync(mapper);
9310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9311 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9312
arthurhungcc7f9802020-04-30 17:55:40 +08009313 // third finger down.
9314 processSlot(mapper, THIRD_SLOT);
9315 processId(mapper, THIRD_TRACKING_ID);
9316 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009317 processPosition(mapper, x3, y3);
9318 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009322 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9323
9324 // third finger move
9325 processId(mapper, THIRD_TRACKING_ID);
9326 processPosition(mapper, x3 + 1, y3 + 1);
9327 processSync(mapper);
9328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9330
9331 // first finger up, third finger receive move.
9332 processSlot(mapper, FIRST_SLOT);
9333 processId(mapper, INVALID_TRACKING_ID);
9334 processSync(mapper);
9335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9337 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9338
9339 // second finger up, third finger receive move.
9340 processSlot(mapper, SECOND_SLOT);
9341 processId(mapper, INVALID_TRACKING_ID);
9342 processSync(mapper);
9343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9345 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9346
9347 // third finger up.
9348 processSlot(mapper, THIRD_SLOT);
9349 processId(mapper, INVALID_TRACKING_ID);
9350 processSync(mapper);
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9352 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9353 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9354}
9355
9356/**
9357 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9358 * and the active finger could still be allowed to receive the events
9359 */
9360TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9361 addConfigurationProperty("touch.deviceType", "touchScreen");
9362 prepareDisplay(DISPLAY_ORIENTATION_0);
9363 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9364 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9365
9366 NotifyMotionArgs motionArgs;
9367
9368 // default tool type is finger
9369 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9370 processId(mapper, FIRST_TRACKING_ID);
9371 processPosition(mapper, x1, y1);
9372 processSync(mapper);
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9374 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9376
9377 // Second finger down.
9378 processSlot(mapper, SECOND_SLOT);
9379 processId(mapper, SECOND_TRACKING_ID);
9380 processPosition(mapper, x2, y2);
9381 processSync(mapper);
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009383 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9385
9386 // If the tool type of the second finger changes to MT_TOOL_PALM,
9387 // we expect to receive ACTION_POINTER_UP with cancel flag.
9388 processId(mapper, SECOND_TRACKING_ID);
9389 processToolType(mapper, MT_TOOL_PALM);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009392 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009393 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9394
9395 // The following MOVE event should be processed.
9396 processSlot(mapper, FIRST_SLOT);
9397 processId(mapper, FIRST_TRACKING_ID);
9398 processPosition(mapper, x1 + 1, y1 + 1);
9399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9402 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9403
9404 // second finger up.
9405 processSlot(mapper, SECOND_SLOT);
9406 processId(mapper, INVALID_TRACKING_ID);
9407 processSync(mapper);
9408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9410
9411 // first finger keep moving
9412 processSlot(mapper, FIRST_SLOT);
9413 processId(mapper, FIRST_TRACKING_ID);
9414 processPosition(mapper, x1 + 2, y1 + 2);
9415 processSync(mapper);
9416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9418
9419 // first finger up.
9420 processId(mapper, INVALID_TRACKING_ID);
9421 processSync(mapper);
9422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9423 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9424 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009425}
9426
Arthur Hung9ad18942021-06-19 02:04:46 +00009427/**
9428 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9429 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9430 * cause slot be valid again.
9431 */
9432TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9433 addConfigurationProperty("touch.deviceType", "touchScreen");
9434 prepareDisplay(DISPLAY_ORIENTATION_0);
9435 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9436 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9437
9438 NotifyMotionArgs motionArgs;
9439
9440 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9441 // First finger down.
9442 processId(mapper, FIRST_TRACKING_ID);
9443 processPosition(mapper, x1, y1);
9444 processPressure(mapper, RAW_PRESSURE_MAX);
9445 processSync(mapper);
9446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9447 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9448 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9449
9450 // First finger move.
9451 processId(mapper, FIRST_TRACKING_ID);
9452 processPosition(mapper, x1 + 1, y1 + 1);
9453 processPressure(mapper, RAW_PRESSURE_MAX);
9454 processSync(mapper);
9455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9457 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9458
9459 // Second finger down.
9460 processSlot(mapper, SECOND_SLOT);
9461 processId(mapper, SECOND_TRACKING_ID);
9462 processPosition(mapper, x2, y2);
9463 processPressure(mapper, RAW_PRESSURE_MAX);
9464 processSync(mapper);
9465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009466 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009467 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9468
9469 // second finger up with some unexpected data.
9470 processSlot(mapper, SECOND_SLOT);
9471 processId(mapper, INVALID_TRACKING_ID);
9472 processPosition(mapper, x2, y2);
9473 processSync(mapper);
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009475 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009476 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9477
9478 // first finger up with some unexpected data.
9479 processSlot(mapper, FIRST_SLOT);
9480 processId(mapper, INVALID_TRACKING_ID);
9481 processPosition(mapper, x2, y2);
9482 processPressure(mapper, RAW_PRESSURE_MAX);
9483 processSync(mapper);
9484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9486 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9487}
9488
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00009489TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9490 addConfigurationProperty("touch.deviceType", "touchScreen");
9491 prepareDisplay(DISPLAY_ORIENTATION_0);
9492 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9493 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9494
9495 NotifyMotionArgs motionArgs;
9496
9497 // First finger down.
9498 processId(mapper, FIRST_TRACKING_ID);
9499 processPosition(mapper, 100, 200);
9500 processPressure(mapper, RAW_PRESSURE_MAX);
9501 processSync(mapper);
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9503 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9504
9505 // Second finger down.
9506 processSlot(mapper, SECOND_SLOT);
9507 processId(mapper, SECOND_TRACKING_ID);
9508 processPosition(mapper, 300, 400);
9509 processPressure(mapper, RAW_PRESSURE_MAX);
9510 processSync(mapper);
9511 ASSERT_NO_FATAL_FAILURE(
9512 mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9513 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
9514
9515 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
9516 // preserved. Resetting should not generate any events.
9517 mapper.reset(ARBITRARY_TIME);
9518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9519
9520 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9521 // the existing touch state to generate a down event.
9522 processPosition(mapper, 301, 302);
9523 processSync(mapper);
9524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9525 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9527 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
9528
9529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9530}
9531
9532TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9533 addConfigurationProperty("touch.deviceType", "touchScreen");
9534 prepareDisplay(DISPLAY_ORIENTATION_0);
9535 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9536 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9537
9538 NotifyMotionArgs motionArgs;
9539
9540 // First finger touches down and releases.
9541 processId(mapper, FIRST_TRACKING_ID);
9542 processPosition(mapper, 100, 200);
9543 processPressure(mapper, RAW_PRESSURE_MAX);
9544 processSync(mapper);
9545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9547 processId(mapper, INVALID_TRACKING_ID);
9548 processSync(mapper);
9549 ASSERT_NO_FATAL_FAILURE(
9550 mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9551 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9552
9553 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9554 // raw state where no pointers are down.
9555 mapper.reset(ARBITRARY_TIME);
9556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9557
9558 // Send an empty sync frame. Since there are no pointers, no events are generated.
9559 processSync(mapper);
9560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9561}
9562
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009563// --- MultiTouchInputMapperTest_ExternalDevice ---
9564
9565class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9566protected:
Chris Yea52ade12020-08-27 16:49:20 -07009567 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009568};
9569
9570/**
9571 * Expect fallback to internal viewport if device is external and external viewport is not present.
9572 */
9573TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9574 prepareAxes(POSITION);
9575 addConfigurationProperty("touch.deviceType", "touchScreen");
9576 prepareDisplay(DISPLAY_ORIENTATION_0);
9577 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9578
9579 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9580
9581 NotifyMotionArgs motionArgs;
9582
9583 // Expect the event to be sent to the internal viewport,
9584 // because an external viewport is not present.
9585 processPosition(mapper, 100, 100);
9586 processSync(mapper);
9587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9588 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9589
9590 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009591 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009592 processPosition(mapper, 100, 100);
9593 processSync(mapper);
9594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9595 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9596}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009597
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009598TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9599 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9600 std::shared_ptr<FakePointerController> fakePointerController =
9601 std::make_shared<FakePointerController>();
9602 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9603 fakePointerController->setPosition(0, 0);
9604 fakePointerController->setButtonState(0);
9605
9606 // prepare device and capture
9607 prepareDisplay(DISPLAY_ORIENTATION_0);
9608 prepareAxes(POSITION | ID | SLOT);
9609 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9610 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9611 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009612 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009613 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9614
9615 // captured touchpad should be a touchpad source
9616 NotifyDeviceResetArgs resetArgs;
9617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9618 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9619
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009620 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009621
9622 const InputDeviceInfo::MotionRange* relRangeX =
9623 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9624 ASSERT_NE(relRangeX, nullptr);
9625 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9626 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9627 const InputDeviceInfo::MotionRange* relRangeY =
9628 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9629 ASSERT_NE(relRangeY, nullptr);
9630 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9631 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9632
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009633 // run captured pointer tests - note that this is unscaled, so input listener events should be
9634 // identical to what the hardware sends (accounting for any
9635 // calibration).
9636 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009637 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009638 processId(mapper, 1);
9639 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9640 processKey(mapper, BTN_TOUCH, 1);
9641 processSync(mapper);
9642
9643 // expect coord[0] to contain initial location of touch 0
9644 NotifyMotionArgs args;
9645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9646 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9647 ASSERT_EQ(1U, args.pointerCount);
9648 ASSERT_EQ(0, args.pointerProperties[0].id);
9649 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9650 ASSERT_NO_FATAL_FAILURE(
9651 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9652
9653 // FINGER 1 DOWN
9654 processSlot(mapper, 1);
9655 processId(mapper, 2);
9656 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9657 processSync(mapper);
9658
9659 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009661 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009662 ASSERT_EQ(2U, args.pointerCount);
9663 ASSERT_EQ(0, args.pointerProperties[0].id);
9664 ASSERT_EQ(1, args.pointerProperties[1].id);
9665 ASSERT_NO_FATAL_FAILURE(
9666 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9667 ASSERT_NO_FATAL_FAILURE(
9668 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9669
9670 // FINGER 1 MOVE
9671 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9672 processSync(mapper);
9673
9674 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9675 // from move
9676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9678 ASSERT_NO_FATAL_FAILURE(
9679 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9680 ASSERT_NO_FATAL_FAILURE(
9681 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9682
9683 // FINGER 0 MOVE
9684 processSlot(mapper, 0);
9685 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9686 processSync(mapper);
9687
9688 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9691 ASSERT_NO_FATAL_FAILURE(
9692 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9693 ASSERT_NO_FATAL_FAILURE(
9694 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9695
9696 // BUTTON DOWN
9697 processKey(mapper, BTN_LEFT, 1);
9698 processSync(mapper);
9699
9700 // touchinputmapper design sends a move before button press
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9704 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9705
9706 // BUTTON UP
9707 processKey(mapper, BTN_LEFT, 0);
9708 processSync(mapper);
9709
9710 // touchinputmapper design sends a move after button release
9711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9712 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9715
9716 // FINGER 0 UP
9717 processId(mapper, -1);
9718 processSync(mapper);
9719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9720 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9721
9722 // FINGER 1 MOVE
9723 processSlot(mapper, 1);
9724 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9725 processSync(mapper);
9726
9727 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9730 ASSERT_EQ(1U, args.pointerCount);
9731 ASSERT_EQ(1, args.pointerProperties[0].id);
9732 ASSERT_NO_FATAL_FAILURE(
9733 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9734
9735 // FINGER 1 UP
9736 processId(mapper, -1);
9737 processKey(mapper, BTN_TOUCH, 0);
9738 processSync(mapper);
9739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9740 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9741
9742 // non captured touchpad should be a mouse source
9743 mFakePolicy->setPointerCapture(false);
9744 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9746 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9747}
9748
9749TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9750 std::shared_ptr<FakePointerController> fakePointerController =
9751 std::make_shared<FakePointerController>();
9752 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9753 fakePointerController->setPosition(0, 0);
9754 fakePointerController->setButtonState(0);
9755
9756 // prepare device and capture
9757 prepareDisplay(DISPLAY_ORIENTATION_0);
9758 prepareAxes(POSITION | ID | SLOT);
9759 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9760 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009761 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009762 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9763 // run uncaptured pointer tests - pushes out generic events
9764 // FINGER 0 DOWN
9765 processId(mapper, 3);
9766 processPosition(mapper, 100, 100);
9767 processKey(mapper, BTN_TOUCH, 1);
9768 processSync(mapper);
9769
9770 // start at (100,100), cursor should be at (0,0) * scale
9771 NotifyMotionArgs args;
9772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9773 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9774 ASSERT_NO_FATAL_FAILURE(
9775 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9776
9777 // FINGER 0 MOVE
9778 processPosition(mapper, 200, 200);
9779 processSync(mapper);
9780
9781 // compute scaling to help with touch position checking
9782 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9783 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9784 float scale =
9785 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9786
9787 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9789 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9791 0, 0, 0, 0, 0, 0, 0));
9792}
9793
9794TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9795 std::shared_ptr<FakePointerController> fakePointerController =
9796 std::make_shared<FakePointerController>();
9797
9798 prepareDisplay(DISPLAY_ORIENTATION_0);
9799 prepareAxes(POSITION | ID | SLOT);
9800 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009801 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009802 mFakePolicy->setPointerCapture(false);
9803 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9804
9805 // uncaptured touchpad should be a pointer device
9806 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9807
9808 // captured touchpad should be a touchpad device
9809 mFakePolicy->setPointerCapture(true);
9810 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9811 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9812}
9813
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009814// --- JoystickInputMapperTest ---
9815
9816class JoystickInputMapperTest : public InputMapperTest {
9817protected:
9818 static const int32_t RAW_X_MIN;
9819 static const int32_t RAW_X_MAX;
9820 static const int32_t RAW_Y_MIN;
9821 static const int32_t RAW_Y_MAX;
9822
9823 void SetUp() override {
9824 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9825 }
9826 void prepareAxes() {
9827 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9828 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9829 }
9830
9831 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9833 }
9834
9835 void processSync(JoystickInputMapper& mapper) {
9836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9837 }
9838
9839 void prepareVirtualDisplay(int32_t orientation) {
9840 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9841 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9842 NO_PORT, ViewportType::VIRTUAL);
9843 }
9844};
9845
9846const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9847const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9848const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9849const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9850
9851TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9852 prepareAxes();
9853 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9854
9855 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9856
9857 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9858
9859 // Send an axis event
9860 processAxis(mapper, ABS_X, 100);
9861 processSync(mapper);
9862
9863 NotifyMotionArgs args;
9864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9865 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9866
9867 // Send another axis event
9868 processAxis(mapper, ABS_Y, 100);
9869 processSync(mapper);
9870
9871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9872 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9873}
9874
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009875// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009876
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009877class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009878protected:
9879 static const char* DEVICE_NAME;
9880 static const char* DEVICE_LOCATION;
9881 static const int32_t DEVICE_ID;
9882 static const int32_t DEVICE_GENERATION;
9883 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009884 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009885 static const int32_t EVENTHUB_ID;
9886
9887 std::shared_ptr<FakeEventHub> mFakeEventHub;
9888 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009889 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009890 std::unique_ptr<InstrumentedInputReader> mReader;
9891 std::shared_ptr<InputDevice> mDevice;
9892
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009893 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009894 mFakeEventHub = std::make_unique<FakeEventHub>();
9895 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009896 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009897 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009898 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009899 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9900 }
9901
9902 void SetUp() override { SetUp(DEVICE_CLASSES); }
9903
9904 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009905 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009906 mFakePolicy.clear();
9907 }
9908
9909 void configureDevice(uint32_t changes) {
9910 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9911 mReader->requestRefreshConfiguration(changes);
9912 mReader->loopOnce();
9913 }
9914 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9915 }
9916
9917 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9918 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009919 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009920 InputDeviceIdentifier identifier;
9921 identifier.name = name;
9922 identifier.location = location;
9923 std::shared_ptr<InputDevice> device =
9924 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9925 identifier);
9926 mReader->pushNextDevice(device);
9927 mFakeEventHub->addDevice(eventHubId, name, classes);
9928 mReader->loopOnce();
9929 return device;
9930 }
9931
9932 template <class T, typename... Args>
9933 T& addControllerAndConfigure(Args... args) {
9934 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9935
9936 return controller;
9937 }
9938};
9939
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009940const char* PeripheralControllerTest::DEVICE_NAME = "device";
9941const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9942const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9943const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9944const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009945const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9946 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009947const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009948
9949// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009950class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009951protected:
9952 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009953 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009954 }
9955};
9956
9957TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009958 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009959
9960 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9961 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9962}
9963
9964TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009965 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009966
9967 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9968 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9969}
9970
9971// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009972class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009973protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009974 void SetUp() override {
9975 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9976 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009977};
9978
Chris Ye85758332021-05-16 23:05:17 -07009979TEST_F(LightControllerTest, MonoLight) {
9980 RawLightInfo infoMono = {.id = 1,
9981 .name = "Mono",
9982 .maxBrightness = 255,
9983 .flags = InputLightClass::BRIGHTNESS,
9984 .path = ""};
9985 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009986
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009987 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009988 InputDeviceInfo info;
9989 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009990 std::vector<InputDeviceLightInfo> lights = info.getLights();
9991 ASSERT_EQ(1U, lights.size());
9992 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009993
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009994 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9995 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009996}
9997
9998TEST_F(LightControllerTest, RGBLight) {
9999 RawLightInfo infoRed = {.id = 1,
10000 .name = "red",
10001 .maxBrightness = 255,
10002 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10003 .path = ""};
10004 RawLightInfo infoGreen = {.id = 2,
10005 .name = "green",
10006 .maxBrightness = 255,
10007 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10008 .path = ""};
10009 RawLightInfo infoBlue = {.id = 3,
10010 .name = "blue",
10011 .maxBrightness = 255,
10012 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10013 .path = ""};
10014 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10015 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10016 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10017
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010018 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010019 InputDeviceInfo info;
10020 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010021 std::vector<InputDeviceLightInfo> lights = info.getLights();
10022 ASSERT_EQ(1U, lights.size());
10023 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010024
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010025 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10026 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010027}
10028
10029TEST_F(LightControllerTest, MultiColorRGBLight) {
10030 RawLightInfo infoColor = {.id = 1,
10031 .name = "red",
10032 .maxBrightness = 255,
10033 .flags = InputLightClass::BRIGHTNESS |
10034 InputLightClass::MULTI_INTENSITY |
10035 InputLightClass::MULTI_INDEX,
10036 .path = ""};
10037
10038 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10039
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010040 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010041 InputDeviceInfo info;
10042 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010043 std::vector<InputDeviceLightInfo> lights = info.getLights();
10044 ASSERT_EQ(1U, lights.size());
10045 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010046
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010047 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10048 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010049}
10050
10051TEST_F(LightControllerTest, PlayerIdLight) {
10052 RawLightInfo info1 = {.id = 1,
10053 .name = "player1",
10054 .maxBrightness = 255,
10055 .flags = InputLightClass::BRIGHTNESS,
10056 .path = ""};
10057 RawLightInfo info2 = {.id = 2,
10058 .name = "player2",
10059 .maxBrightness = 255,
10060 .flags = InputLightClass::BRIGHTNESS,
10061 .path = ""};
10062 RawLightInfo info3 = {.id = 3,
10063 .name = "player3",
10064 .maxBrightness = 255,
10065 .flags = InputLightClass::BRIGHTNESS,
10066 .path = ""};
10067 RawLightInfo info4 = {.id = 4,
10068 .name = "player4",
10069 .maxBrightness = 255,
10070 .flags = InputLightClass::BRIGHTNESS,
10071 .path = ""};
10072 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10073 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10074 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10075 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10076
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010077 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010078 InputDeviceInfo info;
10079 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010080 std::vector<InputDeviceLightInfo> lights = info.getLights();
10081 ASSERT_EQ(1U, lights.size());
10082 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010083
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010084 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10085 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10086 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010087}
10088
Michael Wrightd02c5b62014-02-10 15:10:22 -080010089} // namespace android