blob: 8a97901926a28c669d631b57eaa6816c0bdeb447 [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
Chris Yea52ade12020-08-27 16:49:20 -0700249 void clearSpots() override {}
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
1017 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
1018
1019 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
1020 return std::nullopt;
1021 }
Kim Low03ea0352020-11-06 12:45:07 -08001022
Chris Ye3fdbfef2021-01-06 18:45:18 -08001023 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1024 std::vector<int32_t> ids;
1025 for (const auto& [rawId, info] : mRawLightInfos) {
1026 ids.push_back(rawId);
1027 }
1028 return ids;
1029 }
1030
1031 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1032 auto it = mRawLightInfos.find(lightId);
1033 if (it == mRawLightInfos.end()) {
1034 return std::nullopt;
1035 }
1036 return it->second;
1037 }
1038
1039 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1040 mLightBrightness.emplace(lightId, brightness);
1041 }
1042
1043 void setLightIntensities(int32_t deviceId, int32_t lightId,
1044 std::unordered_map<LightColor, int32_t> intensities) override {
1045 mLightIntensities.emplace(lightId, intensities);
1046 };
1047
1048 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1049 auto lightIt = mLightBrightness.find(lightId);
1050 if (lightIt == mLightBrightness.end()) {
1051 return std::nullopt;
1052 }
1053 return lightIt->second;
1054 }
1055
1056 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1057 int32_t deviceId, int32_t lightId) override {
1058 auto lightIt = mLightIntensities.find(lightId);
1059 if (lightIt == mLightIntensities.end()) {
1060 return std::nullopt;
1061 }
1062 return lightIt->second;
1063 };
1064
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001065 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001066 return false;
1067 }
1068
Chris Yea52ade12020-08-27 16:49:20 -07001069 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070
Chris Yea52ade12020-08-27 16:49:20 -07001071 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072
Chris Yea52ade12020-08-27 16:49:20 -07001073 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Chris Yea52ade12020-08-27 16:49:20 -07001075 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076};
1077
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078// --- FakeInputMapper ---
1079
1080class FakeInputMapper : public InputMapper {
1081 uint32_t mSources;
1082 int32_t mKeyboardType;
1083 int32_t mMetaState;
1084 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1085 KeyedVector<int32_t, int32_t> mScanCodeStates;
1086 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001087 // fake mapping which would normally come from keyCharacterMap
1088 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001089 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001091 std::mutex mLock;
1092 std::condition_variable mStateChangedCondition;
1093 bool mConfigureWasCalled GUARDED_BY(mLock);
1094 bool mResetWasCalled GUARDED_BY(mLock);
1095 bool mProcessWasCalled GUARDED_BY(mLock);
1096 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097
Arthur Hungc23540e2018-11-29 20:42:11 +08001098 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001100 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1101 : InputMapper(deviceContext),
1102 mSources(sources),
1103 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001105 mConfigureWasCalled(false),
1106 mResetWasCalled(false),
1107 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108
Chris Yea52ade12020-08-27 16:49:20 -07001109 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110
1111 void setKeyboardType(int32_t keyboardType) {
1112 mKeyboardType = keyboardType;
1113 }
1114
1115 void setMetaState(int32_t metaState) {
1116 mMetaState = metaState;
1117 }
1118
1119 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001120 std::unique_lock<std::mutex> lock(mLock);
1121 base::ScopedLockAssertion assumeLocked(mLock);
1122 const bool configureCalled =
1123 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1124 return mConfigureWasCalled;
1125 });
1126 if (!configureCalled) {
1127 FAIL() << "Expected configure() to have been called.";
1128 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 mConfigureWasCalled = false;
1130 }
1131
1132 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001133 std::unique_lock<std::mutex> lock(mLock);
1134 base::ScopedLockAssertion assumeLocked(mLock);
1135 const bool resetCalled =
1136 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1137 return mResetWasCalled;
1138 });
1139 if (!resetCalled) {
1140 FAIL() << "Expected reset() to have been called.";
1141 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142 mResetWasCalled = false;
1143 }
1144
Yi Kong9b14ac62018-07-17 13:48:38 -07001145 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001146 std::unique_lock<std::mutex> lock(mLock);
1147 base::ScopedLockAssertion assumeLocked(mLock);
1148 const bool processCalled =
1149 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1150 return mProcessWasCalled;
1151 });
1152 if (!processCalled) {
1153 FAIL() << "Expected process() to have been called.";
1154 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155 if (outLastEvent) {
1156 *outLastEvent = mLastEvent;
1157 }
1158 mProcessWasCalled = false;
1159 }
1160
1161 void setKeyCodeState(int32_t keyCode, int32_t state) {
1162 mKeyCodeStates.replaceValueFor(keyCode, state);
1163 }
1164
1165 void setScanCodeState(int32_t scanCode, int32_t state) {
1166 mScanCodeStates.replaceValueFor(scanCode, state);
1167 }
1168
1169 void setSwitchState(int32_t switchCode, int32_t state) {
1170 mSwitchStates.replaceValueFor(switchCode, state);
1171 }
1172
1173 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001174 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175 }
1176
Philip Junker4af3b3d2021-12-14 10:36:55 +01001177 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1178 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1179 }
1180
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001182 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183
Chris Yea52ade12020-08-27 16:49:20 -07001184 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 InputMapper::populateDeviceInfo(deviceInfo);
1186
1187 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1188 deviceInfo->setKeyboardType(mKeyboardType);
1189 }
1190 }
1191
Chris Yea52ade12020-08-27 16:49:20 -07001192 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001193 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001195
1196 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001197 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001198 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1199 mViewport = config->getDisplayViewportByPort(*displayPort);
1200 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001201
1202 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 }
1204
Chris Yea52ade12020-08-27 16:49:20 -07001205 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001206 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001208 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 }
1210
Chris Yea52ade12020-08-27 16:49:20 -07001211 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001212 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213 mLastEvent = *rawEvent;
1214 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001215 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001216 }
1217
Chris Yea52ade12020-08-27 16:49:20 -07001218 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1220 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1221 }
1222
Philip Junker4af3b3d2021-12-14 10:36:55 +01001223 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1224 auto it = mKeyCodeMapping.find(locationKeyCode);
1225 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1226 }
1227
Chris Yea52ade12020-08-27 16:49:20 -07001228 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001229 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1230 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1231 }
1232
Chris Yea52ade12020-08-27 16:49:20 -07001233 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1235 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1236 }
1237
Chris Yea52ade12020-08-27 16:49:20 -07001238 // Return true if the device has non-empty key layout.
1239 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1240 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241 for (size_t i = 0; i < numCodes; i++) {
1242 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1243 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1244 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001245 }
1246 }
1247 }
Chris Yea52ade12020-08-27 16:49:20 -07001248 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249 return result;
1250 }
1251
1252 virtual int32_t getMetaState() {
1253 return mMetaState;
1254 }
1255
1256 virtual void fadePointer() {
1257 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001258
1259 virtual std::optional<int32_t> getAssociatedDisplay() {
1260 if (mViewport) {
1261 return std::make_optional(mViewport->displayId);
1262 }
1263 return std::nullopt;
1264 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265};
1266
1267
1268// --- InstrumentedInputReader ---
1269
1270class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001271 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272
1273public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001274 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1275 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001276 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001277 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001279 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001281 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001282
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001283 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001284 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001285 InputDeviceIdentifier identifier;
1286 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001287 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001288 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001289 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290 }
1291
Prabir Pradhan28efc192019-11-05 01:10:04 +00001292 // Make the protected loopOnce method accessible to tests.
1293 using InputReader::loopOnce;
1294
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001296 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1297 const InputDeviceIdentifier& identifier)
1298 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001299 if (!mNextDevices.empty()) {
1300 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1301 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001302 return device;
1303 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001304 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001305 }
1306
arthurhungdcef2dc2020-08-11 14:47:50 +08001307 // --- FakeInputReaderContext ---
1308 class FakeInputReaderContext : public ContextImpl {
1309 int32_t mGlobalMetaState;
1310 bool mUpdateGlobalMetaStateWasCalled;
1311 int32_t mGeneration;
1312
1313 public:
1314 FakeInputReaderContext(InputReader* reader)
1315 : ContextImpl(reader),
1316 mGlobalMetaState(0),
1317 mUpdateGlobalMetaStateWasCalled(false),
1318 mGeneration(1) {}
1319
1320 virtual ~FakeInputReaderContext() {}
1321
1322 void assertUpdateGlobalMetaStateWasCalled() {
1323 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1324 << "Expected updateGlobalMetaState() to have been called.";
1325 mUpdateGlobalMetaStateWasCalled = false;
1326 }
1327
1328 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1329
1330 uint32_t getGeneration() { return mGeneration; }
1331
1332 void updateGlobalMetaState() override {
1333 mUpdateGlobalMetaStateWasCalled = true;
1334 ContextImpl::updateGlobalMetaState();
1335 }
1336
1337 int32_t getGlobalMetaState() override {
1338 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1339 }
1340
1341 int32_t bumpGeneration() override {
1342 mGeneration = ContextImpl::bumpGeneration();
1343 return mGeneration;
1344 }
1345 } mFakeContext;
1346
Michael Wrightd02c5b62014-02-10 15:10:22 -08001347 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001348
1349public:
1350 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001351};
1352
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001353// --- InputReaderPolicyTest ---
1354class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001355protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001356 sp<FakeInputReaderPolicy> mFakePolicy;
1357
Chris Yea52ade12020-08-27 16:49:20 -07001358 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1359 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001360};
1361
1362/**
1363 * Check that empty set of viewports is an acceptable configuration.
1364 * Also try to get internal viewport two different ways - by type and by uniqueId.
1365 *
1366 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1367 * Such configuration is not currently allowed.
1368 */
1369TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001370 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001371
1372 // We didn't add any viewports yet, so there shouldn't be any.
1373 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001374 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001375 ASSERT_FALSE(internalViewport);
1376
1377 // Add an internal viewport, then clear it
1378 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001379 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001380 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001381
1382 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001383 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001385 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386
1387 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001388 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001390 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391
1392 mFakePolicy->clearViewports();
1393 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001394 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001395 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001396 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397 ASSERT_FALSE(internalViewport);
1398}
1399
1400TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1401 const std::string internalUniqueId = "local:0";
1402 const std::string externalUniqueId = "local:1";
1403 const std::string virtualUniqueId1 = "virtual:2";
1404 const std::string virtualUniqueId2 = "virtual:3";
1405 constexpr int32_t virtualDisplayId1 = 2;
1406 constexpr int32_t virtualDisplayId2 = 3;
1407
1408 // Add an internal viewport
1409 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001410 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1411 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001412 // Add an external viewport
1413 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001414 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1415 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001416 // Add an virtual viewport
1417 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001418 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1419 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001420 // Add another virtual viewport
1421 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001422 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1423 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001424
1425 // Check matching by type for internal
1426 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001427 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001428 ASSERT_TRUE(internalViewport);
1429 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1430
1431 // Check matching by type for external
1432 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001433 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001434 ASSERT_TRUE(externalViewport);
1435 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1436
1437 // Check matching by uniqueId for virtual viewport #1
1438 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001439 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001440 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001441 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001442 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1443 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1444
1445 // Check matching by uniqueId for virtual viewport #2
1446 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001447 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001448 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001449 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1451 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1452}
1453
1454
1455/**
1456 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1457 * that lookup works by checking display id.
1458 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1459 */
1460TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1461 const std::string uniqueId1 = "uniqueId1";
1462 const std::string uniqueId2 = "uniqueId2";
1463 constexpr int32_t displayId1 = 2;
1464 constexpr int32_t displayId2 = 3;
1465
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001466 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1467 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001468 for (const ViewportType& type : types) {
1469 mFakePolicy->clearViewports();
1470 // Add a viewport
1471 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001472 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1473 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001474 // Add another viewport
1475 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001476 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1477 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001478
1479 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001480 std::optional<DisplayViewport> viewport1 =
1481 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001482 ASSERT_TRUE(viewport1);
1483 ASSERT_EQ(displayId1, viewport1->displayId);
1484 ASSERT_EQ(type, viewport1->type);
1485
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001486 std::optional<DisplayViewport> viewport2 =
1487 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001488 ASSERT_TRUE(viewport2);
1489 ASSERT_EQ(displayId2, viewport2->displayId);
1490 ASSERT_EQ(type, viewport2->type);
1491
1492 // When there are multiple viewports of the same kind, and uniqueId is not specified
1493 // in the call to getDisplayViewport, then that situation is not supported.
1494 // The viewports can be stored in any order, so we cannot rely on the order, since that
1495 // is just implementation detail.
1496 // However, we can check that it still returns *a* viewport, we just cannot assert
1497 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001498 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001499 ASSERT_TRUE(someViewport);
1500 }
1501}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001502
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001503/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001504 * When we have multiple internal displays make sure we always return the default display when
1505 * querying by type.
1506 */
1507TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1508 const std::string uniqueId1 = "uniqueId1";
1509 const std::string uniqueId2 = "uniqueId2";
1510 constexpr int32_t nonDefaultDisplayId = 2;
1511 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1512 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1513
1514 // Add the default display first and ensure it gets returned.
1515 mFakePolicy->clearViewports();
1516 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001517 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001518 ViewportType::INTERNAL);
1519 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001520 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001521 ViewportType::INTERNAL);
1522
1523 std::optional<DisplayViewport> viewport =
1524 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1525 ASSERT_TRUE(viewport);
1526 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1527 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1528
1529 // Add the default display second to make sure order doesn't matter.
1530 mFakePolicy->clearViewports();
1531 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001532 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001533 ViewportType::INTERNAL);
1534 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001535 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001536 ViewportType::INTERNAL);
1537
1538 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1539 ASSERT_TRUE(viewport);
1540 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1541 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1542}
1543
1544/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001545 * Check getDisplayViewportByPort
1546 */
1547TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001548 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001549 const std::string uniqueId1 = "uniqueId1";
1550 const std::string uniqueId2 = "uniqueId2";
1551 constexpr int32_t displayId1 = 1;
1552 constexpr int32_t displayId2 = 2;
1553 const uint8_t hdmi1 = 0;
1554 const uint8_t hdmi2 = 1;
1555 const uint8_t hdmi3 = 2;
1556
1557 mFakePolicy->clearViewports();
1558 // Add a viewport that's associated with some display port that's not of interest.
1559 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001560 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1561 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001562 // Add another viewport, connected to HDMI1 port
1563 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001564 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1565 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001566
1567 // Check that correct display viewport was returned by comparing the display ports.
1568 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1569 ASSERT_TRUE(hdmi1Viewport);
1570 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1571 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1572
1573 // Check that we can still get the same viewport using the uniqueId
1574 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1575 ASSERT_TRUE(hdmi1Viewport);
1576 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1577 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1578 ASSERT_EQ(type, hdmi1Viewport->type);
1579
1580 // Check that we cannot find a port with "HDMI2", because we never added one
1581 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1582 ASSERT_FALSE(hdmi2Viewport);
1583}
1584
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585// --- InputReaderTest ---
1586
1587class InputReaderTest : public testing::Test {
1588protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001589 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001591 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001592 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593
Chris Yea52ade12020-08-27 16:49:20 -07001594 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001595 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001597 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598
Prabir Pradhan28efc192019-11-05 01:10:04 +00001599 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001600 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601 }
1602
Chris Yea52ade12020-08-27 16:49:20 -07001603 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001604 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001605 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 }
1607
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001608 void addDevice(int32_t eventHubId, const std::string& name,
1609 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001611
1612 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001613 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001614 }
1615 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001616 mReader->loopOnce();
1617 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001618 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1619 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001620 }
1621
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001622 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001624 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001625 }
1626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001628 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001629 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001630 }
1631
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001633 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001634 ftl::Flags<InputDeviceClass> classes,
1635 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001636 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001637 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1638 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001639 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001640 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001641 return mapper;
1642 }
1643};
1644
Chris Ye98d3f532020-10-01 21:48:59 -07001645TEST_F(InputReaderTest, PolicyGetInputDevices) {
1646 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001647 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001648 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001649
1650 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001651 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001654 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001655 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1656 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001657 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001658}
1659
Chris Yee7310032020-09-22 15:36:28 -07001660TEST_F(InputReaderTest, GetMergedInputDevices) {
1661 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1662 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1663 // Add two subdevices to device
1664 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1665 // Must add at least one mapper or the device will be ignored!
1666 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1667 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1668
1669 // Push same device instance for next device to be added, so they'll have same identifier.
1670 mReader->pushNextDevice(device);
1671 mReader->pushNextDevice(device);
1672 ASSERT_NO_FATAL_FAILURE(
1673 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1674 ASSERT_NO_FATAL_FAILURE(
1675 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1676
1677 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001678 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001679}
1680
Chris Yee14523a2020-12-19 13:46:00 -08001681TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1682 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1683 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1684 // Add two subdevices to device
1685 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1686 // Must add at least one mapper or the device will be ignored!
1687 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1688 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1689
1690 // Push same device instance for next device to be added, so they'll have same identifier.
1691 mReader->pushNextDevice(device);
1692 mReader->pushNextDevice(device);
1693 // Sensor device is initially disabled
1694 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1695 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1696 nullptr));
1697 // Device is disabled because the only sub device is a sensor device and disabled initially.
1698 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1699 ASSERT_FALSE(device->isEnabled());
1700 ASSERT_NO_FATAL_FAILURE(
1701 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1702 // The merged device is enabled if any sub device is enabled
1703 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1704 ASSERT_TRUE(device->isEnabled());
1705}
1706
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001707TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001709 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 constexpr int32_t eventHubId = 1;
1711 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001712 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001714 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001715 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001716
Yi Kong9b14ac62018-07-17 13:48:38 -07001717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001718
1719 NotifyDeviceResetArgs resetArgs;
1720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001721 ASSERT_EQ(deviceId, resetArgs.deviceId);
1722
1723 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001724 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001725 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001726
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001728 ASSERT_EQ(deviceId, resetArgs.deviceId);
1729 ASSERT_EQ(device->isEnabled(), false);
1730
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001731 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001732 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001735 ASSERT_EQ(device->isEnabled(), false);
1736
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001737 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001738 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001740 ASSERT_EQ(deviceId, resetArgs.deviceId);
1741 ASSERT_EQ(device->isEnabled(), true);
1742}
1743
Michael Wrightd02c5b62014-02-10 15:10:22 -08001744TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001745 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001746 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001747 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001748 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001749 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001750 AINPUT_SOURCE_KEYBOARD, nullptr);
1751 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001752
1753 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1754 AINPUT_SOURCE_ANY, AKEYCODE_A))
1755 << "Should return unknown when the device id is >= 0 but unknown.";
1756
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001757 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1758 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1759 << "Should return unknown when the device id is valid but the sources are not "
1760 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001761
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001762 ASSERT_EQ(AKEY_STATE_DOWN,
1763 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1764 AKEYCODE_A))
1765 << "Should return value provided by mapper when device id is valid and the device "
1766 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001767
1768 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1769 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1770 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1771
1772 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1773 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1774 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1775}
1776
Philip Junker4af3b3d2021-12-14 10:36:55 +01001777TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1778 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1779 constexpr int32_t eventHubId = 1;
1780 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1781 InputDeviceClass::KEYBOARD,
1782 AINPUT_SOURCE_KEYBOARD, nullptr);
1783 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1784
1785 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1786 << "Should return unknown when the device with the specified id is not found.";
1787
1788 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1789 << "Should return correct mapping when device id is valid and mapping exists.";
1790
1791 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1792 << "Should return the location key code when device id is valid and there's no "
1793 "mapping.";
1794}
1795
1796TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1797 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1798 constexpr int32_t eventHubId = 1;
1799 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1800 InputDeviceClass::JOYSTICK,
1801 AINPUT_SOURCE_GAMEPAD, nullptr);
1802 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1803
1804 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1805 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1806}
1807
Michael Wrightd02c5b62014-02-10 15:10:22 -08001808TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001809 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001810 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001811 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001812 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001813 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001814 AINPUT_SOURCE_KEYBOARD, nullptr);
1815 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001816
1817 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1818 AINPUT_SOURCE_ANY, KEY_A))
1819 << "Should return unknown when the device id is >= 0 but unknown.";
1820
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1822 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1823 << "Should return unknown when the device id is valid but the sources are not "
1824 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001825
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001826 ASSERT_EQ(AKEY_STATE_DOWN,
1827 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1828 KEY_A))
1829 << "Should return value provided by mapper when device id is valid and the device "
1830 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001831
1832 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1833 AINPUT_SOURCE_TRACKBALL, KEY_A))
1834 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1835
1836 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1837 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1838 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1839}
1840
1841TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001842 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001843 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001844 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001845 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001846 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001847 AINPUT_SOURCE_KEYBOARD, nullptr);
1848 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849
1850 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1851 AINPUT_SOURCE_ANY, SW_LID))
1852 << "Should return unknown when the device id is >= 0 but unknown.";
1853
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1855 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1856 << "Should return unknown when the device id is valid but the sources are not "
1857 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001858
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001859 ASSERT_EQ(AKEY_STATE_DOWN,
1860 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1861 SW_LID))
1862 << "Should return value provided by mapper when device id is valid and the device "
1863 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001864
1865 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1866 AINPUT_SOURCE_TRACKBALL, SW_LID))
1867 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1868
1869 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1870 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1871 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1872}
1873
1874TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001875 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001876 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001878 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001879 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001880 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001882 mapper.addSupportedKeyCode(AKEYCODE_A);
1883 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884
1885 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1886 uint8_t flags[4] = { 0, 0, 0, 1 };
1887
1888 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1889 << "Should return false when device id is >= 0 but unknown.";
1890 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1891
1892 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001893 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1894 << "Should return false when device id is valid but the sources are not supported by "
1895 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1897
1898 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001899 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1900 keyCodes, flags))
1901 << "Should return value provided by mapper when device id is valid and the device "
1902 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001903 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1904
1905 flags[3] = 1;
1906 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1907 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1908 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1909
1910 flags[3] = 1;
1911 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1912 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1913 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1914}
1915
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001916TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001917 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001918 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001919
1920 NotifyConfigurationChangedArgs args;
1921
1922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1923 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1924}
1925
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001926TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001927 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001928 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001929 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001930 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001931 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001932 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001933 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001934 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001935
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001936 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001937 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001938 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1939
1940 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001941 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001942 ASSERT_EQ(when, event.when);
1943 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001944 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945 ASSERT_EQ(EV_KEY, event.type);
1946 ASSERT_EQ(KEY_A, event.code);
1947 ASSERT_EQ(1, event.value);
1948}
1949
Garfield Tan1c7bc862020-01-28 13:24:04 -08001950TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001951 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001952 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 constexpr int32_t eventHubId = 1;
1954 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001955 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001956 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001957 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001958 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001959
1960 NotifyDeviceResetArgs resetArgs;
1961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001962 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001963
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001964 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001965 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001967 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001968 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001970 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001971 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001973 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001974 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001975
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001976 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001977 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001979 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001980 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001981}
1982
Garfield Tan1c7bc862020-01-28 13:24:04 -08001983TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1984 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001985 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001986 constexpr int32_t eventHubId = 1;
1987 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1988 // Must add at least one mapper or the device will be ignored!
1989 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001990 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001991 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1992
1993 NotifyDeviceResetArgs resetArgs;
1994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1995 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1996}
1997
Arthur Hungc23540e2018-11-29 20:42:11 +08001998TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001999 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002000 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002001 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002002 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002003 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2004 FakeInputMapper& mapper =
2005 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002006 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002007
2008 const uint8_t hdmi1 = 1;
2009
2010 // Associated touch screen with second display.
2011 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2012
2013 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002014 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002015 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002016 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002017 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002018 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002019 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002020 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002021 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002022 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002023
2024 // Add the device, and make sure all of the callbacks are triggered.
2025 // The device is added after the input port associations are processed since
2026 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002027 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002030 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002031
Arthur Hung2c9a3342019-07-23 14:18:59 +08002032 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002033 ASSERT_EQ(deviceId, device->getId());
2034 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2035 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002036
2037 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002038 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002039 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002040 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002041}
2042
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002043TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2044 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002045 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002046 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2047 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2048 // Must add at least one mapper or the device will be ignored!
2049 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2050 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2051 mReader->pushNextDevice(device);
2052 mReader->pushNextDevice(device);
2053 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2054 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2055
2056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2057
2058 NotifyDeviceResetArgs resetArgs;
2059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2060 ASSERT_EQ(deviceId, resetArgs.deviceId);
2061 ASSERT_TRUE(device->isEnabled());
2062 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2063 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2064
2065 disableDevice(deviceId);
2066 mReader->loopOnce();
2067
2068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2069 ASSERT_EQ(deviceId, resetArgs.deviceId);
2070 ASSERT_FALSE(device->isEnabled());
2071 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2072 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2073
2074 enableDevice(deviceId);
2075 mReader->loopOnce();
2076
2077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2078 ASSERT_EQ(deviceId, resetArgs.deviceId);
2079 ASSERT_TRUE(device->isEnabled());
2080 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2081 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2082}
2083
2084TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2085 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002086 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002087 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2088 // Add two subdevices to device
2089 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2090 FakeInputMapper& mapperDevice1 =
2091 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2092 FakeInputMapper& mapperDevice2 =
2093 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2094 mReader->pushNextDevice(device);
2095 mReader->pushNextDevice(device);
2096 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2097 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2098
2099 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2100 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2101
2102 ASSERT_EQ(AKEY_STATE_DOWN,
2103 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2104 ASSERT_EQ(AKEY_STATE_DOWN,
2105 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2106 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2107 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2108}
2109
Prabir Pradhan7e186182020-11-10 13:56:45 -08002110TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2111 NotifyPointerCaptureChangedArgs args;
2112
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002113 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002114 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2115 mReader->loopOnce();
2116 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002117 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2118 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002119
2120 mFakePolicy->setPointerCapture(false);
2121 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2122 mReader->loopOnce();
2123 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002124 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002125
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002126 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002127 // does not change.
2128 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2129 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002130 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002131}
2132
Chris Ye87143712020-11-10 05:05:58 +00002133class FakeVibratorInputMapper : public FakeInputMapper {
2134public:
2135 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2136 : FakeInputMapper(deviceContext, sources) {}
2137
2138 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2139};
2140
2141TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2142 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002143 ftl::Flags<InputDeviceClass> deviceClass =
2144 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002145 constexpr int32_t eventHubId = 1;
2146 const char* DEVICE_LOCATION = "BLUETOOTH";
2147 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2148 FakeVibratorInputMapper& mapper =
2149 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2150 mReader->pushNextDevice(device);
2151
2152 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2153 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2154
2155 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2156 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2157}
2158
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002159// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002160
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002161class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002162public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002163 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002164
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002165 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002166
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002167 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2168
Chris Yee2b1e5c2021-03-10 22:45:12 -08002169 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2170
2171 void dump(std::string& dump) override {}
2172
2173 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2174 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002175 }
2176
Chris Yee2b1e5c2021-03-10 22:45:12 -08002177 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2178 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002179 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002180
2181 bool setLightColor(int32_t lightId, int32_t color) override {
2182 getDeviceContext().setLightBrightness(lightId, color >> 24);
2183 return true;
2184 }
2185
2186 std::optional<int32_t> getLightColor(int32_t lightId) override {
2187 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2188 if (!result.has_value()) {
2189 return std::nullopt;
2190 }
2191 return result.value() << 24;
2192 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002193
2194 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2195
2196 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2197
2198private:
2199 InputDeviceContext& mDeviceContext;
2200 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2201 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002202 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002203};
2204
Chris Yee2b1e5c2021-03-10 22:45:12 -08002205TEST_F(InputReaderTest, BatteryGetCapacity) {
2206 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002207 ftl::Flags<InputDeviceClass> deviceClass =
2208 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002209 constexpr int32_t eventHubId = 1;
2210 const char* DEVICE_LOCATION = "BLUETOOTH";
2211 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002212 FakePeripheralController& controller =
2213 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002214 mReader->pushNextDevice(device);
2215
2216 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2217
2218 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2219 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2220}
2221
2222TEST_F(InputReaderTest, BatteryGetStatus) {
2223 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002224 ftl::Flags<InputDeviceClass> deviceClass =
2225 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002226 constexpr int32_t eventHubId = 1;
2227 const char* DEVICE_LOCATION = "BLUETOOTH";
2228 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002229 FakePeripheralController& controller =
2230 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002231 mReader->pushNextDevice(device);
2232
2233 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2234
2235 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2236 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2237}
2238
Chris Ye3fdbfef2021-01-06 18:45:18 -08002239TEST_F(InputReaderTest, LightGetColor) {
2240 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002241 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002242 constexpr int32_t eventHubId = 1;
2243 const char* DEVICE_LOCATION = "BLUETOOTH";
2244 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002245 FakePeripheralController& controller =
2246 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002247 mReader->pushNextDevice(device);
2248 RawLightInfo info = {.id = 1,
2249 .name = "Mono",
2250 .maxBrightness = 255,
2251 .flags = InputLightClass::BRIGHTNESS,
2252 .path = ""};
2253 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2254 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2255
2256 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002257
Chris Yee2b1e5c2021-03-10 22:45:12 -08002258 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2259 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002260 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2261 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2262}
2263
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002264// --- InputReaderIntegrationTest ---
2265
2266// These tests create and interact with the InputReader only through its interface.
2267// The InputReader is started during SetUp(), which starts its processing in its own
2268// thread. The tests use linux uinput to emulate input devices.
2269// NOTE: Interacting with the physical device while these tests are running may cause
2270// the tests to fail.
2271class InputReaderIntegrationTest : public testing::Test {
2272protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002273 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002274 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002275 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002276
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002277 std::shared_ptr<FakePointerController> mFakePointerController;
2278
Chris Yea52ade12020-08-27 16:49:20 -07002279 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002280 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002281 mFakePointerController = std::make_shared<FakePointerController>();
2282 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002283 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2284 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002285
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002286 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2287 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002288 ASSERT_EQ(mReader->start(), OK);
2289
2290 // Since this test is run on a real device, all the input devices connected
2291 // to the test device will show up in mReader. We wait for those input devices to
2292 // show up before beginning the tests.
2293 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2295 }
2296
Chris Yea52ade12020-08-27 16:49:20 -07002297 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002298 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002299 mReader.reset();
2300 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002301 mFakePolicy.clear();
2302 }
2303};
2304
2305TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2306 // An invalid input device that is only used for this test.
2307 class InvalidUinputDevice : public UinputDevice {
2308 public:
2309 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2310
2311 private:
2312 void configureDevice(int fd, uinput_user_dev* device) override {}
2313 };
2314
2315 const size_t numDevices = mFakePolicy->getInputDevices().size();
2316
2317 // UinputDevice does not set any event or key bits, so InputReader should not
2318 // consider it as a valid device.
2319 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2320 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2321 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2322 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2323
2324 invalidDevice.reset();
2325 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2326 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2327 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2328}
2329
2330TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2331 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2332
2333 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2334 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2335 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2336 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2337
2338 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002339 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002340 const auto& it =
2341 std::find_if(inputDevices.begin(), inputDevices.end(),
2342 [&keyboard](const InputDeviceInfo& info) {
2343 return info.getIdentifier().name == keyboard->getName();
2344 });
2345
2346 ASSERT_NE(it, inputDevices.end());
2347 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2348 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2349 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002350
2351 keyboard.reset();
2352 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2353 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2354 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2355}
2356
2357TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2358 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2359 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2360
2361 NotifyConfigurationChangedArgs configChangedArgs;
2362 ASSERT_NO_FATAL_FAILURE(
2363 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002364 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002365 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2366
2367 NotifyKeyArgs keyArgs;
2368 keyboard->pressAndReleaseHomeKey();
2369 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2370 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002371 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002372 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002373 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002374 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002375 prevTimestamp = keyArgs.eventTime;
2376
2377 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2378 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002379 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002380 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002381 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002382}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002384/**
2385 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2386 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2387 * are passed to the listener.
2388 */
2389static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2390TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2391 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2393 NotifyKeyArgs keyArgs;
2394
2395 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2396 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2398 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2399
2400 controller->pressAndReleaseKey(BTN_GEAR_UP);
2401 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2403 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2404}
2405
Arthur Hungaab25622020-01-16 11:22:11 +08002406// --- TouchProcessTest ---
2407class TouchIntegrationTest : public InputReaderIntegrationTest {
2408protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002409 const std::string UNIQUE_ID = "local:0";
2410
Chris Yea52ade12020-08-27 16:49:20 -07002411 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002412 InputReaderIntegrationTest::SetUp();
2413 // At least add an internal display.
2414 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2415 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002416 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002417
2418 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2419 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2420 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2421 }
2422
2423 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2424 int32_t orientation, const std::string& uniqueId,
2425 std::optional<uint8_t> physicalPort,
2426 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002427 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2428 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002429 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2430 }
2431
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002432 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2433 NotifyMotionArgs args;
2434 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2435 EXPECT_EQ(action, args.action);
2436 ASSERT_EQ(points.size(), args.pointerCount);
2437 for (size_t i = 0; i < args.pointerCount; i++) {
2438 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2439 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2440 }
2441 }
2442
Arthur Hungaab25622020-01-16 11:22:11 +08002443 std::unique_ptr<UinputTouchScreen> mDevice;
2444};
2445
2446TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2447 NotifyMotionArgs args;
2448 const Point centerPoint = mDevice->getCenterPoint();
2449
2450 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002451 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002452 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002453 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2456
2457 // ACTION_MOVE
2458 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002459 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002460 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2462
2463 // ACTION_UP
2464 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002465 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002466 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2467 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2468}
2469
2470TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2471 NotifyMotionArgs args;
2472 const Point centerPoint = mDevice->getCenterPoint();
2473
2474 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002475 mDevice->sendSlot(FIRST_SLOT);
2476 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002477 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002478 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2480 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2481
2482 // ACTION_POINTER_DOWN (Second slot)
2483 const Point secondPoint = centerPoint + Point(100, 100);
2484 mDevice->sendSlot(SECOND_SLOT);
2485 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002486 mDevice->sendDown(secondPoint);
2487 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002488 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002489 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002490
2491 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002492 mDevice->sendMove(secondPoint + Point(1, 1));
2493 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002494 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2496
2497 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002498 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002499 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002501 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002502
2503 // ACTION_UP
2504 mDevice->sendSlot(FIRST_SLOT);
2505 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002506 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002507 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2508 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2509}
2510
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002511/**
2512 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2513 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2514 * data?
2515 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2516 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2517 * for Pointer 0 only is generated after.
2518 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2519 * events, we will not miss any information.
2520 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2521 * event generated afterwards that contains the newest movement of pointer 0.
2522 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2523 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2524 * losing information about non-palm pointers.
2525 */
2526TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2527 NotifyMotionArgs args;
2528 const Point centerPoint = mDevice->getCenterPoint();
2529
2530 // ACTION_DOWN
2531 mDevice->sendSlot(FIRST_SLOT);
2532 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2533 mDevice->sendDown(centerPoint);
2534 mDevice->sendSync();
2535 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2536
2537 // ACTION_POINTER_DOWN (Second slot)
2538 const Point secondPoint = centerPoint + Point(100, 100);
2539 mDevice->sendSlot(SECOND_SLOT);
2540 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2541 mDevice->sendDown(secondPoint);
2542 mDevice->sendSync();
2543 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2544
2545 // ACTION_MOVE (First slot)
2546 mDevice->sendSlot(FIRST_SLOT);
2547 mDevice->sendMove(centerPoint + Point(5, 5));
2548 // ACTION_POINTER_UP (Second slot)
2549 mDevice->sendSlot(SECOND_SLOT);
2550 mDevice->sendPointerUp();
2551 // Send a single sync for the above 2 pointer updates
2552 mDevice->sendSync();
2553
2554 // First, we should get POINTER_UP for the second pointer
2555 assertReceivedMotion(ACTION_POINTER_1_UP,
2556 {/*first pointer */ centerPoint + Point(5, 5),
2557 /*second pointer*/ secondPoint});
2558
2559 // Next, the MOVE event for the first pointer
2560 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2561}
2562
2563/**
2564 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2565 * move, and then it will go up, all in the same frame.
2566 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2567 * gets sent to the listener.
2568 */
2569TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2570 NotifyMotionArgs args;
2571 const Point centerPoint = mDevice->getCenterPoint();
2572
2573 // ACTION_DOWN
2574 mDevice->sendSlot(FIRST_SLOT);
2575 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2576 mDevice->sendDown(centerPoint);
2577 mDevice->sendSync();
2578 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2579
2580 // ACTION_POINTER_DOWN (Second slot)
2581 const Point secondPoint = centerPoint + Point(100, 100);
2582 mDevice->sendSlot(SECOND_SLOT);
2583 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2584 mDevice->sendDown(secondPoint);
2585 mDevice->sendSync();
2586 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2587
2588 // ACTION_MOVE (First slot)
2589 mDevice->sendSlot(FIRST_SLOT);
2590 mDevice->sendMove(centerPoint + Point(5, 5));
2591 // ACTION_POINTER_UP (Second slot)
2592 mDevice->sendSlot(SECOND_SLOT);
2593 mDevice->sendMove(secondPoint + Point(6, 6));
2594 mDevice->sendPointerUp();
2595 // Send a single sync for the above 2 pointer updates
2596 mDevice->sendSync();
2597
2598 // First, we should get POINTER_UP for the second pointer
2599 // The movement of the second pointer during the liftoff frame is ignored.
2600 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2601 assertReceivedMotion(ACTION_POINTER_1_UP,
2602 {/*first pointer */ centerPoint + Point(5, 5),
2603 /*second pointer*/ secondPoint});
2604
2605 // Next, the MOVE event for the first pointer
2606 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2607}
2608
Arthur Hungaab25622020-01-16 11:22:11 +08002609TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2610 NotifyMotionArgs args;
2611 const Point centerPoint = mDevice->getCenterPoint();
2612
2613 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002614 mDevice->sendSlot(FIRST_SLOT);
2615 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002616 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002617 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002618 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2619 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2620
arthurhungcc7f9802020-04-30 17:55:40 +08002621 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002622 const Point secondPoint = centerPoint + Point(100, 100);
2623 mDevice->sendSlot(SECOND_SLOT);
2624 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2625 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002626 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002627 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002628 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002629
arthurhungcc7f9802020-04-30 17:55:40 +08002630 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002631 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002632 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002633 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2635
arthurhungcc7f9802020-04-30 17:55:40 +08002636 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2637 // a palm event.
2638 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002639 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002640 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002641 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002642 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002643 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002644
arthurhungcc7f9802020-04-30 17:55:40 +08002645 // Send up to second slot, expect first slot send moving.
2646 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002647 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002648 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002650
arthurhungcc7f9802020-04-30 17:55:40 +08002651 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002652 mDevice->sendSlot(FIRST_SLOT);
2653 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002654 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002655
arthurhungcc7f9802020-04-30 17:55:40 +08002656 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2657 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002658}
2659
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661class InputDeviceTest : public testing::Test {
2662protected:
2663 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002664 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 static const int32_t DEVICE_ID;
2666 static const int32_t DEVICE_GENERATION;
2667 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002668 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002669 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002671 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002673 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002674 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002675 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676
Chris Yea52ade12020-08-27 16:49:20 -07002677 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002678 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002680 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002681 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002682 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683 InputDeviceIdentifier identifier;
2684 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002685 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002686 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002687 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002688 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002689 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002690 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 }
2692
Chris Yea52ade12020-08-27 16:49:20 -07002693 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002694 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 }
2697};
2698
2699const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002700const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002701const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2703const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002704const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002705 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002706const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707
2708TEST_F(InputDeviceTest, ImmutableProperties) {
2709 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002710 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002711 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712}
2713
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002714TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2715 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002716}
2717
Michael Wrightd02c5b62014-02-10 15:10:22 -08002718TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2719 // Configuration.
2720 InputReaderConfiguration config;
2721 mDevice->configure(ARBITRARY_TIME, &config, 0);
2722
2723 // Reset.
2724 mDevice->reset(ARBITRARY_TIME);
2725
2726 NotifyDeviceResetArgs resetArgs;
2727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2728 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2729 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2730
2731 // Metadata.
2732 ASSERT_TRUE(mDevice->isIgnored());
2733 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2734
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002735 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002736 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002737 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2739 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2740
2741 // State queries.
2742 ASSERT_EQ(0, mDevice->getMetaState());
2743
2744 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2745 << "Ignored device should return unknown key code state.";
2746 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2747 << "Ignored device should return unknown scan code state.";
2748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2749 << "Ignored device should return unknown switch state.";
2750
2751 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2752 uint8_t flags[2] = { 0, 1 };
2753 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2754 << "Ignored device should never mark any key codes.";
2755 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2756 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2757}
2758
2759TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2760 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002761 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002762
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002763 FakeInputMapper& mapper1 =
2764 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002765 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2766 mapper1.setMetaState(AMETA_ALT_ON);
2767 mapper1.addSupportedKeyCode(AKEYCODE_A);
2768 mapper1.addSupportedKeyCode(AKEYCODE_B);
2769 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2770 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2771 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2772 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2773 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002775 FakeInputMapper& mapper2 =
2776 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002777 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778
2779 InputReaderConfiguration config;
2780 mDevice->configure(ARBITRARY_TIME, &config, 0);
2781
2782 String8 propertyValue;
2783 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2784 << "Device should have read configuration during configuration phase.";
2785 ASSERT_STREQ("value", propertyValue.string());
2786
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2788 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789
2790 // Reset
2791 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002792 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2793 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794
2795 NotifyDeviceResetArgs resetArgs;
2796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2797 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2798 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2799
2800 // Metadata.
2801 ASSERT_FALSE(mDevice->isIgnored());
2802 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2803
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002804 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002806 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002807 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2808 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2809
2810 // State queries.
2811 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2812 << "Should query mappers and combine meta states.";
2813
2814 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2815 << "Should return unknown key code state when source not supported.";
2816 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2817 << "Should return unknown scan code state when source not supported.";
2818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2819 << "Should return unknown switch state when source not supported.";
2820
2821 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2822 << "Should query mapper when source is supported.";
2823 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2824 << "Should query mapper when source is supported.";
2825 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2826 << "Should query mapper when source is supported.";
2827
2828 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2829 uint8_t flags[4] = { 0, 0, 0, 1 };
2830 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2831 << "Should do nothing when source is unsupported.";
2832 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2833 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2834 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2835 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2836
2837 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2838 << "Should query mapper when source is supported.";
2839 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2840 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2841 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2842 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2843
2844 // Event handling.
2845 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002846 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847 mDevice->process(&event, 1);
2848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002849 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2850 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851}
2852
Arthur Hung2c9a3342019-07-23 14:18:59 +08002853// A single input device is associated with a specific display. Check that:
2854// 1. Device is disabled if the viewport corresponding to the associated display is not found
2855// 2. Device is disabled when setEnabled API is called
2856TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002857 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002858
2859 // First Configuration.
2860 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2861
2862 // Device should be enabled by default.
2863 ASSERT_TRUE(mDevice->isEnabled());
2864
2865 // Prepare associated info.
2866 constexpr uint8_t hdmi = 1;
2867 const std::string UNIQUE_ID = "local:1";
2868
2869 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2870 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2871 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2872 // Device should be disabled because it is associated with a specific display via
2873 // input port <-> display port association, but the corresponding display is not found
2874 ASSERT_FALSE(mDevice->isEnabled());
2875
2876 // Prepare displays.
2877 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002878 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2879 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002880 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2881 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2882 ASSERT_TRUE(mDevice->isEnabled());
2883
2884 // Device should be disabled after set disable.
2885 mFakePolicy->addDisabledDevice(mDevice->getId());
2886 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2887 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2888 ASSERT_FALSE(mDevice->isEnabled());
2889
2890 // Device should still be disabled even found the associated display.
2891 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2892 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2893 ASSERT_FALSE(mDevice->isEnabled());
2894}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002895
Christine Franks1ba71cc2021-04-07 14:37:42 -07002896TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2897 // Device should be enabled by default.
2898 mFakePolicy->clearViewports();
2899 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2900 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2901 ASSERT_TRUE(mDevice->isEnabled());
2902
2903 // Device should be disabled because it is associated with a specific display, but the
2904 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002905 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002906 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2907 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2908 ASSERT_FALSE(mDevice->isEnabled());
2909
2910 // Device should be enabled when a display is found.
2911 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2912 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2913 NO_PORT, ViewportType::INTERNAL);
2914 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2915 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2916 ASSERT_TRUE(mDevice->isEnabled());
2917
2918 // Device should be disabled after set disable.
2919 mFakePolicy->addDisabledDevice(mDevice->getId());
2920 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2921 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2922 ASSERT_FALSE(mDevice->isEnabled());
2923
2924 // Device should still be disabled even found the associated display.
2925 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2926 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2927 ASSERT_FALSE(mDevice->isEnabled());
2928}
2929
Christine Franks2a2293c2022-01-18 11:51:16 -08002930TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2931 mFakePolicy->clearViewports();
2932 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2933 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2934
Christine Franks2a2293c2022-01-18 11:51:16 -08002935 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2936 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2937 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2938 NO_PORT, ViewportType::INTERNAL);
2939 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2940 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2941 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2942}
2943
Siarhei Vishniakou6c57b2f2022-09-28 10:48:29 -07002944/**
2945 * This test reproduces a crash caused by a dangling reference that remains after device is added
2946 * and removed. The reference is accessed in InputDevice::dump(..);
2947 */
2948TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2949 constexpr int32_t TEST_EVENTHUB_ID = 10;
2950 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2951
2952 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
2953 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
2954 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2955 std::string dumpStr, eventHubDevStr;
2956 device.dump(dumpStr, eventHubDevStr);
2957}
2958
Michael Wrightd02c5b62014-02-10 15:10:22 -08002959// --- InputMapperTest ---
2960
2961class InputMapperTest : public testing::Test {
2962protected:
2963 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002964 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965 static const int32_t DEVICE_ID;
2966 static const int32_t DEVICE_GENERATION;
2967 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002968 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002969 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002970
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002971 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002973 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002974 std::unique_ptr<InstrumentedInputReader> mReader;
2975 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002976
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002977 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002978 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002980 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002981 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002982 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002983 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002984 // Consume the device reset notification generated when adding a new device.
2985 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986 }
2987
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002988 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002989 SetUp(DEVICE_CLASSES);
2990 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002991
Chris Yea52ade12020-08-27 16:49:20 -07002992 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002993 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995 }
2996
2997 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002998 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999 }
3000
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003001 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003002 if (!changes ||
3003 (changes &
3004 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3005 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003006 mReader->requestRefreshConfiguration(changes);
3007 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003008 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003009 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhan36690412022-08-05 22:26:56 +00003010 // Loop the reader to flush the input listener queue.
3011 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003012 }
3013
arthurhungdcef2dc2020-08-11 14:47:50 +08003014 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3015 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003016 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003017 InputDeviceIdentifier identifier;
3018 identifier.name = name;
3019 identifier.location = location;
3020 std::shared_ptr<InputDevice> device =
3021 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3022 identifier);
3023 mReader->pushNextDevice(device);
3024 mFakeEventHub->addDevice(eventHubId, name, classes);
3025 mReader->loopOnce();
3026 return device;
3027 }
3028
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003029 template <class T, typename... Args>
3030 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003031 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003032 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003034 mapper.reset(ARBITRARY_TIME);
Prabir Pradhan36690412022-08-05 22:26:56 +00003035 // Loop the reader to flush the input listener queue.
3036 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003037 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003038 }
3039
3040 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003041 int32_t orientation, const std::string& uniqueId,
3042 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003043 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3044 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003045 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3046 }
3047
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003048 void clearViewports() {
3049 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050 }
3051
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003052 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3053 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 RawEvent event;
3055 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003056 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003057 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 event.type = type;
3059 event.code = code;
3060 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003061 mapper.process(&event);
Prabir Pradhan36690412022-08-05 22:26:56 +00003062 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003063 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064 }
3065
3066 static void assertMotionRange(const InputDeviceInfo& info,
3067 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3068 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003069 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3071 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3072 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3073 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3074 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3075 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3076 }
3077
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003078 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3079 float size, float touchMajor, float touchMinor, float toolMajor,
3080 float toolMinor, float orientation, float distance,
3081 float scaledAxisEpsilon = 1.f) {
3082 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3083 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3085 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003086 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3087 scaledAxisEpsilon);
3088 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3089 scaledAxisEpsilon);
3090 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3091 scaledAxisEpsilon);
3092 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3093 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3095 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3096 }
3097
Michael Wright17db18e2020-06-26 20:51:44 +01003098 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003100 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101 ASSERT_NEAR(x, actualX, 1);
3102 ASSERT_NEAR(y, actualY, 1);
3103 }
3104};
3105
3106const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003107const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003108const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3110const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003111const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3112 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003113const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114
3115// --- SwitchInputMapperTest ---
3116
3117class SwitchInputMapperTest : public InputMapperTest {
3118protected:
3119};
3120
3121TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003122 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003124 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125}
3126
3127TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003128 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003130 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003131 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003133 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003134 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135}
3136
3137TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003138 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144
3145 NotifySwitchArgs args;
3146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3147 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003148 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3149 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150 args.switchMask);
3151 ASSERT_EQ(uint32_t(0), args.policyFlags);
3152}
3153
Chris Ye87143712020-11-10 05:05:58 +00003154// --- VibratorInputMapperTest ---
3155class VibratorInputMapperTest : public InputMapperTest {
3156protected:
3157 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3158};
3159
3160TEST_F(VibratorInputMapperTest, GetSources) {
3161 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3162
3163 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3164}
3165
3166TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3167 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3168
3169 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3170}
3171
3172TEST_F(VibratorInputMapperTest, Vibrate) {
3173 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003174 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003175 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3176
3177 VibrationElement pattern(2);
3178 VibrationSequence sequence(2);
3179 pattern.duration = std::chrono::milliseconds(200);
3180 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3181 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3182 sequence.addElement(pattern);
3183 pattern.duration = std::chrono::milliseconds(500);
3184 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3185 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3186 sequence.addElement(pattern);
3187
3188 std::vector<int64_t> timings = {0, 1};
3189 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3190
3191 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003192 // Start vibrating
3193 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003194 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003195 // Verify vibrator state listener was notified.
3196 mReader->loopOnce();
3197 NotifyVibratorStateArgs args;
3198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3199 ASSERT_EQ(DEVICE_ID, args.deviceId);
3200 ASSERT_TRUE(args.isOn);
3201 // Stop vibrating
3202 mapper.cancelVibrate(VIBRATION_TOKEN);
3203 ASSERT_FALSE(mapper.isVibrating());
3204 // Verify vibrator state listener was notified.
3205 mReader->loopOnce();
3206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3207 ASSERT_EQ(DEVICE_ID, args.deviceId);
3208 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003209}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
Chris Yef59a2f42020-10-16 12:55:26 -07003211// --- SensorInputMapperTest ---
3212
3213class SensorInputMapperTest : public InputMapperTest {
3214protected:
3215 static const int32_t ACCEL_RAW_MIN;
3216 static const int32_t ACCEL_RAW_MAX;
3217 static const int32_t ACCEL_RAW_FUZZ;
3218 static const int32_t ACCEL_RAW_FLAT;
3219 static const int32_t ACCEL_RAW_RESOLUTION;
3220
3221 static const int32_t GYRO_RAW_MIN;
3222 static const int32_t GYRO_RAW_MAX;
3223 static const int32_t GYRO_RAW_FUZZ;
3224 static const int32_t GYRO_RAW_FLAT;
3225 static const int32_t GYRO_RAW_RESOLUTION;
3226
3227 static const float GRAVITY_MS2_UNIT;
3228 static const float DEGREE_RADIAN_UNIT;
3229
3230 void prepareAccelAxes();
3231 void prepareGyroAxes();
3232 void setAccelProperties();
3233 void setGyroProperties();
3234 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3235};
3236
3237const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3238const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3239const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3240const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3241const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3242
3243const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3244const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3245const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3246const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3247const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3248
3249const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3250const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3251
3252void SensorInputMapperTest::prepareAccelAxes() {
3253 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3254 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3255 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3256 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3258 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3259}
3260
3261void SensorInputMapperTest::prepareGyroAxes() {
3262 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3263 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3264 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3265 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3267 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3268}
3269
3270void SensorInputMapperTest::setAccelProperties() {
3271 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3272 /* sensorDataIndex */ 0);
3273 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3274 /* sensorDataIndex */ 1);
3275 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3276 /* sensorDataIndex */ 2);
3277 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3278 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3279 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3280 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3281 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3282}
3283
3284void SensorInputMapperTest::setGyroProperties() {
3285 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3286 /* sensorDataIndex */ 0);
3287 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3288 /* sensorDataIndex */ 1);
3289 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3290 /* sensorDataIndex */ 2);
3291 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3292 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3293 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3294 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3295 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3296}
3297
3298TEST_F(SensorInputMapperTest, GetSources) {
3299 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3300
3301 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3302}
3303
3304TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3305 setAccelProperties();
3306 prepareAccelAxes();
3307 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3308
3309 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3310 std::chrono::microseconds(10000),
3311 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003312 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003318
3319 NotifySensorArgs args;
3320 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3321 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3322 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3323
3324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3325 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3326 ASSERT_EQ(args.deviceId, DEVICE_ID);
3327 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3328 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3329 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3330 ASSERT_EQ(args.values, values);
3331 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3332}
3333
3334TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3335 setGyroProperties();
3336 prepareGyroAxes();
3337 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3338
3339 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3340 std::chrono::microseconds(10000),
3341 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003342 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003348
3349 NotifySensorArgs args;
3350 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3351 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3352 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3353
3354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3355 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3356 ASSERT_EQ(args.deviceId, DEVICE_ID);
3357 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3358 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3359 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3360 ASSERT_EQ(args.values, values);
3361 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3362}
3363
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364// --- KeyboardInputMapperTest ---
3365
3366class KeyboardInputMapperTest : public InputMapperTest {
3367protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003368 const std::string UNIQUE_ID = "local:0";
3369
3370 void prepareDisplay(int32_t orientation);
3371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003373 int32_t originalKeyCode, int32_t rotatedKeyCode,
3374 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375};
3376
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003377/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3378 * orientation.
3379 */
3380void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003381 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3382 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003383}
3384
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003385void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003386 int32_t originalScanCode, int32_t originalKeyCode,
3387 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388 NotifyKeyArgs args;
3389
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3392 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3393 ASSERT_EQ(originalScanCode, args.scanCode);
3394 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003395 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3399 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3400 ASSERT_EQ(originalScanCode, args.scanCode);
3401 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003402 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403}
3404
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003406 KeyboardInputMapper& mapper =
3407 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3408 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003410 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411}
3412
3413TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3414 const int32_t USAGE_A = 0x070004;
3415 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003416 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3417 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003418 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3419 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3420 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003422 KeyboardInputMapper& mapper =
3423 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3424 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003425 // Initial metastate is AMETA_NONE.
3426 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427
3428 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430 NotifyKeyArgs args;
3431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3432 ASSERT_EQ(DEVICE_ID, args.deviceId);
3433 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3434 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3435 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3436 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3437 ASSERT_EQ(KEY_HOME, args.scanCode);
3438 ASSERT_EQ(AMETA_NONE, args.metaState);
3439 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3440 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3441 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3442
3443 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003444 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3446 ASSERT_EQ(DEVICE_ID, args.deviceId);
3447 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3448 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3449 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3450 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3451 ASSERT_EQ(KEY_HOME, args.scanCode);
3452 ASSERT_EQ(AMETA_NONE, args.metaState);
3453 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3454 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3455 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3456
3457 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3461 ASSERT_EQ(DEVICE_ID, args.deviceId);
3462 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3463 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3464 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3465 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3466 ASSERT_EQ(0, args.scanCode);
3467 ASSERT_EQ(AMETA_NONE, args.metaState);
3468 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3469 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3470 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3471
3472 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3474 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3476 ASSERT_EQ(DEVICE_ID, args.deviceId);
3477 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3478 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3479 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3480 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3481 ASSERT_EQ(0, args.scanCode);
3482 ASSERT_EQ(AMETA_NONE, args.metaState);
3483 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3484 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3485 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3486
3487 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3491 ASSERT_EQ(DEVICE_ID, args.deviceId);
3492 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3493 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3494 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3495 ASSERT_EQ(0, args.keyCode);
3496 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3497 ASSERT_EQ(AMETA_NONE, args.metaState);
3498 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3499 ASSERT_EQ(0U, args.policyFlags);
3500 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3501
3502 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3504 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3506 ASSERT_EQ(DEVICE_ID, args.deviceId);
3507 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3508 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3509 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3510 ASSERT_EQ(0, args.keyCode);
3511 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3512 ASSERT_EQ(AMETA_NONE, args.metaState);
3513 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3514 ASSERT_EQ(0U, args.policyFlags);
3515 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3516}
3517
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003518/**
3519 * Ensure that the readTime is set to the time when the EV_KEY is received.
3520 */
3521TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3522 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3523
3524 KeyboardInputMapper& mapper =
3525 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3526 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3527 NotifyKeyArgs args;
3528
3529 // Key down
3530 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3532 ASSERT_EQ(12, args.readTime);
3533
3534 // Key up
3535 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3537 ASSERT_EQ(15, args.readTime);
3538}
3539
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003541 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3542 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003543 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3544 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3545 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003547 KeyboardInputMapper& mapper =
3548 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3549 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550
Arthur Hung95f68612022-04-07 14:08:22 +08003551 // Initial metastate is AMETA_NONE.
3552 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553
3554 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556 NotifyKeyArgs args;
3557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003560 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
3562 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003563 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003566 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567
3568 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003569 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3571 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003572 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573
3574 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003575 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3577 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003578 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003579 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580}
3581
3582TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003583 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3584 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3585 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3586 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003588 KeyboardInputMapper& mapper =
3589 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3590 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003592 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3594 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3595 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3596 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3597 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3598 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3599 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3600 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3601}
3602
3603TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003604 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3605 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3606 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3607 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003610 KeyboardInputMapper& mapper =
3611 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3612 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003614 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003615 ASSERT_NO_FATAL_FAILURE(
3616 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3617 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3618 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3619 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3620 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3622 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003624 clearViewports();
3625 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003626 ASSERT_NO_FATAL_FAILURE(
3627 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3628 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3629 AKEYCODE_DPAD_UP, DISPLAY_ID));
3630 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3631 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3633 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003635 clearViewports();
3636 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003637 ASSERT_NO_FATAL_FAILURE(
3638 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3639 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3640 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3641 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3642 AKEYCODE_DPAD_UP, DISPLAY_ID));
3643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3644 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003646 clearViewports();
3647 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003648 ASSERT_NO_FATAL_FAILURE(
3649 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3650 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3651 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3652 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3653 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3654 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3655 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656
3657 // Special case: if orientation changes while key is down, we still emit the same keycode
3658 // in the key up as we did in the key down.
3659 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003660 clearViewports();
3661 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3664 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3665 ASSERT_EQ(KEY_UP, args.scanCode);
3666 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3667
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003668 clearViewports();
3669 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3672 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3673 ASSERT_EQ(KEY_UP, args.scanCode);
3674 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3675}
3676
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003677TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3678 // If the keyboard is not orientation aware,
3679 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003680 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003682 KeyboardInputMapper& mapper =
3683 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3684 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003685 NotifyKeyArgs args;
3686
3687 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3692 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3693
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003694 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3699 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3700}
3701
3702TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3703 // If the keyboard is orientation aware,
3704 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003705 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003706
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003707 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 KeyboardInputMapper& mapper =
3709 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3710 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003711 NotifyKeyArgs args;
3712
3713 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3714 // ^--- already checked by the previous test
3715
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003716 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003717 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3722 ASSERT_EQ(DISPLAY_ID, args.displayId);
3723
3724 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003725 clearViewports();
3726 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003727 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3732 ASSERT_EQ(newDisplayId, args.displayId);
3733}
3734
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003736 KeyboardInputMapper& mapper =
3737 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3738 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003740 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003741 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003743 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003744 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745}
3746
Philip Junker4af3b3d2021-12-14 10:36:55 +01003747TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3748 KeyboardInputMapper& mapper =
3749 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3750 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3751
3752 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3753 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3754 << "If a mapping is available, the result is equal to the mapping";
3755
3756 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3757 << "If no mapping is available, the result is the key location";
3758}
3759
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761 KeyboardInputMapper& mapper =
3762 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3763 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003765 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003768 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003769 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770}
3771
3772TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003773 KeyboardInputMapper& mapper =
3774 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3775 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003777 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778
3779 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3780 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003781 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782 ASSERT_TRUE(flags[0]);
3783 ASSERT_FALSE(flags[1]);
3784}
3785
3786TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003787 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3788 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3789 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3790 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3791 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3792 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003794 KeyboardInputMapper& mapper =
3795 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3796 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003797 // Initial metastate is AMETA_NONE.
3798 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799
3800 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3802 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804
3805 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003808 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3809 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3810 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003811 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812
3813 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003816 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3817 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003819 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003820
3821 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3825 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3826 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003827 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828
3829 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003832 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3833 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3834 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003835 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836
3837 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003840 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3841 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3842 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003843 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003844
3845 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003848 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3849 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3850 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003851 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852}
3853
Chris Yea52ade12020-08-27 16:49:20 -07003854TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3855 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3856 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3857 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3858 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3859
3860 KeyboardInputMapper& mapper =
3861 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3862 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3863
Chris Yea52ade12020-08-27 16:49:20 -07003864 // Meta state should be AMETA_NONE after reset
3865 mapper.reset(ARBITRARY_TIME);
3866 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3867 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3868 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3869 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3870
3871 NotifyKeyArgs args;
3872 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3875 ASSERT_EQ(AMETA_NONE, args.metaState);
3876 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3877 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3878 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3879
3880 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003881 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3883 ASSERT_EQ(AMETA_NONE, args.metaState);
3884 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3885 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3886 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3887}
3888
Arthur Hung2c9a3342019-07-23 14:18:59 +08003889TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3890 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003891 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3892 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3893 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3894 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003895
3896 // keyboard 2.
3897 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003898 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003899 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003900 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003901 std::shared_ptr<InputDevice> device2 =
3902 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003903 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003904
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003905 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3906 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3907 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3908 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003909
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003910 KeyboardInputMapper& mapper =
3911 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3912 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003913
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003914 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003915 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003916 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003917 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3918 device2->reset(ARBITRARY_TIME);
3919
3920 // Prepared displays and associated info.
3921 constexpr uint8_t hdmi1 = 0;
3922 constexpr uint8_t hdmi2 = 1;
3923 const std::string SECONDARY_UNIQUE_ID = "local:1";
3924
3925 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3926 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3927
3928 // No associated display viewport found, should disable the device.
3929 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3930 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3931 ASSERT_FALSE(device2->isEnabled());
3932
3933 // Prepare second display.
3934 constexpr int32_t newDisplayId = 2;
3935 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003936 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003937 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003938 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003939 // Default device will reconfigure above, need additional reconfiguration for another device.
3940 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3941 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3942
3943 // Device should be enabled after the associated display is found.
3944 ASSERT_TRUE(mDevice->isEnabled());
3945 ASSERT_TRUE(device2->isEnabled());
3946
3947 // Test pad key events
3948 ASSERT_NO_FATAL_FAILURE(
3949 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3950 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3951 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3952 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3953 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3954 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3955 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3956
3957 ASSERT_NO_FATAL_FAILURE(
3958 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3959 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3960 AKEYCODE_DPAD_RIGHT, newDisplayId));
3961 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3962 AKEYCODE_DPAD_DOWN, newDisplayId));
3963 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3964 AKEYCODE_DPAD_LEFT, newDisplayId));
3965}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966
arthurhungc903df12020-08-11 15:08:42 +08003967TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3968 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3969 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3970 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3971 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3972 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3973 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3974
3975 KeyboardInputMapper& mapper =
3976 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3977 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003978 // Initial metastate is AMETA_NONE.
3979 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003980
3981 // Initialization should have turned all of the lights off.
3982 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3983 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3984 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3985
3986 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003989 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3990 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3991
3992 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003995 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3996 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3997
3998 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004001 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4002 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4003
4004 mFakeEventHub->removeDevice(EVENTHUB_ID);
4005 mReader->loopOnce();
4006
4007 // keyboard 2 should default toggle keys.
4008 const std::string USB2 = "USB2";
4009 const std::string DEVICE_NAME2 = "KEYBOARD2";
4010 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4011 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4012 std::shared_ptr<InputDevice> device2 =
4013 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004014 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004015 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4016 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4017 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4018 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4019 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4020 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4021
arthurhung6fe95782020-10-05 22:41:16 +08004022 KeyboardInputMapper& mapper2 =
4023 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4024 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004025 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4026 device2->reset(ARBITRARY_TIME);
4027
4028 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4029 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4030 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004031 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4032 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004033}
4034
Arthur Hungcb40a002021-08-03 14:31:01 +00004035TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4036 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4037 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4038 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4039
4040 // Suppose we have two mappers. (DPAD + KEYBOARD)
4041 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4042 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4043 KeyboardInputMapper& mapper =
4044 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4045 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004046 // Initial metastate is AMETA_NONE.
4047 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004048
4049 mReader->toggleCapsLockState(DEVICE_ID);
4050 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4051}
4052
Arthur Hungfb3cc112022-04-13 07:39:50 +00004053TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4054 // keyboard 1.
4055 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4056 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4057 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4058 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4059 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4060 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4061
4062 KeyboardInputMapper& mapper1 =
4063 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4064 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4065
4066 // keyboard 2.
4067 const std::string USB2 = "USB2";
4068 const std::string DEVICE_NAME2 = "KEYBOARD2";
4069 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4070 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4071 std::shared_ptr<InputDevice> device2 =
4072 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4073 ftl::Flags<InputDeviceClass>(0));
4074 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4075 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4076 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4077 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4078 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4079 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4080
4081 KeyboardInputMapper& mapper2 =
4082 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4083 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4084 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4085 device2->reset(ARBITRARY_TIME);
4086
Arthur Hung95f68612022-04-07 14:08:22 +08004087 // Initial metastate is AMETA_NONE.
4088 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4089 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4090
4091 // Toggle num lock on and off.
4092 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4093 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004094 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4095 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4096 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4097
4098 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4099 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4100 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4101 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4102 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4103
4104 // Toggle caps lock on and off.
4105 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4106 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4107 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4108 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4109 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4110
4111 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4112 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4113 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4114 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4115 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4116
4117 // Toggle scroll lock on and off.
4118 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4119 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4120 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4121 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4122 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4123
4124 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4125 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4126 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4127 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4128 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4129}
4130
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004131// --- KeyboardInputMapperTest_ExternalDevice ---
4132
4133class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4134protected:
Chris Yea52ade12020-08-27 16:49:20 -07004135 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004136};
4137
4138TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004139 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4140 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004141
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004142 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4143 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4144 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4145 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004147 KeyboardInputMapper& mapper =
4148 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4149 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004152 NotifyKeyArgs args;
4153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4154 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4155
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004156 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4158 ASSERT_EQ(uint32_t(0), args.policyFlags);
4159
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4162 ASSERT_EQ(uint32_t(0), args.policyFlags);
4163
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004164 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4166 ASSERT_EQ(uint32_t(0), args.policyFlags);
4167
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4170 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4171
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004172 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4174 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4175}
4176
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004177TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004178 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004179
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004180 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4181 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4182 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004183
Powei Fengd041c5d2019-05-03 17:11:33 -07004184 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004185 KeyboardInputMapper& mapper =
4186 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4187 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004188
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004190 NotifyKeyArgs args;
4191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4192 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4193
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004194 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4196 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4197
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4200 ASSERT_EQ(uint32_t(0), args.policyFlags);
4201
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004202 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4204 ASSERT_EQ(uint32_t(0), args.policyFlags);
4205
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4208 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4209
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004210 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4212 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4213}
4214
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215// --- CursorInputMapperTest ---
4216
4217class CursorInputMapperTest : public InputMapperTest {
4218protected:
4219 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4220
Michael Wright17db18e2020-06-26 20:51:44 +01004221 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222
Chris Yea52ade12020-08-27 16:49:20 -07004223 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224 InputMapperTest::SetUp();
4225
Michael Wright17db18e2020-06-26 20:51:44 +01004226 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004227 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 }
4229
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004230 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4231 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004232
4233 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004234 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4235 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4236 }
4237
4238 void prepareSecondaryDisplay() {
4239 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4240 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4241 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004242 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004243
4244 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4245 float pressure) {
4246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4247 0.0f, 0.0f, 0.0f, EPSILON));
4248 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249};
4250
4251const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4252
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004253void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4254 int32_t originalY, int32_t rotatedX,
4255 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 NotifyMotionArgs args;
4257
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004263 ASSERT_NO_FATAL_FAILURE(
4264 assertCursorPointerCoords(args.pointerCoords[0],
4265 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4266 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267}
4268
4269TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004271 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004273 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274}
4275
4276TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004278 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004280 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281}
4282
4283TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004285 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286
4287 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004288 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289
4290 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004291 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4292 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4294 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4295
4296 // When the bounds are set, then there should be a valid motion range.
4297 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4298
4299 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004300 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301
4302 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4303 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4304 1, 800 - 1, 0.0f, 0.0f));
4305 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4306 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4307 2, 480 - 1, 0.0f, 0.0f));
4308 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4309 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4310 0.0f, 1.0f, 0.0f, 0.0f));
4311}
4312
4313TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004315 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316
4317 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004318 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319
4320 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4321 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4322 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4323 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4324 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4325 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4326 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4327 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4328 0.0f, 1.0f, 0.0f, 0.0f));
4329}
4330
4331TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004333 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334
arthurhungdcef2dc2020-08-11 14:47:50 +08004335 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336
4337 NotifyMotionArgs args;
4338
4339 // Button press.
4340 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4344 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4345 ASSERT_EQ(DEVICE_ID, args.deviceId);
4346 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4347 ASSERT_EQ(uint32_t(0), args.policyFlags);
4348 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4349 ASSERT_EQ(0, args.flags);
4350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4351 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4352 ASSERT_EQ(0, args.edgeFlags);
4353 ASSERT_EQ(uint32_t(1), args.pointerCount);
4354 ASSERT_EQ(0, args.pointerProperties[0].id);
4355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004356 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4358 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4359 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4360
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4362 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4363 ASSERT_EQ(DEVICE_ID, args.deviceId);
4364 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4365 ASSERT_EQ(uint32_t(0), args.policyFlags);
4366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4367 ASSERT_EQ(0, args.flags);
4368 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4369 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4370 ASSERT_EQ(0, args.edgeFlags);
4371 ASSERT_EQ(uint32_t(1), args.pointerCount);
4372 ASSERT_EQ(0, args.pointerProperties[0].id);
4373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004374 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4376 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4377 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4378
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004380 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4381 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4383 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4384 ASSERT_EQ(DEVICE_ID, args.deviceId);
4385 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4386 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4388 ASSERT_EQ(0, args.flags);
4389 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4390 ASSERT_EQ(0, args.buttonState);
4391 ASSERT_EQ(0, args.edgeFlags);
4392 ASSERT_EQ(uint32_t(1), args.pointerCount);
4393 ASSERT_EQ(0, args.pointerProperties[0].id);
4394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004395 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004396 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4397 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4398 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4399
4400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4401 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4402 ASSERT_EQ(DEVICE_ID, args.deviceId);
4403 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4404 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4406 ASSERT_EQ(0, args.flags);
4407 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4408 ASSERT_EQ(0, args.buttonState);
4409 ASSERT_EQ(0, args.edgeFlags);
4410 ASSERT_EQ(uint32_t(1), args.pointerCount);
4411 ASSERT_EQ(0, args.pointerProperties[0].id);
4412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4415 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4416 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4417}
4418
4419TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004421 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422
4423 NotifyMotionArgs args;
4424
4425 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004430 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4431 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4432 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433
4434 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004439 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4440 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441}
4442
4443TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004445 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446
4447 NotifyMotionArgs args;
4448
4449 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004454 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004458 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004465 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004466
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004469 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470}
4471
4472TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004474 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475
4476 NotifyMotionArgs args;
4477
4478 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4486 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4487 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4490 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004491 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4492 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4493 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004494
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004501 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4502 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4503 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504
4505 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004510 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004511
4512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004514 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515}
4516
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004517TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004518 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004520 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4521 // need to be rotated.
4522 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004523 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004525 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4534}
4535
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004536TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004537 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004539 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4540 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004541 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004543 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004544 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4546 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4547 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4548 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4553
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004554 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004555 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004556 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4557 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4558 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4559 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4561 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004565 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004566 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4568 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4569 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4570 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4571 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4575
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00004576 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004577 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004578 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4579 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4580 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4581 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4582 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4583 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4584 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4585 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586}
4587
4588TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004590 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591
4592 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4593 mFakePointerController->setPosition(100, 200);
4594 mFakePointerController->setButtonState(0);
4595
4596 NotifyMotionArgs motionArgs;
4597 NotifyKeyArgs keyArgs;
4598
4599 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4601 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4603 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4604 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4605 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004606 ASSERT_NO_FATAL_FAILURE(
4607 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4610 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4611 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4612 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004613 ASSERT_NO_FATAL_FAILURE(
4614 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_EQ(0, motionArgs.buttonState);
4621 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004622 ASSERT_NO_FATAL_FAILURE(
4623 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624
4625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004626 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627 ASSERT_EQ(0, motionArgs.buttonState);
4628 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004629 ASSERT_NO_FATAL_FAILURE(
4630 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004631
4632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004634 ASSERT_EQ(0, motionArgs.buttonState);
4635 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004636 ASSERT_NO_FATAL_FAILURE(
4637 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638
4639 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4644 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4645 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4646 motionArgs.buttonState);
4647 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4648 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004649 ASSERT_NO_FATAL_FAILURE(
4650 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4653 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4654 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4655 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4656 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004657 ASSERT_NO_FATAL_FAILURE(
4658 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004659
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4661 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4662 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4663 motionArgs.buttonState);
4664 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4665 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004666 ASSERT_NO_FATAL_FAILURE(
4667 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004668
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004672 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4674 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004675 ASSERT_NO_FATAL_FAILURE(
4676 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004677
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004680 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4681 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004682 ASSERT_NO_FATAL_FAILURE(
4683 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004688 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4689 ASSERT_EQ(0, motionArgs.buttonState);
4690 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004691 ASSERT_NO_FATAL_FAILURE(
4692 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004695
4696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 ASSERT_EQ(0, motionArgs.buttonState);
4698 ASSERT_EQ(0, mFakePointerController->getButtonState());
4699 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004700 ASSERT_NO_FATAL_FAILURE(
4701 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004702
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4704 ASSERT_EQ(0, motionArgs.buttonState);
4705 ASSERT_EQ(0, mFakePointerController->getButtonState());
4706 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004707 ASSERT_NO_FATAL_FAILURE(
4708 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709
4710 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4714 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4715 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004716
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004718 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4720 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004721 ASSERT_NO_FATAL_FAILURE(
4722 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004723
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4726 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4727 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004728 ASSERT_NO_FATAL_FAILURE(
4729 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004734 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735 ASSERT_EQ(0, motionArgs.buttonState);
4736 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004737 ASSERT_NO_FATAL_FAILURE(
4738 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004739
4740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004742 ASSERT_EQ(0, motionArgs.buttonState);
4743 ASSERT_EQ(0, mFakePointerController->getButtonState());
4744
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004745 ASSERT_NO_FATAL_FAILURE(
4746 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4748 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4749 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4750
4751 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4755 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4756 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004757
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4761 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004762 ASSERT_NO_FATAL_FAILURE(
4763 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004764
4765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4766 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4767 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4768 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004769 ASSERT_NO_FATAL_FAILURE(
4770 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004775 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 ASSERT_EQ(0, motionArgs.buttonState);
4777 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004778 ASSERT_NO_FATAL_FAILURE(
4779 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004780
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4783 ASSERT_EQ(0, motionArgs.buttonState);
4784 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004785 ASSERT_NO_FATAL_FAILURE(
4786 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004787
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4789 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4790 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4791
4792 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4796 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4797 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004798
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004800 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4802 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004803 ASSERT_NO_FATAL_FAILURE(
4804 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004805
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4807 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4808 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4809 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004810 ASSERT_NO_FATAL_FAILURE(
4811 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004816 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_EQ(0, motionArgs.buttonState);
4818 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004819 ASSERT_NO_FATAL_FAILURE(
4820 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004821
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4824 ASSERT_EQ(0, motionArgs.buttonState);
4825 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004826 ASSERT_NO_FATAL_FAILURE(
4827 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004828
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4830 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4831 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4832
4833 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4837 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4838 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004839
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004841 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4843 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004844 ASSERT_NO_FATAL_FAILURE(
4845 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004846
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4848 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4849 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4850 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004851 ASSERT_NO_FATAL_FAILURE(
4852 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004857 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858 ASSERT_EQ(0, motionArgs.buttonState);
4859 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004860 ASSERT_NO_FATAL_FAILURE(
4861 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004862
4863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4864 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4865 ASSERT_EQ(0, motionArgs.buttonState);
4866 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004867 ASSERT_NO_FATAL_FAILURE(
4868 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004869
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4871 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4872 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4873}
4874
4875TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878
4879 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4880 mFakePointerController->setPosition(100, 200);
4881 mFakePointerController->setButtonState(0);
4882
4883 NotifyMotionArgs args;
4884
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004889 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4890 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4892 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 +01004893 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004894}
4895
4896TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004897 addConfigurationProperty("cursor.mode", "pointer");
4898 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004899 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004900
4901 NotifyDeviceResetArgs resetArgs;
4902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4903 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4904 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4905
4906 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4907 mFakePointerController->setPosition(100, 200);
4908 mFakePointerController->setButtonState(0);
4909
4910 NotifyMotionArgs args;
4911
4912 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004913 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4914 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4917 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4920 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 +01004921 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004922
4923 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4927 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4928 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4930 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4932 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4935 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4936
4937 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004938 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4939 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4941 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4942 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4944 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4946 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4947 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4949 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4950
4951 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4956 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4959 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 +01004960 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004961
4962 // Disable pointer capture and check that the device generation got bumped
4963 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004964 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004965 mFakePolicy->setPointerCapture(false);
4966 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004967 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004968
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004970 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4971
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4976 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4979 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 +01004980 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981}
4982
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004983/**
4984 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4985 * pointer acceleration or speed processing should not be applied.
4986 */
4987TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4988 addConfigurationProperty("cursor.mode", "pointer");
4989 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4990 100.f /*high threshold*/, 10.f /*acceleration*/);
4991 mFakePolicy->setVelocityControlParams(testParams);
4992 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4993
4994 NotifyDeviceResetArgs resetArgs;
4995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4996 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4997 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4998
4999 NotifyMotionArgs args;
5000
5001 // Move and verify scale is applied.
5002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5006 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5007 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5008 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5009 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5010 ASSERT_GT(relX, 10);
5011 ASSERT_GT(relY, 20);
5012
5013 // Enable Pointer Capture
5014 mFakePolicy->setPointerCapture(true);
5015 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5016 NotifyPointerCaptureChangedArgs captureArgs;
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5018 ASSERT_TRUE(captureArgs.request.enable);
5019
5020 // Move and verify scale is not applied.
5021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5025 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5027 ASSERT_EQ(10, args.pointerCoords[0].getX());
5028 ASSERT_EQ(20, args.pointerCoords[0].getY());
5029}
5030
Prabir Pradhan258e2b92022-06-24 18:37:04 +00005031TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5032 addConfigurationProperty("cursor.mode", "pointer");
5033 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5034
5035 NotifyDeviceResetArgs resetArgs;
5036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5037 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5038 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5039
5040 // Ensure the display is rotated.
5041 prepareDisplay(DISPLAY_ORIENTATION_90);
5042
5043 NotifyMotionArgs args;
5044
5045 // Verify that the coordinates are rotated.
5046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5050 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5051 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5052 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5053 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5054
5055 // Enable Pointer Capture.
5056 mFakePolicy->setPointerCapture(true);
5057 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5058 NotifyPointerCaptureChangedArgs captureArgs;
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5060 ASSERT_TRUE(captureArgs.request.enable);
5061
5062 // Move and verify rotation is not applied.
5063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5067 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5069 ASSERT_EQ(10, args.pointerCoords[0].getX());
5070 ASSERT_EQ(20, args.pointerCoords[0].getY());
5071}
5072
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005073TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005074 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005075
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005076 // Set up the default display.
5077 prepareDisplay(DISPLAY_ORIENTATION_90);
5078
5079 // Set up the secondary display as the display on which the pointer should be shown.
5080 // The InputDevice is not associated with any display.
5081 prepareSecondaryDisplay();
5082 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005083 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5084
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005085 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005086 mFakePointerController->setPosition(100, 200);
5087 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005088
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005089 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5094 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5095 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005096 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc04d04d2022-09-08 22:03:30 +00005097}
5098
5099TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5100 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5101
5102 // Set up the default display.
5103 prepareDisplay(DISPLAY_ORIENTATION_90);
5104
5105 // Set up the secondary display as the display on which the pointer should be shown,
5106 // and associate the InputDevice with the secondary display.
5107 prepareSecondaryDisplay();
5108 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5109 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5110 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5111
5112 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5113 mFakePointerController->setPosition(100, 200);
5114 mFakePointerController->setButtonState(0);
5115
5116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5120 AllOf(WithAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE),
5121 WithDisplayId(SECONDARY_DISPLAY_ID), WithCoords(110.0f, 220.0f))));
5122 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5123}
5124
5125TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5126 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5127
5128 // Set up the default display as the display on which the pointer should be shown.
5129 prepareDisplay(DISPLAY_ORIENTATION_90);
5130 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5131
5132 // Associate the InputDevice with the secondary display.
5133 prepareSecondaryDisplay();
5134 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5135 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5136
5137 // The mapper should not generate any events because it is associated with a display that is
5138 // different from the pointer display.
5139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005143}
5144
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145// --- TouchInputMapperTest ---
5146
5147class TouchInputMapperTest : public InputMapperTest {
5148protected:
5149 static const int32_t RAW_X_MIN;
5150 static const int32_t RAW_X_MAX;
5151 static const int32_t RAW_Y_MIN;
5152 static const int32_t RAW_Y_MAX;
5153 static const int32_t RAW_TOUCH_MIN;
5154 static const int32_t RAW_TOUCH_MAX;
5155 static const int32_t RAW_TOOL_MIN;
5156 static const int32_t RAW_TOOL_MAX;
5157 static const int32_t RAW_PRESSURE_MIN;
5158 static const int32_t RAW_PRESSURE_MAX;
5159 static const int32_t RAW_ORIENTATION_MIN;
5160 static const int32_t RAW_ORIENTATION_MAX;
5161 static const int32_t RAW_DISTANCE_MIN;
5162 static const int32_t RAW_DISTANCE_MAX;
5163 static const int32_t RAW_TILT_MIN;
5164 static const int32_t RAW_TILT_MAX;
5165 static const int32_t RAW_ID_MIN;
5166 static const int32_t RAW_ID_MAX;
5167 static const int32_t RAW_SLOT_MIN;
5168 static const int32_t RAW_SLOT_MAX;
5169 static const float X_PRECISION;
5170 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005171 static const float X_PRECISION_VIRTUAL;
5172 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173
5174 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005175 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176
5177 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5178
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005179 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005180 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005181
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182 enum Axes {
5183 POSITION = 1 << 0,
5184 TOUCH = 1 << 1,
5185 TOOL = 1 << 2,
5186 PRESSURE = 1 << 3,
5187 ORIENTATION = 1 << 4,
5188 MINOR = 1 << 5,
5189 ID = 1 << 6,
5190 DISTANCE = 1 << 7,
5191 TILT = 1 << 8,
5192 SLOT = 1 << 9,
5193 TOOL_TYPE = 1 << 10,
5194 };
5195
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005196 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5197 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005198 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005200 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201 int32_t toRawX(float displayX);
5202 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005203 int32_t toRotatedRawX(float displayX);
5204 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005205 float toCookedX(float rawX, float rawY);
5206 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005208 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005210 float toDisplayY(int32_t rawY, int32_t displayHeight);
5211
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212};
5213
5214const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5215const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5216const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5217const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5218const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5219const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5220const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5221const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005222const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5223const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5225const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5226const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5227const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5228const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5229const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5230const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5231const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5232const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5233const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5234const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5235const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005236const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5237 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5238const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5239 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005240const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5241 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242
5243const float TouchInputMapperTest::GEOMETRIC_SCALE =
5244 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5245 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5246
5247const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5248 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5249 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5250};
5251
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005252void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005253 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5254 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005255}
5256
5257void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5258 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5259 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260}
5261
Santos Cordonfa5cf462017-04-05 10:37:00 -07005262void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005263 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5264 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5265 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005266}
5267
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005269 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5270 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5271 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5272 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273}
5274
Jason Gerecke489fda82012-09-07 17:19:40 -07005275void TouchInputMapperTest::prepareLocationCalibration() {
5276 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5277}
5278
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279int32_t TouchInputMapperTest::toRawX(float displayX) {
5280 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5281}
5282
5283int32_t TouchInputMapperTest::toRawY(float displayY) {
5284 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5285}
5286
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005287int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5288 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5289}
5290
5291int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5292 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5293}
5294
Jason Gerecke489fda82012-09-07 17:19:40 -07005295float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5296 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5297 return rawX;
5298}
5299
5300float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5301 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5302 return rawY;
5303}
5304
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005306 return toDisplayX(rawX, DISPLAY_WIDTH);
5307}
5308
5309float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5310 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311}
5312
5313float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005314 return toDisplayY(rawY, DISPLAY_HEIGHT);
5315}
5316
5317float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5318 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319}
5320
5321
5322// --- SingleTouchInputMapperTest ---
5323
5324class SingleTouchInputMapperTest : public TouchInputMapperTest {
5325protected:
5326 void prepareButtons();
5327 void prepareAxes(int axes);
5328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005329 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5330 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5331 void processUp(SingleTouchInputMapper& mappery);
5332 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5333 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5334 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5335 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5336 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5337 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338};
5339
5340void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005341 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342}
5343
5344void SingleTouchInputMapperTest::prepareAxes(int axes) {
5345 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005346 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5347 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348 }
5349 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005350 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5351 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352 }
5353 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005354 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5355 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356 }
5357 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005358 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5359 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360 }
5361 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005362 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5363 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364 }
5365}
5366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005367void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005371}
5372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005373void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376}
5377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005378void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380}
5381
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005382void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384}
5385
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005386void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5387 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389}
5390
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005391void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393}
5394
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005395void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5396 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399}
5400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005401void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5402 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404}
5405
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005406void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408}
5409
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 prepareButtons();
5412 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005413 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005415 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416}
5417
5418TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005419 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5420 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421 prepareButtons();
5422 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005423 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005425 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426}
5427
5428TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 prepareButtons();
5430 prepareAxes(POSITION);
5431 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005432 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005434 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435}
5436
5437TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438 prepareButtons();
5439 prepareAxes(POSITION);
5440 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005441 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444}
5445
5446TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447 addConfigurationProperty("touch.deviceType", "touchScreen");
5448 prepareDisplay(DISPLAY_ORIENTATION_0);
5449 prepareButtons();
5450 prepareAxes(POSITION);
5451 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005452 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005453
5454 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005455 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456
5457 // Virtual key is down.
5458 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5459 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5460 processDown(mapper, x, y);
5461 processSync(mapper);
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005464 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465
5466 // Virtual key is up.
5467 processUp(mapper);
5468 processSync(mapper);
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005471 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472}
5473
5474TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475 addConfigurationProperty("touch.deviceType", "touchScreen");
5476 prepareDisplay(DISPLAY_ORIENTATION_0);
5477 prepareButtons();
5478 prepareAxes(POSITION);
5479 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005480 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481
5482 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005483 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484
5485 // Virtual key is down.
5486 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5487 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5488 processDown(mapper, x, y);
5489 processSync(mapper);
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493
5494 // Virtual key is up.
5495 processUp(mapper);
5496 processSync(mapper);
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005499 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500}
5501
5502TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503 addConfigurationProperty("touch.deviceType", "touchScreen");
5504 prepareDisplay(DISPLAY_ORIENTATION_0);
5505 prepareButtons();
5506 prepareAxes(POSITION);
5507 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005508 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005509
5510 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5511 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005512 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513 ASSERT_TRUE(flags[0]);
5514 ASSERT_FALSE(flags[1]);
5515}
5516
5517TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518 addConfigurationProperty("touch.deviceType", "touchScreen");
5519 prepareDisplay(DISPLAY_ORIENTATION_0);
5520 prepareButtons();
5521 prepareAxes(POSITION);
5522 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005523 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524
arthurhungdcef2dc2020-08-11 14:47:50 +08005525 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526
5527 NotifyKeyArgs args;
5528
5529 // Press virtual key.
5530 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5531 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5532 processDown(mapper, x, y);
5533 processSync(mapper);
5534
5535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5536 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5537 ASSERT_EQ(DEVICE_ID, args.deviceId);
5538 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5539 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5540 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5541 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5542 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5543 ASSERT_EQ(KEY_HOME, args.scanCode);
5544 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5545 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5546
5547 // Release virtual key.
5548 processUp(mapper);
5549 processSync(mapper);
5550
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5552 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5553 ASSERT_EQ(DEVICE_ID, args.deviceId);
5554 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5555 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5556 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5557 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5558 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5559 ASSERT_EQ(KEY_HOME, args.scanCode);
5560 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5561 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5562
5563 // Should not have sent any motions.
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5565}
5566
5567TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568 addConfigurationProperty("touch.deviceType", "touchScreen");
5569 prepareDisplay(DISPLAY_ORIENTATION_0);
5570 prepareButtons();
5571 prepareAxes(POSITION);
5572 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005573 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005574
arthurhungdcef2dc2020-08-11 14:47:50 +08005575 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576
5577 NotifyKeyArgs keyArgs;
5578
5579 // Press virtual key.
5580 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5581 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5582 processDown(mapper, x, y);
5583 processSync(mapper);
5584
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5586 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5587 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5588 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5589 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5590 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5591 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5592 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5593 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5594 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5595 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5596
5597 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5598 // into the display area.
5599 y -= 100;
5600 processMove(mapper, x, y);
5601 processSync(mapper);
5602
5603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5604 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5605 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5606 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5607 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5608 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5609 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5610 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5611 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5612 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5613 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5614 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5615
5616 NotifyMotionArgs motionArgs;
5617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5618 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5619 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5620 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5621 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5622 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5623 ASSERT_EQ(0, motionArgs.flags);
5624 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5625 ASSERT_EQ(0, motionArgs.buttonState);
5626 ASSERT_EQ(0, motionArgs.edgeFlags);
5627 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5628 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5631 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5632 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5633 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5634 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5635
5636 // Keep moving out of bounds. Should generate a pointer move.
5637 y -= 50;
5638 processMove(mapper, x, y);
5639 processSync(mapper);
5640
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5643 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5644 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5645 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5647 ASSERT_EQ(0, motionArgs.flags);
5648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5649 ASSERT_EQ(0, motionArgs.buttonState);
5650 ASSERT_EQ(0, motionArgs.edgeFlags);
5651 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5655 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5656 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5657 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5659
5660 // Release out of bounds. Should generate a pointer up.
5661 processUp(mapper);
5662 processSync(mapper);
5663
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5665 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5666 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5667 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5668 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5669 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5670 ASSERT_EQ(0, motionArgs.flags);
5671 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5672 ASSERT_EQ(0, motionArgs.buttonState);
5673 ASSERT_EQ(0, motionArgs.edgeFlags);
5674 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5675 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5678 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5679 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5680 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5681 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5682
5683 // Should not have sent any more keys or motions.
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5686}
5687
5688TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689 addConfigurationProperty("touch.deviceType", "touchScreen");
5690 prepareDisplay(DISPLAY_ORIENTATION_0);
5691 prepareButtons();
5692 prepareAxes(POSITION);
5693 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005694 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695
arthurhungdcef2dc2020-08-11 14:47:50 +08005696 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697
5698 NotifyMotionArgs motionArgs;
5699
5700 // Initially go down out of bounds.
5701 int32_t x = -10;
5702 int32_t y = -10;
5703 processDown(mapper, x, y);
5704 processSync(mapper);
5705
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5707
5708 // Move into the display area. Should generate a pointer down.
5709 x = 50;
5710 y = 75;
5711 processMove(mapper, x, y);
5712 processSync(mapper);
5713
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5715 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5716 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5717 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5718 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5719 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5720 ASSERT_EQ(0, motionArgs.flags);
5721 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5722 ASSERT_EQ(0, motionArgs.buttonState);
5723 ASSERT_EQ(0, motionArgs.edgeFlags);
5724 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5725 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5728 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5729 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5730 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5732
5733 // Release. Should generate a pointer up.
5734 processUp(mapper);
5735 processSync(mapper);
5736
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5738 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5739 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5740 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5741 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5742 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5743 ASSERT_EQ(0, motionArgs.flags);
5744 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5745 ASSERT_EQ(0, motionArgs.buttonState);
5746 ASSERT_EQ(0, motionArgs.edgeFlags);
5747 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5748 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5751 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5752 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5753 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5754 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5755
5756 // Should not have sent any more keys or motions.
5757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5759}
5760
Santos Cordonfa5cf462017-04-05 10:37:00 -07005761TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005762 addConfigurationProperty("touch.deviceType", "touchScreen");
5763 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5764
5765 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5766 prepareButtons();
5767 prepareAxes(POSITION);
5768 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005769 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005770
arthurhungdcef2dc2020-08-11 14:47:50 +08005771 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005772
5773 NotifyMotionArgs motionArgs;
5774
5775 // Down.
5776 int32_t x = 100;
5777 int32_t y = 125;
5778 processDown(mapper, x, y);
5779 processSync(mapper);
5780
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5783 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5784 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5785 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5786 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5787 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5788 ASSERT_EQ(0, motionArgs.flags);
5789 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5790 ASSERT_EQ(0, motionArgs.buttonState);
5791 ASSERT_EQ(0, motionArgs.edgeFlags);
5792 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5793 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5796 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5797 1, 0, 0, 0, 0, 0, 0, 0));
5798 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5799 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5800 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5801
5802 // Move.
5803 x += 50;
5804 y += 75;
5805 processMove(mapper, x, y);
5806 processSync(mapper);
5807
5808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5809 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5810 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5811 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5812 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5813 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5815 ASSERT_EQ(0, motionArgs.flags);
5816 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5817 ASSERT_EQ(0, motionArgs.buttonState);
5818 ASSERT_EQ(0, motionArgs.edgeFlags);
5819 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5820 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5823 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5824 1, 0, 0, 0, 0, 0, 0, 0));
5825 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5826 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5827 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5828
5829 // Up.
5830 processUp(mapper);
5831 processSync(mapper);
5832
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5834 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5835 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5836 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5837 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5838 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5839 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5840 ASSERT_EQ(0, motionArgs.flags);
5841 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5842 ASSERT_EQ(0, motionArgs.buttonState);
5843 ASSERT_EQ(0, motionArgs.edgeFlags);
5844 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5845 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5848 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5849 1, 0, 0, 0, 0, 0, 0, 0));
5850 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5851 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5852 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5853
5854 // Should not have sent any more keys or motions.
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5857}
5858
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860 addConfigurationProperty("touch.deviceType", "touchScreen");
5861 prepareDisplay(DISPLAY_ORIENTATION_0);
5862 prepareButtons();
5863 prepareAxes(POSITION);
5864 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005865 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866
arthurhungdcef2dc2020-08-11 14:47:50 +08005867 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868
5869 NotifyMotionArgs motionArgs;
5870
5871 // Down.
5872 int32_t x = 100;
5873 int32_t y = 125;
5874 processDown(mapper, x, y);
5875 processSync(mapper);
5876
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5878 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5879 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5880 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5881 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5882 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5883 ASSERT_EQ(0, motionArgs.flags);
5884 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5885 ASSERT_EQ(0, motionArgs.buttonState);
5886 ASSERT_EQ(0, motionArgs.edgeFlags);
5887 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5888 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5891 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5892 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5893 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5894 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5895
5896 // Move.
5897 x += 50;
5898 y += 75;
5899 processMove(mapper, x, y);
5900 processSync(mapper);
5901
5902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5903 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5904 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5905 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5906 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5908 ASSERT_EQ(0, motionArgs.flags);
5909 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5910 ASSERT_EQ(0, motionArgs.buttonState);
5911 ASSERT_EQ(0, motionArgs.edgeFlags);
5912 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5913 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5916 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5917 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5918 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5919 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5920
5921 // Up.
5922 processUp(mapper);
5923 processSync(mapper);
5924
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5927 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5928 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5929 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5931 ASSERT_EQ(0, motionArgs.flags);
5932 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5933 ASSERT_EQ(0, motionArgs.buttonState);
5934 ASSERT_EQ(0, motionArgs.edgeFlags);
5935 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5936 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5939 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5940 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5941 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5942 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5943
5944 // Should not have sent any more keys or motions.
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5947}
5948
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005949TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950 addConfigurationProperty("touch.deviceType", "touchScreen");
5951 prepareButtons();
5952 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005953 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5954 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005955 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956
5957 NotifyMotionArgs args;
5958
5959 // Rotation 90.
5960 prepareDisplay(DISPLAY_ORIENTATION_90);
5961 processDown(mapper, toRawX(50), toRawY(75));
5962 processSync(mapper);
5963
5964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5965 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5966 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5967
5968 processUp(mapper);
5969 processSync(mapper);
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5971}
5972
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005973TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974 addConfigurationProperty("touch.deviceType", "touchScreen");
5975 prepareButtons();
5976 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005977 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5978 // orientation-aware are affected by display rotation.
5979 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005980 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981
5982 NotifyMotionArgs args;
5983
5984 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005985 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986 prepareDisplay(DISPLAY_ORIENTATION_0);
5987 processDown(mapper, toRawX(50), toRawY(75));
5988 processSync(mapper);
5989
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5991 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5992 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5993
5994 processUp(mapper);
5995 processSync(mapper);
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5997
5998 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005999 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006001 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 processSync(mapper);
6003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6005 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6006 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6007
6008 processUp(mapper);
6009 processSync(mapper);
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6011
6012 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006013 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014 prepareDisplay(DISPLAY_ORIENTATION_180);
6015 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6016 processSync(mapper);
6017
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6019 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6020 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6021
6022 processUp(mapper);
6023 processSync(mapper);
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6025
6026 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006027 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006029 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 processSync(mapper);
6031
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6033 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6034 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6035
6036 processUp(mapper);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6039}
6040
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006041TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6042 addConfigurationProperty("touch.deviceType", "touchScreen");
6043 prepareButtons();
6044 prepareAxes(POSITION);
6045 addConfigurationProperty("touch.orientationAware", "1");
6046 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6047 clearViewports();
6048 prepareDisplay(DISPLAY_ORIENTATION_0);
6049 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6050 NotifyMotionArgs args;
6051
6052 // Orientation 0.
6053 processDown(mapper, toRawX(50), toRawY(75));
6054 processSync(mapper);
6055
6056 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6057 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6058 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6059
6060 processUp(mapper);
6061 processSync(mapper);
6062 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6063}
6064
6065TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6066 addConfigurationProperty("touch.deviceType", "touchScreen");
6067 prepareButtons();
6068 prepareAxes(POSITION);
6069 addConfigurationProperty("touch.orientationAware", "1");
6070 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6071 clearViewports();
6072 prepareDisplay(DISPLAY_ORIENTATION_0);
6073 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6074 NotifyMotionArgs args;
6075
6076 // Orientation 90.
6077 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6078 processSync(mapper);
6079
6080 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6081 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6082 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6083
6084 processUp(mapper);
6085 processSync(mapper);
6086 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6087}
6088
6089TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6090 addConfigurationProperty("touch.deviceType", "touchScreen");
6091 prepareButtons();
6092 prepareAxes(POSITION);
6093 addConfigurationProperty("touch.orientationAware", "1");
6094 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6095 clearViewports();
6096 prepareDisplay(DISPLAY_ORIENTATION_0);
6097 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6098 NotifyMotionArgs args;
6099
6100 // Orientation 180.
6101 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6102 processSync(mapper);
6103
6104 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6105 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6106 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6107
6108 processUp(mapper);
6109 processSync(mapper);
6110 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6111}
6112
6113TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6114 addConfigurationProperty("touch.deviceType", "touchScreen");
6115 prepareButtons();
6116 prepareAxes(POSITION);
6117 addConfigurationProperty("touch.orientationAware", "1");
6118 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6119 clearViewports();
6120 prepareDisplay(DISPLAY_ORIENTATION_0);
6121 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6122 NotifyMotionArgs args;
6123
6124 // Orientation 270.
6125 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6126 processSync(mapper);
6127
6128 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6129 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6130 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6131
6132 processUp(mapper);
6133 processSync(mapper);
6134 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6135}
6136
6137TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6138 addConfigurationProperty("touch.deviceType", "touchScreen");
6139 prepareButtons();
6140 prepareAxes(POSITION);
6141 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6142 // orientation-aware are affected by display rotation.
6143 addConfigurationProperty("touch.orientationAware", "0");
6144 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6145 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6146
6147 NotifyMotionArgs args;
6148
6149 // Orientation 90, Rotation 0.
6150 clearViewports();
6151 prepareDisplay(DISPLAY_ORIENTATION_0);
6152 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6153 processSync(mapper);
6154
6155 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6156 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6157 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6158
6159 processUp(mapper);
6160 processSync(mapper);
6161 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6162
6163 // Orientation 90, Rotation 90.
6164 clearViewports();
6165 prepareDisplay(DISPLAY_ORIENTATION_90);
6166 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6167 processSync(mapper);
6168
6169 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6170 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6171 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6172
6173 processUp(mapper);
6174 processSync(mapper);
6175 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6176
6177 // Orientation 90, Rotation 180.
6178 clearViewports();
6179 prepareDisplay(DISPLAY_ORIENTATION_180);
6180 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6181 processSync(mapper);
6182
6183 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6184 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6185 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6186
6187 processUp(mapper);
6188 processSync(mapper);
6189 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6190
6191 // Orientation 90, Rotation 270.
6192 clearViewports();
6193 prepareDisplay(DISPLAY_ORIENTATION_270);
6194 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6195 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6196 processSync(mapper);
6197
6198 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6199 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6200 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6201
6202 processUp(mapper);
6203 processSync(mapper);
6204 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6205}
6206
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 addConfigurationProperty("touch.deviceType", "touchScreen");
6209 prepareDisplay(DISPLAY_ORIENTATION_0);
6210 prepareButtons();
6211 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006212 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213
6214 // These calculations are based on the input device calibration documentation.
6215 int32_t rawX = 100;
6216 int32_t rawY = 200;
6217 int32_t rawPressure = 10;
6218 int32_t rawToolMajor = 12;
6219 int32_t rawDistance = 2;
6220 int32_t rawTiltX = 30;
6221 int32_t rawTiltY = 110;
6222
6223 float x = toDisplayX(rawX);
6224 float y = toDisplayY(rawY);
6225 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6226 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6227 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6228 float distance = float(rawDistance);
6229
6230 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6231 float tiltScale = M_PI / 180;
6232 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6233 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6234 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6235 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6236
6237 processDown(mapper, rawX, rawY);
6238 processPressure(mapper, rawPressure);
6239 processToolMajor(mapper, rawToolMajor);
6240 processDistance(mapper, rawDistance);
6241 processTilt(mapper, rawTiltX, rawTiltY);
6242 processSync(mapper);
6243
6244 NotifyMotionArgs args;
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6247 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6248 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6249}
6250
Jason Gerecke489fda82012-09-07 17:19:40 -07006251TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006252 addConfigurationProperty("touch.deviceType", "touchScreen");
6253 prepareDisplay(DISPLAY_ORIENTATION_0);
6254 prepareLocationCalibration();
6255 prepareButtons();
6256 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006257 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006258
6259 int32_t rawX = 100;
6260 int32_t rawY = 200;
6261
6262 float x = toDisplayX(toCookedX(rawX, rawY));
6263 float y = toDisplayY(toCookedY(rawX, rawY));
6264
6265 processDown(mapper, rawX, rawY);
6266 processSync(mapper);
6267
6268 NotifyMotionArgs args;
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6271 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6272}
6273
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 addConfigurationProperty("touch.deviceType", "touchScreen");
6276 prepareDisplay(DISPLAY_ORIENTATION_0);
6277 prepareButtons();
6278 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006279 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280
6281 NotifyMotionArgs motionArgs;
6282 NotifyKeyArgs keyArgs;
6283
6284 processDown(mapper, 100, 200);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6288 ASSERT_EQ(0, motionArgs.buttonState);
6289
6290 // press BTN_LEFT, release BTN_LEFT
6291 processKey(mapper, BTN_LEFT, 1);
6292 processSync(mapper);
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6295 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6296
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6299 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6300
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 processKey(mapper, BTN_LEFT, 0);
6302 processSync(mapper);
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006309 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310
6311 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6312 processKey(mapper, BTN_RIGHT, 1);
6313 processKey(mapper, BTN_MIDDLE, 1);
6314 processSync(mapper);
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6317 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6318 motionArgs.buttonState);
6319
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6322 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6323
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6326 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6327 motionArgs.buttonState);
6328
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 processKey(mapper, BTN_RIGHT, 0);
6330 processSync(mapper);
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006334
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 processKey(mapper, BTN_MIDDLE, 0);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348
6349 // press BTN_BACK, release BTN_BACK
6350 processKey(mapper, BTN_BACK, 1);
6351 processSync(mapper);
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6353 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6354 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006355
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6359
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6362 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006363
6364 processKey(mapper, BTN_BACK, 0);
6365 processSync(mapper);
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006372 ASSERT_EQ(0, motionArgs.buttonState);
6373
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6375 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6376 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6377
6378 // press BTN_SIDE, release BTN_SIDE
6379 processKey(mapper, BTN_SIDE, 1);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6382 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6383 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6388
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6391 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392
6393 processKey(mapper, BTN_SIDE, 0);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006401 ASSERT_EQ(0, motionArgs.buttonState);
6402
Michael Wrightd02c5b62014-02-10 15:10:22 -08006403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6404 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6405 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6406
6407 // press BTN_FORWARD, release BTN_FORWARD
6408 processKey(mapper, BTN_FORWARD, 1);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6411 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6412 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006413
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6420 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421
6422 processKey(mapper, BTN_FORWARD, 0);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006425 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006430 ASSERT_EQ(0, motionArgs.buttonState);
6431
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6433 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6434 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6435
6436 // press BTN_EXTRA, release BTN_EXTRA
6437 processKey(mapper, BTN_EXTRA, 1);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6440 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6441 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006442
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006445 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6446
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6449 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450
6451 processKey(mapper, BTN_EXTRA, 0);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006454 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006459 ASSERT_EQ(0, motionArgs.buttonState);
6460
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6462 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6463 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6464
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6466
Michael Wrightd02c5b62014-02-10 15:10:22 -08006467 // press BTN_STYLUS, release BTN_STYLUS
6468 processKey(mapper, BTN_STYLUS, 1);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006472 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6473
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6476 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006477
6478 processKey(mapper, BTN_STYLUS, 0);
6479 processSync(mapper);
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006481 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006483
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006486 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006487
6488 // press BTN_STYLUS2, release BTN_STYLUS2
6489 processKey(mapper, BTN_STYLUS2, 1);
6490 processSync(mapper);
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006493 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6494
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6497 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498
6499 processKey(mapper, BTN_STYLUS2, 0);
6500 processSync(mapper);
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006502 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006503 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006504
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006507 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006508
6509 // release touch
6510 processUp(mapper);
6511 processSync(mapper);
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6513 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6514 ASSERT_EQ(0, motionArgs.buttonState);
6515}
6516
6517TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006518 addConfigurationProperty("touch.deviceType", "touchScreen");
6519 prepareDisplay(DISPLAY_ORIENTATION_0);
6520 prepareButtons();
6521 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006522 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006523
6524 NotifyMotionArgs motionArgs;
6525
6526 // default tool type is finger
6527 processDown(mapper, 100, 200);
6528 processSync(mapper);
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6530 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6532
6533 // eraser
6534 processKey(mapper, BTN_TOOL_RUBBER, 1);
6535 processSync(mapper);
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6539
6540 // stylus
6541 processKey(mapper, BTN_TOOL_RUBBER, 0);
6542 processKey(mapper, BTN_TOOL_PEN, 1);
6543 processSync(mapper);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6547
6548 // brush
6549 processKey(mapper, BTN_TOOL_PEN, 0);
6550 processKey(mapper, BTN_TOOL_BRUSH, 1);
6551 processSync(mapper);
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6555
6556 // pencil
6557 processKey(mapper, BTN_TOOL_BRUSH, 0);
6558 processKey(mapper, BTN_TOOL_PENCIL, 1);
6559 processSync(mapper);
6560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6563
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006564 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006565 processKey(mapper, BTN_TOOL_PENCIL, 0);
6566 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6567 processSync(mapper);
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6571
6572 // mouse
6573 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6574 processKey(mapper, BTN_TOOL_MOUSE, 1);
6575 processSync(mapper);
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6579
6580 // lens
6581 processKey(mapper, BTN_TOOL_MOUSE, 0);
6582 processKey(mapper, BTN_TOOL_LENS, 1);
6583 processSync(mapper);
6584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6587
6588 // double-tap
6589 processKey(mapper, BTN_TOOL_LENS, 0);
6590 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6591 processSync(mapper);
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6595
6596 // triple-tap
6597 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6598 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6599 processSync(mapper);
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6603
6604 // quad-tap
6605 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6606 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6607 processSync(mapper);
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6611
6612 // finger
6613 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6614 processKey(mapper, BTN_TOOL_FINGER, 1);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6619
6620 // stylus trumps finger
6621 processKey(mapper, BTN_TOOL_PEN, 1);
6622 processSync(mapper);
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6626
6627 // eraser trumps stylus
6628 processKey(mapper, BTN_TOOL_RUBBER, 1);
6629 processSync(mapper);
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6633
6634 // mouse trumps eraser
6635 processKey(mapper, BTN_TOOL_MOUSE, 1);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6640
6641 // back to default tool type
6642 processKey(mapper, BTN_TOOL_MOUSE, 0);
6643 processKey(mapper, BTN_TOOL_RUBBER, 0);
6644 processKey(mapper, BTN_TOOL_PEN, 0);
6645 processKey(mapper, BTN_TOOL_FINGER, 0);
6646 processSync(mapper);
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6650}
6651
6652TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006653 addConfigurationProperty("touch.deviceType", "touchScreen");
6654 prepareDisplay(DISPLAY_ORIENTATION_0);
6655 prepareButtons();
6656 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006657 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006658 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006659
6660 NotifyMotionArgs motionArgs;
6661
6662 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6663 processKey(mapper, BTN_TOOL_FINGER, 1);
6664 processMove(mapper, 100, 200);
6665 processSync(mapper);
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6669 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6670
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6672 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6674 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6675
6676 // move a little
6677 processMove(mapper, 150, 250);
6678 processSync(mapper);
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6682 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6683
6684 // down when BTN_TOUCH is pressed, pressure defaults to 1
6685 processKey(mapper, BTN_TOUCH, 1);
6686 processSync(mapper);
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6688 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6690 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6691
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6693 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6695 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6696
6697 // up when BTN_TOUCH is released, hover restored
6698 processKey(mapper, BTN_TOUCH, 0);
6699 processSync(mapper);
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6701 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6703 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6704
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6706 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6708 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6709
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6711 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6713 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6714
6715 // exit hover when pointer goes away
6716 processKey(mapper, BTN_TOOL_FINGER, 0);
6717 processSync(mapper);
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6721 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6722}
6723
6724TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006725 addConfigurationProperty("touch.deviceType", "touchScreen");
6726 prepareDisplay(DISPLAY_ORIENTATION_0);
6727 prepareButtons();
6728 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006729 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006730
6731 NotifyMotionArgs motionArgs;
6732
6733 // initially hovering because pressure is 0
6734 processDown(mapper, 100, 200);
6735 processPressure(mapper, 0);
6736 processSync(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6738 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6740 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6741
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6745 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6746
6747 // move a little
6748 processMove(mapper, 150, 250);
6749 processSync(mapper);
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6753 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6754
6755 // down when pressure is non-zero
6756 processPressure(mapper, RAW_PRESSURE_MAX);
6757 processSync(mapper);
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6761 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6762
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6766 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6767
6768 // up when pressure becomes 0, hover restored
6769 processPressure(mapper, 0);
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6772 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6774 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6775
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6777 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6779 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6780
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6784 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6785
6786 // exit hover when pointer goes away
6787 processUp(mapper);
6788 processSync(mapper);
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6790 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6792 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6793}
6794
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006795TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6796 addConfigurationProperty("touch.deviceType", "touchScreen");
6797 prepareDisplay(DISPLAY_ORIENTATION_0);
6798 prepareButtons();
6799 prepareAxes(POSITION | PRESSURE);
6800 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6801 NotifyMotionArgs motionArgs;
6802
6803 // Set the initial state for the touch pointer.
6804 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6805 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6806 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6807 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6808
6809 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
6810 // state by reading the current axis values.
6811 mapper.reset(ARBITRARY_TIME);
6812
6813 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6814 // the recreated touch state to generate a down event.
6815 processSync(mapper);
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6817 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6818
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6820}
6821
Prabir Pradhan588d6392022-11-02 20:05:13 +00006822TEST_F(SingleTouchInputMapperTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
6823 std::shared_ptr<FakePointerController> fakePointerController =
6824 std::make_shared<FakePointerController>();
6825 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
6826 fakePointerController->setPosition(100, 200);
6827 fakePointerController->setButtonState(0);
6828 mFakePolicy->setPointerController(fakePointerController);
6829
6830 addConfigurationProperty("touch.deviceType", "pointer");
6831 prepareDisplay(DISPLAY_ORIENTATION_0);
6832 prepareButtons();
6833 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6834 prepareAxes(POSITION);
6835 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6837
6838 // Start a stylus gesture.
6839 processKey(mapper, BTN_TOOL_PEN, 1);
6840 processDown(mapper, 100, 200);
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6843 AllOf(WithAction(AMOTION_EVENT_ACTION_DOWN),
6844 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
6845 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6846 // TODO(b/257078296): Pointer mode generates extra event.
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6848 AllOf(WithAction(AMOTION_EVENT_ACTION_MOVE),
6849 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
6850 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6852
6853 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
6854 // gesture should be disabled.
6855 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6856 viewport->isActive = false;
6857 mFakePolicy->updateViewport(*viewport);
6858 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6860 AllOf(WithAction(AMOTION_EVENT_ACTION_CANCEL),
6861 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
6862 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6863 // TODO(b/257078296): Pointer mode generates extra event.
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6865 AllOf(WithAction(AMOTION_EVENT_ACTION_CANCEL),
6866 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
6867 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6869}
6870
lilinnane74b35f2022-07-19 16:00:50 +08006871TEST_F(SingleTouchInputMapperTest,
6872 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6873 addConfigurationProperty("touch.deviceType", "touchScreen");
6874 prepareDisplay(DISPLAY_ORIENTATION_0);
6875 prepareButtons();
6876 prepareAxes(POSITION);
6877 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6878 NotifyMotionArgs motionArgs;
6879
6880 // Down.
6881 processDown(mapper, 100, 200);
6882 processSync(mapper);
6883
6884 // We should receive a down event
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6886 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6887
6888 // Change display id
6889 clearViewports();
6890 prepareSecondaryDisplay(ViewportType::INTERNAL);
6891
6892 // We should receive a cancel event
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6894 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6895 // Then receive reset called
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6897}
6898
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006899TEST_F(SingleTouchInputMapperTest,
6900 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6901 addConfigurationProperty("touch.deviceType", "touchScreen");
6902 prepareDisplay(DISPLAY_ORIENTATION_0);
6903 prepareButtons();
6904 prepareAxes(POSITION);
6905 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6907 NotifyMotionArgs motionArgs;
6908
6909 // Start a new gesture.
6910 processDown(mapper, 100, 200);
6911 processSync(mapper);
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6913 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6914
6915 // Make the viewport inactive. This will put the device in disabled mode.
6916 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6917 viewport->isActive = false;
6918 mFakePolicy->updateViewport(*viewport);
6919 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6920
6921 // We should receive a cancel event for the ongoing gesture.
6922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6923 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6924 // Then we should be notified that the device was reset.
6925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6926
6927 // No events are generated while the viewport is inactive.
6928 processMove(mapper, 101, 201);
6929 processSync(mapper);
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006930 processUp(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006931 processSync(mapper);
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6933
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006934 // Start a new gesture while the viewport is still inactive.
6935 processDown(mapper, 300, 400);
6936 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6937 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6938 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6939 processSync(mapper);
6940
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006941 // Make the viewport active again. The device should resume processing events.
6942 viewport->isActive = true;
6943 mFakePolicy->updateViewport(*viewport);
6944 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6945
6946 // The device is reset because it changes back to direct mode, without generating any events.
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6949
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00006950 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanf670dad2022-08-05 22:32:11 +00006951 processSync(mapper);
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6953 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6954
6955 // No more events.
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6958}
6959
Prabir Pradhan5632d622021-09-06 07:57:20 -07006960// --- TouchDisplayProjectionTest ---
6961
6962class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6963public:
6964 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6965 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6966 // rotated equivalent of the given un-rotated physical display bounds.
6967 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6968 uint32_t inverseRotationFlags;
6969 auto width = DISPLAY_WIDTH;
6970 auto height = DISPLAY_HEIGHT;
6971 switch (orientation) {
6972 case DISPLAY_ORIENTATION_90:
6973 inverseRotationFlags = ui::Transform::ROT_270;
6974 std::swap(width, height);
6975 break;
6976 case DISPLAY_ORIENTATION_180:
6977 inverseRotationFlags = ui::Transform::ROT_180;
6978 break;
6979 case DISPLAY_ORIENTATION_270:
6980 inverseRotationFlags = ui::Transform::ROT_90;
6981 std::swap(width, height);
6982 break;
6983 case DISPLAY_ORIENTATION_0:
6984 inverseRotationFlags = ui::Transform::ROT_0;
6985 break;
6986 default:
6987 FAIL() << "Invalid orientation: " << orientation;
6988 }
6989
6990 const ui::Transform rotation(inverseRotationFlags, width, height);
6991 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6992
6993 std::optional<DisplayViewport> internalViewport =
6994 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6995 DisplayViewport& v = *internalViewport;
6996 v.displayId = DISPLAY_ID;
6997 v.orientation = orientation;
6998
6999 v.logicalLeft = 0;
7000 v.logicalTop = 0;
7001 v.logicalRight = 100;
7002 v.logicalBottom = 100;
7003
7004 v.physicalLeft = rotatedPhysicalDisplay.left;
7005 v.physicalTop = rotatedPhysicalDisplay.top;
7006 v.physicalRight = rotatedPhysicalDisplay.right;
7007 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7008
7009 v.deviceWidth = width;
7010 v.deviceHeight = height;
7011
7012 v.isActive = true;
7013 v.uniqueId = UNIQUE_ID;
7014 v.type = ViewportType::INTERNAL;
7015 mFakePolicy->updateViewport(v);
7016 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7017 }
7018
7019 void assertReceivedMove(const Point& point) {
7020 NotifyMotionArgs motionArgs;
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7023 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7025 1, 0, 0, 0, 0, 0, 0, 0));
7026 }
7027};
7028
7029TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7030 addConfigurationProperty("touch.deviceType", "touchScreen");
7031 prepareDisplay(DISPLAY_ORIENTATION_0);
7032
7033 prepareButtons();
7034 prepareAxes(POSITION);
7035 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7036
7037 NotifyMotionArgs motionArgs;
7038
7039 // Configure the DisplayViewport such that the logical display maps to a subsection of
7040 // the display panel called the physical display. Here, the physical display is bounded by the
7041 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7042 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7043 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7044 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7045
7046 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7047 DISPLAY_ORIENTATION_270}) {
7048 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7049
7050 // Touches outside the physical display should be ignored, and should not generate any
7051 // events. Ensure touches at the following points that lie outside of the physical display
7052 // area do not generate any events.
7053 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7054 processDown(mapper, toRawX(point.x), toRawY(point.y));
7055 processSync(mapper);
7056 processUp(mapper);
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7059 << "Unexpected event generated for touch outside physical display at point: "
7060 << point.x << ", " << point.y;
7061 }
7062 }
7063}
7064
7065TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7066 addConfigurationProperty("touch.deviceType", "touchScreen");
7067 prepareDisplay(DISPLAY_ORIENTATION_0);
7068
7069 prepareButtons();
7070 prepareAxes(POSITION);
7071 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7072
7073 NotifyMotionArgs motionArgs;
7074
7075 // Configure the DisplayViewport such that the logical display maps to a subsection of
7076 // the display panel called the physical display. Here, the physical display is bounded by the
7077 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7078 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7079
7080 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7081 DISPLAY_ORIENTATION_270}) {
7082 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7083
7084 // Touches that start outside the physical display should be ignored until it enters the
7085 // physical display bounds, at which point it should generate a down event. Start a touch at
7086 // the point (5, 100), which is outside the physical display bounds.
7087 static const Point kOutsidePoint{5, 100};
7088 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7089 processSync(mapper);
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7091
7092 // Move the touch into the physical display area. This should generate a pointer down.
7093 processMove(mapper, toRawX(11), toRawY(21));
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7097 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7098 ASSERT_NO_FATAL_FAILURE(
7099 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7100
7101 // Move the touch inside the physical display area. This should generate a pointer move.
7102 processMove(mapper, toRawX(69), toRawY(159));
7103 processSync(mapper);
7104 assertReceivedMove({69, 159});
7105
7106 // Move outside the physical display area. Since the pointer is already down, this should
7107 // now continue generating events.
7108 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7109 processSync(mapper);
7110 assertReceivedMove(kOutsidePoint);
7111
7112 // Release. This should generate a pointer up.
7113 processUp(mapper);
7114 processSync(mapper);
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7116 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7118 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7119
7120 // Ensure no more events were generated.
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7123 }
7124}
7125
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126// --- MultiTouchInputMapperTest ---
7127
7128class MultiTouchInputMapperTest : public TouchInputMapperTest {
7129protected:
7130 void prepareAxes(int axes);
7131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007132 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7133 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7134 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7135 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7136 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7137 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7138 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7139 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7140 void processId(MultiTouchInputMapper& mapper, int32_t id);
7141 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7142 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7143 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7144 void processMTSync(MultiTouchInputMapper& mapper);
7145 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146};
7147
7148void MultiTouchInputMapperTest::prepareAxes(int axes) {
7149 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007150 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152 }
7153 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007154 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7155 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007157 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7158 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159 }
7160 }
7161 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007162 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7163 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007164 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007165 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007166 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167 }
7168 }
7169 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007170 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7171 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172 }
7173 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007174 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7175 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176 }
7177 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007178 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7179 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180 }
7181 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007182 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7183 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184 }
7185 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007186 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7187 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188 }
7189 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007190 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007191 }
7192}
7193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007194void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7195 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198}
7199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007200void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7201 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203}
7204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007205void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7206 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208}
7209
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007210void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007212}
7213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007214void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216}
7217
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007218void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7219 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007221}
7222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007223void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007225}
7226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007227void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229}
7230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007231void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233}
7234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007235void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237}
7238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007239void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241}
7242
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007243void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7244 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246}
7247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007248void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007250}
7251
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007252void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007254}
7255
Michael Wrightd02c5b62014-02-10 15:10:22 -08007256TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007257 addConfigurationProperty("touch.deviceType", "touchScreen");
7258 prepareDisplay(DISPLAY_ORIENTATION_0);
7259 prepareAxes(POSITION);
7260 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007261 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007262
arthurhungdcef2dc2020-08-11 14:47:50 +08007263 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007264
7265 NotifyMotionArgs motionArgs;
7266
7267 // Two fingers down at once.
7268 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7269 processPosition(mapper, x1, y1);
7270 processMTSync(mapper);
7271 processPosition(mapper, x2, y2);
7272 processMTSync(mapper);
7273 processSync(mapper);
7274
7275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7277 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7278 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7279 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7280 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7281 ASSERT_EQ(0, motionArgs.flags);
7282 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7283 ASSERT_EQ(0, motionArgs.buttonState);
7284 ASSERT_EQ(0, motionArgs.edgeFlags);
7285 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7286 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7289 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7290 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7291 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7293
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7295 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7296 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7297 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7298 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007299 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007300 ASSERT_EQ(0, motionArgs.flags);
7301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7302 ASSERT_EQ(0, motionArgs.buttonState);
7303 ASSERT_EQ(0, motionArgs.edgeFlags);
7304 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7305 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7307 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7310 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7312 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7313 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7314 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7315 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7316
7317 // Move.
7318 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7319 processPosition(mapper, x1, y1);
7320 processMTSync(mapper);
7321 processPosition(mapper, x2, y2);
7322 processMTSync(mapper);
7323 processSync(mapper);
7324
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7327 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7328 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7329 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7331 ASSERT_EQ(0, motionArgs.flags);
7332 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7333 ASSERT_EQ(0, motionArgs.buttonState);
7334 ASSERT_EQ(0, motionArgs.edgeFlags);
7335 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7336 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7338 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7341 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7343 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7344 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7345 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7347
7348 // First finger up.
7349 x2 += 15; y2 -= 20;
7350 processPosition(mapper, x2, y2);
7351 processMTSync(mapper);
7352 processSync(mapper);
7353
7354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7355 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7356 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7357 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7358 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007359 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360 ASSERT_EQ(0, motionArgs.flags);
7361 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7362 ASSERT_EQ(0, motionArgs.buttonState);
7363 ASSERT_EQ(0, motionArgs.edgeFlags);
7364 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7365 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7367 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7370 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7372 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7373 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7374 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7376
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7379 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7380 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7381 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7383 ASSERT_EQ(0, motionArgs.flags);
7384 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7385 ASSERT_EQ(0, motionArgs.buttonState);
7386 ASSERT_EQ(0, motionArgs.edgeFlags);
7387 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7388 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7391 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7392 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7393 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7395
7396 // Move.
7397 x2 += 20; y2 -= 25;
7398 processPosition(mapper, x2, y2);
7399 processMTSync(mapper);
7400 processSync(mapper);
7401
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7404 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7405 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7406 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7408 ASSERT_EQ(0, motionArgs.flags);
7409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7410 ASSERT_EQ(0, motionArgs.buttonState);
7411 ASSERT_EQ(0, motionArgs.edgeFlags);
7412 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7413 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7416 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7417 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7418 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7420
7421 // New finger down.
7422 int32_t x3 = 700, y3 = 300;
7423 processPosition(mapper, x2, y2);
7424 processMTSync(mapper);
7425 processPosition(mapper, x3, y3);
7426 processMTSync(mapper);
7427 processSync(mapper);
7428
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7430 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7431 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7432 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7433 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007434 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007435 ASSERT_EQ(0, motionArgs.flags);
7436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7437 ASSERT_EQ(0, motionArgs.buttonState);
7438 ASSERT_EQ(0, motionArgs.edgeFlags);
7439 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7442 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7445 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7447 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7448 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7449 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7451
7452 // Second finger up.
7453 x3 += 30; y3 -= 20;
7454 processPosition(mapper, x3, y3);
7455 processMTSync(mapper);
7456 processSync(mapper);
7457
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7460 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7461 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7462 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007463 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007464 ASSERT_EQ(0, motionArgs.flags);
7465 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7466 ASSERT_EQ(0, motionArgs.buttonState);
7467 ASSERT_EQ(0, motionArgs.edgeFlags);
7468 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7469 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7471 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7474 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7476 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7477 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7478 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7480
7481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7483 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7484 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7485 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7487 ASSERT_EQ(0, motionArgs.flags);
7488 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7489 ASSERT_EQ(0, motionArgs.buttonState);
7490 ASSERT_EQ(0, motionArgs.edgeFlags);
7491 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7492 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7495 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7496 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7497 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7498 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7499
7500 // Last finger up.
7501 processMTSync(mapper);
7502 processSync(mapper);
7503
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7505 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7506 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7507 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7508 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7509 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7510 ASSERT_EQ(0, motionArgs.flags);
7511 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7512 ASSERT_EQ(0, motionArgs.buttonState);
7513 ASSERT_EQ(0, motionArgs.edgeFlags);
7514 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7515 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7518 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7519 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7520 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7522
7523 // Should not have sent any more keys or motions.
7524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7526}
7527
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007528TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7529 addConfigurationProperty("touch.deviceType", "touchScreen");
7530 prepareDisplay(DISPLAY_ORIENTATION_0);
7531
7532 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7533 /*fuzz*/ 0, /*resolution*/ 10);
7534 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7535 /*fuzz*/ 0, /*resolution*/ 11);
7536 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7537 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7538 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7539 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7540 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7541 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7542 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7543 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7544
7545 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7546
7547 // X and Y axes
7548 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7549 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7550 // Touch major and minor
7551 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7552 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7553 // Tool major and minor
7554 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7555 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7556}
7557
7558TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7559 addConfigurationProperty("touch.deviceType", "touchScreen");
7560 prepareDisplay(DISPLAY_ORIENTATION_0);
7561
7562 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7563 /*fuzz*/ 0, /*resolution*/ 10);
7564 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7565 /*fuzz*/ 0, /*resolution*/ 11);
7566
7567 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7568
7569 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7570
7571 // Touch major and minor
7572 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7573 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7574 // Tool major and minor
7575 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7576 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7577}
7578
Michael Wrightd02c5b62014-02-10 15:10:22 -08007579TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007580 addConfigurationProperty("touch.deviceType", "touchScreen");
7581 prepareDisplay(DISPLAY_ORIENTATION_0);
7582 prepareAxes(POSITION | ID);
7583 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007584 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007585
arthurhungdcef2dc2020-08-11 14:47:50 +08007586 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587
7588 NotifyMotionArgs motionArgs;
7589
7590 // Two fingers down at once.
7591 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7592 processPosition(mapper, x1, y1);
7593 processId(mapper, 1);
7594 processMTSync(mapper);
7595 processPosition(mapper, x2, y2);
7596 processId(mapper, 2);
7597 processMTSync(mapper);
7598 processSync(mapper);
7599
7600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7601 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7602 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7603 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7606 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7607
7608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007609 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007610 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7611 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7613 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7616 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7618 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7619
7620 // Move.
7621 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7622 processPosition(mapper, x1, y1);
7623 processId(mapper, 1);
7624 processMTSync(mapper);
7625 processPosition(mapper, x2, y2);
7626 processId(mapper, 2);
7627 processMTSync(mapper);
7628 processSync(mapper);
7629
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7632 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7635 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7638 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7640 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7641
7642 // First finger up.
7643 x2 += 15; y2 -= 20;
7644 processPosition(mapper, x2, y2);
7645 processId(mapper, 2);
7646 processMTSync(mapper);
7647 processSync(mapper);
7648
7649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007650 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7654 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7657 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7659 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7660
7661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7663 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7664 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7667 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7668
7669 // Move.
7670 x2 += 20; y2 -= 25;
7671 processPosition(mapper, x2, y2);
7672 processId(mapper, 2);
7673 processMTSync(mapper);
7674 processSync(mapper);
7675
7676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7678 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7679 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7682 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7683
7684 // New finger down.
7685 int32_t x3 = 700, y3 = 300;
7686 processPosition(mapper, x2, y2);
7687 processId(mapper, 2);
7688 processMTSync(mapper);
7689 processPosition(mapper, x3, y3);
7690 processId(mapper, 3);
7691 processMTSync(mapper);
7692 processSync(mapper);
7693
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007695 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007696 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7697 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7699 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7702 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7704 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7705
7706 // Second finger up.
7707 x3 += 30; y3 -= 20;
7708 processPosition(mapper, x3, y3);
7709 processId(mapper, 3);
7710 processMTSync(mapper);
7711 processSync(mapper);
7712
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007714 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7716 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7718 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7721 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7723 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7724
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7727 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7728 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7731 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7732
7733 // Last finger up.
7734 processMTSync(mapper);
7735 processSync(mapper);
7736
7737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7738 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7739 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7740 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7743 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7744
7745 // Should not have sent any more keys or motions.
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7748}
7749
7750TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007751 addConfigurationProperty("touch.deviceType", "touchScreen");
7752 prepareDisplay(DISPLAY_ORIENTATION_0);
7753 prepareAxes(POSITION | ID | SLOT);
7754 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007755 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007756
arthurhungdcef2dc2020-08-11 14:47:50 +08007757 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007758
7759 NotifyMotionArgs motionArgs;
7760
7761 // Two fingers down at once.
7762 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7763 processPosition(mapper, x1, y1);
7764 processId(mapper, 1);
7765 processSlot(mapper, 1);
7766 processPosition(mapper, x2, y2);
7767 processId(mapper, 2);
7768 processSync(mapper);
7769
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7771 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7772 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7776 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7777
7778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007779 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007780 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7781 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7783 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7786 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7788 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7789
7790 // Move.
7791 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7792 processSlot(mapper, 0);
7793 processPosition(mapper, x1, y1);
7794 processSlot(mapper, 1);
7795 processPosition(mapper, x2, y2);
7796 processSync(mapper);
7797
7798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7800 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7801 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7803 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7804 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7806 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7808 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7809
7810 // First finger up.
7811 x2 += 15; y2 -= 20;
7812 processSlot(mapper, 0);
7813 processId(mapper, -1);
7814 processSlot(mapper, 1);
7815 processPosition(mapper, x2, y2);
7816 processSync(mapper);
7817
7818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007819 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007820 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7821 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7823 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7826 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7828 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7829
7830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7832 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7833 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7836 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7837
7838 // Move.
7839 x2 += 20; y2 -= 25;
7840 processPosition(mapper, x2, y2);
7841 processSync(mapper);
7842
7843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7845 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7846 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7849 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7850
7851 // New finger down.
7852 int32_t x3 = 700, y3 = 300;
7853 processPosition(mapper, x2, y2);
7854 processSlot(mapper, 0);
7855 processId(mapper, 3);
7856 processPosition(mapper, x3, y3);
7857 processSync(mapper);
7858
7859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007860 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007861 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7862 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7863 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7864 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7867 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7869 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7870
7871 // Second finger up.
7872 x3 += 30; y3 -= 20;
7873 processSlot(mapper, 1);
7874 processId(mapper, -1);
7875 processSlot(mapper, 0);
7876 processPosition(mapper, x3, y3);
7877 processSync(mapper);
7878
7879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007880 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007881 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7882 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7884 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7885 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7887 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7889 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7890
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7893 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7894 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7897 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7898
7899 // Last finger up.
7900 processId(mapper, -1);
7901 processSync(mapper);
7902
7903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7904 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7905 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7906 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7909 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7910
7911 // Should not have sent any more keys or motions.
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7914}
7915
7916TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007917 addConfigurationProperty("touch.deviceType", "touchScreen");
7918 prepareDisplay(DISPLAY_ORIENTATION_0);
7919 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921
7922 // These calculations are based on the input device calibration documentation.
7923 int32_t rawX = 100;
7924 int32_t rawY = 200;
7925 int32_t rawTouchMajor = 7;
7926 int32_t rawTouchMinor = 6;
7927 int32_t rawToolMajor = 9;
7928 int32_t rawToolMinor = 8;
7929 int32_t rawPressure = 11;
7930 int32_t rawDistance = 0;
7931 int32_t rawOrientation = 3;
7932 int32_t id = 5;
7933
7934 float x = toDisplayX(rawX);
7935 float y = toDisplayY(rawY);
7936 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7937 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7938 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7939 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7940 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7941 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7942 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7943 float distance = float(rawDistance);
7944
7945 processPosition(mapper, rawX, rawY);
7946 processTouchMajor(mapper, rawTouchMajor);
7947 processTouchMinor(mapper, rawTouchMinor);
7948 processToolMajor(mapper, rawToolMajor);
7949 processToolMinor(mapper, rawToolMinor);
7950 processPressure(mapper, rawPressure);
7951 processOrientation(mapper, rawOrientation);
7952 processDistance(mapper, rawDistance);
7953 processId(mapper, id);
7954 processMTSync(mapper);
7955 processSync(mapper);
7956
7957 NotifyMotionArgs args;
7958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7959 ASSERT_EQ(0, args.pointerProperties[0].id);
7960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7961 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7962 orientation, distance));
7963}
7964
7965TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966 addConfigurationProperty("touch.deviceType", "touchScreen");
7967 prepareDisplay(DISPLAY_ORIENTATION_0);
7968 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7969 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007970 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971
7972 // These calculations are based on the input device calibration documentation.
7973 int32_t rawX = 100;
7974 int32_t rawY = 200;
7975 int32_t rawTouchMajor = 140;
7976 int32_t rawTouchMinor = 120;
7977 int32_t rawToolMajor = 180;
7978 int32_t rawToolMinor = 160;
7979
7980 float x = toDisplayX(rawX);
7981 float y = toDisplayY(rawY);
7982 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7983 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7984 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7985 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7986 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7987
7988 processPosition(mapper, rawX, rawY);
7989 processTouchMajor(mapper, rawTouchMajor);
7990 processTouchMinor(mapper, rawTouchMinor);
7991 processToolMajor(mapper, rawToolMajor);
7992 processToolMinor(mapper, rawToolMinor);
7993 processMTSync(mapper);
7994 processSync(mapper);
7995
7996 NotifyMotionArgs args;
7997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7999 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8000}
8001
8002TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008003 addConfigurationProperty("touch.deviceType", "touchScreen");
8004 prepareDisplay(DISPLAY_ORIENTATION_0);
8005 prepareAxes(POSITION | TOUCH | TOOL);
8006 addConfigurationProperty("touch.size.calibration", "diameter");
8007 addConfigurationProperty("touch.size.scale", "10");
8008 addConfigurationProperty("touch.size.bias", "160");
8009 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008010 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011
8012 // These calculations are based on the input device calibration documentation.
8013 // Note: We only provide a single common touch/tool value because the device is assumed
8014 // not to emit separate values for each pointer (isSummed = 1).
8015 int32_t rawX = 100;
8016 int32_t rawY = 200;
8017 int32_t rawX2 = 150;
8018 int32_t rawY2 = 250;
8019 int32_t rawTouchMajor = 5;
8020 int32_t rawToolMajor = 8;
8021
8022 float x = toDisplayX(rawX);
8023 float y = toDisplayY(rawY);
8024 float x2 = toDisplayX(rawX2);
8025 float y2 = toDisplayY(rawY2);
8026 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8027 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8028 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8029
8030 processPosition(mapper, rawX, rawY);
8031 processTouchMajor(mapper, rawTouchMajor);
8032 processToolMajor(mapper, rawToolMajor);
8033 processMTSync(mapper);
8034 processPosition(mapper, rawX2, rawY2);
8035 processTouchMajor(mapper, rawTouchMajor);
8036 processToolMajor(mapper, rawToolMajor);
8037 processMTSync(mapper);
8038 processSync(mapper);
8039
8040 NotifyMotionArgs args;
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8042 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8043
8044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008045 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046 ASSERT_EQ(size_t(2), args.pointerCount);
8047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8048 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8050 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8051}
8052
8053TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 addConfigurationProperty("touch.deviceType", "touchScreen");
8055 prepareDisplay(DISPLAY_ORIENTATION_0);
8056 prepareAxes(POSITION | TOUCH | TOOL);
8057 addConfigurationProperty("touch.size.calibration", "area");
8058 addConfigurationProperty("touch.size.scale", "43");
8059 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008060 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061
8062 // These calculations are based on the input device calibration documentation.
8063 int32_t rawX = 100;
8064 int32_t rawY = 200;
8065 int32_t rawTouchMajor = 5;
8066 int32_t rawToolMajor = 8;
8067
8068 float x = toDisplayX(rawX);
8069 float y = toDisplayY(rawY);
8070 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8071 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8072 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8073
8074 processPosition(mapper, rawX, rawY);
8075 processTouchMajor(mapper, rawTouchMajor);
8076 processToolMajor(mapper, rawToolMajor);
8077 processMTSync(mapper);
8078 processSync(mapper);
8079
8080 NotifyMotionArgs args;
8081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8083 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8084}
8085
8086TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008087 addConfigurationProperty("touch.deviceType", "touchScreen");
8088 prepareDisplay(DISPLAY_ORIENTATION_0);
8089 prepareAxes(POSITION | PRESSURE);
8090 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8091 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008092 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093
Michael Wrightaa449c92017-12-13 21:21:43 +00008094 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008095 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008096 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8097 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8098 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8099
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 // These calculations are based on the input device calibration documentation.
8101 int32_t rawX = 100;
8102 int32_t rawY = 200;
8103 int32_t rawPressure = 60;
8104
8105 float x = toDisplayX(rawX);
8106 float y = toDisplayY(rawY);
8107 float pressure = float(rawPressure) * 0.01f;
8108
8109 processPosition(mapper, rawX, rawY);
8110 processPressure(mapper, rawPressure);
8111 processMTSync(mapper);
8112 processSync(mapper);
8113
8114 NotifyMotionArgs args;
8115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8117 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8118}
8119
8120TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 addConfigurationProperty("touch.deviceType", "touchScreen");
8122 prepareDisplay(DISPLAY_ORIENTATION_0);
8123 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008124 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125
8126 NotifyMotionArgs motionArgs;
8127 NotifyKeyArgs keyArgs;
8128
8129 processId(mapper, 1);
8130 processPosition(mapper, 100, 200);
8131 processSync(mapper);
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8134 ASSERT_EQ(0, motionArgs.buttonState);
8135
8136 // press BTN_LEFT, release BTN_LEFT
8137 processKey(mapper, BTN_LEFT, 1);
8138 processSync(mapper);
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8141 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8142
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8144 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8145 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8146
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147 processKey(mapper, BTN_LEFT, 0);
8148 processSync(mapper);
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008152
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156
8157 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8158 processKey(mapper, BTN_RIGHT, 1);
8159 processKey(mapper, BTN_MIDDLE, 1);
8160 processSync(mapper);
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8163 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8164 motionArgs.buttonState);
8165
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8167 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8168 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8169
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8171 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8172 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8173 motionArgs.buttonState);
8174
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 processKey(mapper, BTN_RIGHT, 0);
8176 processSync(mapper);
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008180
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008183 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184
8185 processKey(mapper, BTN_MIDDLE, 0);
8186 processSync(mapper);
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008190
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008193 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008194
8195 // press BTN_BACK, release BTN_BACK
8196 processKey(mapper, BTN_BACK, 1);
8197 processSync(mapper);
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8199 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8200 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008201
Michael Wrightd02c5b62014-02-10 15:10:22 -08008202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008204 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8205
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8208 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209
8210 processKey(mapper, BTN_BACK, 0);
8211 processSync(mapper);
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008215
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218 ASSERT_EQ(0, motionArgs.buttonState);
8219
Michael Wrightd02c5b62014-02-10 15:10:22 -08008220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8221 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8222 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8223
8224 // press BTN_SIDE, release BTN_SIDE
8225 processKey(mapper, BTN_SIDE, 1);
8226 processSync(mapper);
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8228 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8229 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008230
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008233 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8234
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8237 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238
8239 processKey(mapper, BTN_SIDE, 0);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008244
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247 ASSERT_EQ(0, motionArgs.buttonState);
8248
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8250 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8251 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8252
8253 // press BTN_FORWARD, release BTN_FORWARD
8254 processKey(mapper, BTN_FORWARD, 1);
8255 processSync(mapper);
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8257 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8258 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008259
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008262 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8263
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8266 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267
8268 processKey(mapper, BTN_FORWARD, 0);
8269 processSync(mapper);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008273
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008276 ASSERT_EQ(0, motionArgs.buttonState);
8277
Michael Wrightd02c5b62014-02-10 15:10:22 -08008278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8280 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8281
8282 // press BTN_EXTRA, release BTN_EXTRA
8283 processKey(mapper, BTN_EXTRA, 1);
8284 processSync(mapper);
8285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8286 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8287 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008288
Michael Wrightd02c5b62014-02-10 15:10:22 -08008289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008291 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8292
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8295 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008296
8297 processKey(mapper, BTN_EXTRA, 0);
8298 processSync(mapper);
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008301 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008302
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008305 ASSERT_EQ(0, motionArgs.buttonState);
8306
Michael Wrightd02c5b62014-02-10 15:10:22 -08008307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8308 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8309 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8310
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8312
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313 // press BTN_STYLUS, release BTN_STYLUS
8314 processKey(mapper, BTN_STYLUS, 1);
8315 processSync(mapper);
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008318 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8319
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8322 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323
8324 processKey(mapper, BTN_STYLUS, 0);
8325 processSync(mapper);
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008328 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008329
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008332 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008333
8334 // press BTN_STYLUS2, release BTN_STYLUS2
8335 processKey(mapper, BTN_STYLUS2, 1);
8336 processSync(mapper);
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008339 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8340
8341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8343 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344
8345 processKey(mapper, BTN_STYLUS2, 0);
8346 processSync(mapper);
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008349 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008350
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008353 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008354
8355 // release touch
8356 processId(mapper, -1);
8357 processSync(mapper);
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8359 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8360 ASSERT_EQ(0, motionArgs.buttonState);
8361}
8362
8363TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008364 addConfigurationProperty("touch.deviceType", "touchScreen");
8365 prepareDisplay(DISPLAY_ORIENTATION_0);
8366 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008367 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008368
8369 NotifyMotionArgs motionArgs;
8370
8371 // default tool type is finger
8372 processId(mapper, 1);
8373 processPosition(mapper, 100, 200);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8378
8379 // eraser
8380 processKey(mapper, BTN_TOOL_RUBBER, 1);
8381 processSync(mapper);
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8385
8386 // stylus
8387 processKey(mapper, BTN_TOOL_RUBBER, 0);
8388 processKey(mapper, BTN_TOOL_PEN, 1);
8389 processSync(mapper);
8390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8393
8394 // brush
8395 processKey(mapper, BTN_TOOL_PEN, 0);
8396 processKey(mapper, BTN_TOOL_BRUSH, 1);
8397 processSync(mapper);
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8401
8402 // pencil
8403 processKey(mapper, BTN_TOOL_BRUSH, 0);
8404 processKey(mapper, BTN_TOOL_PENCIL, 1);
8405 processSync(mapper);
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8409
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008410 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008411 processKey(mapper, BTN_TOOL_PENCIL, 0);
8412 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8413 processSync(mapper);
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8417
8418 // mouse
8419 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8420 processKey(mapper, BTN_TOOL_MOUSE, 1);
8421 processSync(mapper);
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8425
8426 // lens
8427 processKey(mapper, BTN_TOOL_MOUSE, 0);
8428 processKey(mapper, BTN_TOOL_LENS, 1);
8429 processSync(mapper);
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8433
8434 // double-tap
8435 processKey(mapper, BTN_TOOL_LENS, 0);
8436 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8441
8442 // triple-tap
8443 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8444 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8445 processSync(mapper);
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8449
8450 // quad-tap
8451 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8452 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8453 processSync(mapper);
8454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8457
8458 // finger
8459 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8460 processKey(mapper, BTN_TOOL_FINGER, 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8465
8466 // stylus trumps finger
8467 processKey(mapper, BTN_TOOL_PEN, 1);
8468 processSync(mapper);
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8472
8473 // eraser trumps stylus
8474 processKey(mapper, BTN_TOOL_RUBBER, 1);
8475 processSync(mapper);
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8479
8480 // mouse trumps eraser
8481 processKey(mapper, BTN_TOOL_MOUSE, 1);
8482 processSync(mapper);
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8486
8487 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8488 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8489 processSync(mapper);
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8493
8494 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8495 processToolType(mapper, MT_TOOL_PEN);
8496 processSync(mapper);
8497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8500
8501 // back to default tool type
8502 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8503 processKey(mapper, BTN_TOOL_MOUSE, 0);
8504 processKey(mapper, BTN_TOOL_RUBBER, 0);
8505 processKey(mapper, BTN_TOOL_PEN, 0);
8506 processKey(mapper, BTN_TOOL_FINGER, 0);
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8511}
8512
8513TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514 addConfigurationProperty("touch.deviceType", "touchScreen");
8515 prepareDisplay(DISPLAY_ORIENTATION_0);
8516 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008517 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008518 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008519
8520 NotifyMotionArgs motionArgs;
8521
8522 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8523 processId(mapper, 1);
8524 processPosition(mapper, 100, 200);
8525 processSync(mapper);
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8529 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8530
8531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8534 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8535
8536 // move a little
8537 processPosition(mapper, 150, 250);
8538 processSync(mapper);
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8540 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8542 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8543
8544 // down when BTN_TOUCH is pressed, pressure defaults to 1
8545 processKey(mapper, BTN_TOUCH, 1);
8546 processSync(mapper);
8547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8548 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8550 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8551
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8553 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8555 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8556
8557 // up when BTN_TOUCH is released, hover restored
8558 processKey(mapper, BTN_TOUCH, 0);
8559 processSync(mapper);
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8563 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8564
8565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8566 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8568 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8569
8570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8571 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8573 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8574
8575 // exit hover when pointer goes away
8576 processId(mapper, -1);
8577 processSync(mapper);
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8581 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8582}
8583
8584TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008585 addConfigurationProperty("touch.deviceType", "touchScreen");
8586 prepareDisplay(DISPLAY_ORIENTATION_0);
8587 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008588 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008589
8590 NotifyMotionArgs motionArgs;
8591
8592 // initially hovering because pressure is 0
8593 processId(mapper, 1);
8594 processPosition(mapper, 100, 200);
8595 processPressure(mapper, 0);
8596 processSync(mapper);
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8600 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8601
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8605 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8606
8607 // move a little
8608 processPosition(mapper, 150, 250);
8609 processSync(mapper);
8610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8613 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8614
8615 // down when pressure becomes non-zero
8616 processPressure(mapper, RAW_PRESSURE_MAX);
8617 processSync(mapper);
8618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8619 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8621 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8622
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8624 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8626 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8627
8628 // up when pressure becomes 0, hover restored
8629 processPressure(mapper, 0);
8630 processSync(mapper);
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8632 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8634 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8635
8636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8637 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8639 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8640
8641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8644 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8645
8646 // exit hover when pointer goes away
8647 processId(mapper, -1);
8648 processSync(mapper);
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8650 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8652 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8653}
8654
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008655/**
8656 * Set the input device port <--> display port associations, and check that the
8657 * events are routed to the display that matches the display port.
8658 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8659 */
8660TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008661 const std::string usb2 = "USB2";
8662 const uint8_t hdmi1 = 0;
8663 const uint8_t hdmi2 = 1;
8664 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008665 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008666
8667 addConfigurationProperty("touch.deviceType", "touchScreen");
8668 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008669 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008670
8671 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8672 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8673
8674 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8675 // for this input device is specified, and the matching viewport is not present,
8676 // the input device should be disabled (at the mapper level).
8677
8678 // Add viewport for display 2 on hdmi2
8679 prepareSecondaryDisplay(type, hdmi2);
8680 // Send a touch event
8681 processPosition(mapper, 100, 100);
8682 processSync(mapper);
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8684
8685 // Add viewport for display 1 on hdmi1
8686 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8687 // Send a touch event again
8688 processPosition(mapper, 100, 100);
8689 processSync(mapper);
8690
8691 NotifyMotionArgs args;
8692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8693 ASSERT_EQ(DISPLAY_ID, args.displayId);
8694}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008695
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008696TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8697 addConfigurationProperty("touch.deviceType", "touchScreen");
8698 prepareAxes(POSITION);
8699 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8700
8701 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8702
8703 prepareDisplay(DISPLAY_ORIENTATION_0);
8704 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8705
8706 // Send a touch event
8707 processPosition(mapper, 100, 100);
8708 processSync(mapper);
8709
8710 NotifyMotionArgs args;
8711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8712 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8713}
8714
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008715TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008716 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008717 std::shared_ptr<FakePointerController> fakePointerController =
8718 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008719 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008720 fakePointerController->setPosition(100, 200);
8721 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008722 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008723
Garfield Tan888a6a42020-01-09 11:39:16 -08008724 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008725 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008726
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008727 prepareDisplay(DISPLAY_ORIENTATION_0);
8728 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008729 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008730
8731 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008732 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008733
8734 NotifyMotionArgs motionArgs;
8735 processPosition(mapper, 100, 100);
8736 processSync(mapper);
8737
8738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8740 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8741}
8742
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008743/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008744 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8745 */
8746TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8747 addConfigurationProperty("touch.deviceType", "touchScreen");
8748 prepareAxes(POSITION);
8749 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8750
8751 prepareDisplay(DISPLAY_ORIENTATION_0);
8752 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8753 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8754 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8755 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8756
8757 NotifyMotionArgs args;
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8759 ASSERT_EQ(26, args.readTime);
8760
8761 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8762 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8763 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8764
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8766 ASSERT_EQ(33, args.readTime);
8767}
8768
8769/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008770 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8771 * events should not be delivered to the listener.
8772 */
8773TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8774 addConfigurationProperty("touch.deviceType", "touchScreen");
8775 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8776 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8777 ViewportType::INTERNAL);
8778 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8779 prepareAxes(POSITION);
8780 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8781
8782 NotifyMotionArgs motionArgs;
8783 processPosition(mapper, 100, 100);
8784 processSync(mapper);
8785
8786 mFakeListener->assertNotifyMotionWasNotCalled();
8787}
8788
Garfield Tanc734e4f2021-01-15 20:01:39 -08008789TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8790 addConfigurationProperty("touch.deviceType", "touchScreen");
8791 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8792 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8793 ViewportType::INTERNAL);
8794 std::optional<DisplayViewport> optionalDisplayViewport =
8795 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8796 ASSERT_TRUE(optionalDisplayViewport.has_value());
8797 DisplayViewport displayViewport = *optionalDisplayViewport;
8798
8799 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8800 prepareAxes(POSITION);
8801 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8802
8803 // Finger down
8804 int32_t x = 100, y = 100;
8805 processPosition(mapper, x, y);
8806 processSync(mapper);
8807
8808 NotifyMotionArgs motionArgs;
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8810 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8811
8812 // Deactivate display viewport
8813 displayViewport.isActive = false;
8814 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8815 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8816
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008817 // The ongoing touch should be canceled immediately
8818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8819 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8820
8821 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008822 x += 10, y += 10;
8823 processPosition(mapper, x, y);
8824 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008826
8827 // Reactivate display viewport
8828 displayViewport.isActive = true;
8829 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8830 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8831
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008832 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008833 x += 10, y += 10;
8834 processPosition(mapper, x, y);
8835 processSync(mapper);
Prabir Pradhanf670dad2022-08-05 22:32:11 +00008836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8837 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008838}
8839
Arthur Hung7c645402019-01-25 17:45:42 +08008840TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8841 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008842 prepareAxes(POSITION | ID | SLOT);
8843 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008844 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008845
8846 // Create the second touch screen device, and enable multi fingers.
8847 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008848 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008849 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008850 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008851 std::shared_ptr<InputDevice> device2 =
8852 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008853 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008855 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8856 0 /*flat*/, 0 /*fuzz*/);
8857 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8858 0 /*flat*/, 0 /*fuzz*/);
8859 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8860 0 /*flat*/, 0 /*fuzz*/);
8861 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8862 0 /*flat*/, 0 /*fuzz*/);
8863 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8864 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8865 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008866
8867 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008868 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008869 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8870 device2->reset(ARBITRARY_TIME);
8871
8872 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008873 std::shared_ptr<FakePointerController> fakePointerController =
8874 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008875 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008876
8877 // Setup policy for associated displays and show touches.
8878 const uint8_t hdmi1 = 0;
8879 const uint8_t hdmi2 = 1;
8880 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8881 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8882 mFakePolicy->setShowTouches(true);
8883
8884 // Create displays.
8885 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008886 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008887
8888 // Default device will reconfigure above, need additional reconfiguration for another device.
8889 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008890 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008891
8892 // Two fingers down at default display.
8893 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8894 processPosition(mapper, x1, y1);
8895 processId(mapper, 1);
8896 processSlot(mapper, 1);
8897 processPosition(mapper, x2, y2);
8898 processId(mapper, 2);
8899 processSync(mapper);
8900
8901 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8902 fakePointerController->getSpots().find(DISPLAY_ID);
8903 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8904 ASSERT_EQ(size_t(2), iter->second.size());
8905
8906 // Two fingers down at second display.
8907 processPosition(mapper2, x1, y1);
8908 processId(mapper2, 1);
8909 processSlot(mapper2, 1);
8910 processPosition(mapper2, x2, y2);
8911 processId(mapper2, 2);
8912 processSync(mapper2);
8913
8914 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8915 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8916 ASSERT_EQ(size_t(2), iter->second.size());
8917}
8918
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008919TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008920 prepareAxes(POSITION);
8921 addConfigurationProperty("touch.deviceType", "touchScreen");
8922 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008923 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008924
8925 NotifyMotionArgs motionArgs;
8926 // Unrotated video frame
8927 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8928 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008929 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008930 processPosition(mapper, 100, 200);
8931 processSync(mapper);
8932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8933 ASSERT_EQ(frames, motionArgs.videoFrames);
8934
8935 // Subsequent touch events should not have any videoframes
8936 // This is implemented separately in FakeEventHub,
8937 // but that should match the behaviour of TouchVideoDevice.
8938 processPosition(mapper, 200, 200);
8939 processSync(mapper);
8940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8941 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8942}
8943
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008944TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008945 prepareAxes(POSITION);
8946 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008947 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008948 // Unrotated video frame
8949 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8950 NotifyMotionArgs motionArgs;
8951
8952 // Test all 4 orientations
8953 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008954 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8955 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8956 clearViewports();
8957 prepareDisplay(orientation);
8958 std::vector<TouchVideoFrame> frames{frame};
8959 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8960 processPosition(mapper, 100, 200);
8961 processSync(mapper);
8962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8963 ASSERT_EQ(frames, motionArgs.videoFrames);
8964 }
8965}
8966
8967TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8968 prepareAxes(POSITION);
8969 addConfigurationProperty("touch.deviceType", "touchScreen");
8970 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8971 // orientation-aware are affected by display rotation.
8972 addConfigurationProperty("touch.orientationAware", "0");
8973 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8974 // Unrotated video frame
8975 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8976 NotifyMotionArgs motionArgs;
8977
8978 // Test all 4 orientations
8979 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008980 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8981 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8982 clearViewports();
8983 prepareDisplay(orientation);
8984 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008985 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008986 processPosition(mapper, 100, 200);
8987 processSync(mapper);
8988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008989 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8990 // compared to the display. This is so that when the window transform (which contains the
8991 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8992 // window's coordinate space.
8993 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008994 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnane74b35f2022-07-19 16:00:50 +08008995
8996 // Release finger.
8997 processSync(mapper);
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008999 }
9000}
9001
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009002TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009003 prepareAxes(POSITION);
9004 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009005 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009006 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9007 // so mix these.
9008 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9009 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9010 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9011 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9012 NotifyMotionArgs motionArgs;
9013
9014 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009015 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009016 processPosition(mapper, 100, 200);
9017 processSync(mapper);
9018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009019 ASSERT_EQ(frames, motionArgs.videoFrames);
9020}
9021
9022TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9023 prepareAxes(POSITION);
9024 addConfigurationProperty("touch.deviceType", "touchScreen");
9025 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9026 // orientation-aware are affected by display rotation.
9027 addConfigurationProperty("touch.orientationAware", "0");
9028 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9029 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9030 // so mix these.
9031 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9032 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9033 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9034 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9035 NotifyMotionArgs motionArgs;
9036
9037 prepareDisplay(DISPLAY_ORIENTATION_90);
9038 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9039 processPosition(mapper, 100, 200);
9040 processSync(mapper);
9041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9042 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9043 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9044 // compared to the display. This is so that when the window transform (which contains the
9045 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9046 // window's coordinate space.
9047 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9048 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009049 ASSERT_EQ(frames, motionArgs.videoFrames);
9050}
9051
Arthur Hung9da14732019-09-02 16:16:58 +08009052/**
9053 * If we had defined port associations, but the viewport is not ready, the touch device would be
9054 * expected to be disabled, and it should be enabled after the viewport has found.
9055 */
9056TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009057 constexpr uint8_t hdmi2 = 1;
9058 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009059 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009060
9061 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9062
9063 addConfigurationProperty("touch.deviceType", "touchScreen");
9064 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009065 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009066
9067 ASSERT_EQ(mDevice->isEnabled(), false);
9068
9069 // Add display on hdmi2, the device should be enabled and can receive touch event.
9070 prepareSecondaryDisplay(type, hdmi2);
9071 ASSERT_EQ(mDevice->isEnabled(), true);
9072
9073 // Send a touch event.
9074 processPosition(mapper, 100, 100);
9075 processSync(mapper);
9076
9077 NotifyMotionArgs args;
9078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9079 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9080}
9081
Arthur Hung421eb1c2020-01-16 00:09:42 +08009082TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009083 addConfigurationProperty("touch.deviceType", "touchScreen");
9084 prepareDisplay(DISPLAY_ORIENTATION_0);
9085 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009086 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009087
9088 NotifyMotionArgs motionArgs;
9089
9090 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9091 // finger down
9092 processId(mapper, 1);
9093 processPosition(mapper, x1, y1);
9094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9098
9099 // finger move
9100 processId(mapper, 1);
9101 processPosition(mapper, x2, y2);
9102 processSync(mapper);
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9106
9107 // finger up.
9108 processId(mapper, -1);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9111 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9113
9114 // new finger down
9115 processId(mapper, 1);
9116 processPosition(mapper, x3, y3);
9117 processSync(mapper);
9118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9119 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9121}
9122
9123/**
arthurhungcc7f9802020-04-30 17:55:40 +08009124 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9125 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009126 */
arthurhungcc7f9802020-04-30 17:55:40 +08009127TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009128 addConfigurationProperty("touch.deviceType", "touchScreen");
9129 prepareDisplay(DISPLAY_ORIENTATION_0);
9130 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009131 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009132
9133 NotifyMotionArgs motionArgs;
9134
9135 // default tool type is finger
9136 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009137 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009138 processPosition(mapper, x1, y1);
9139 processSync(mapper);
9140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9141 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9143
9144 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9145 processToolType(mapper, MT_TOOL_PALM);
9146 processSync(mapper);
9147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9148 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9149
9150 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009151 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009152 processPosition(mapper, x2, y2);
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9155
9156 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009157 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009158 processSync(mapper);
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9160
9161 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009162 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009163 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009164 processPosition(mapper, x3, y3);
9165 processSync(mapper);
9166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9169}
9170
arthurhungbf89a482020-04-17 17:37:55 +08009171/**
arthurhungcc7f9802020-04-30 17:55:40 +08009172 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9173 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009174 */
arthurhungcc7f9802020-04-30 17:55:40 +08009175TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009176 addConfigurationProperty("touch.deviceType", "touchScreen");
9177 prepareDisplay(DISPLAY_ORIENTATION_0);
9178 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9179 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9180
9181 NotifyMotionArgs motionArgs;
9182
9183 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009184 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9185 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009186 processPosition(mapper, x1, y1);
9187 processSync(mapper);
9188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9191
9192 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009193 processSlot(mapper, SECOND_SLOT);
9194 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009195 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009196 processSync(mapper);
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009198 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9200
9201 // If the tool type of the first finger changes to MT_TOOL_PALM,
9202 // we expect to receive ACTION_POINTER_UP with cancel flag.
9203 processSlot(mapper, FIRST_SLOT);
9204 processId(mapper, FIRST_TRACKING_ID);
9205 processToolType(mapper, MT_TOOL_PALM);
9206 processSync(mapper);
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009208 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009209 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9210
9211 // The following MOVE events of second finger should be processed.
9212 processSlot(mapper, SECOND_SLOT);
9213 processId(mapper, SECOND_TRACKING_ID);
9214 processPosition(mapper, x2 + 1, y2 + 1);
9215 processSync(mapper);
9216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9218 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9219
9220 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9221 // it. Second finger receive move.
9222 processSlot(mapper, FIRST_SLOT);
9223 processId(mapper, INVALID_TRACKING_ID);
9224 processSync(mapper);
9225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9227 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9228
9229 // Second finger keeps moving.
9230 processSlot(mapper, SECOND_SLOT);
9231 processId(mapper, SECOND_TRACKING_ID);
9232 processPosition(mapper, x2 + 2, y2 + 2);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9236 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9237
9238 // Second finger up.
9239 processId(mapper, INVALID_TRACKING_ID);
9240 processSync(mapper);
9241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9242 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9243 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9244}
9245
9246/**
9247 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9248 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9249 */
9250TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9251 addConfigurationProperty("touch.deviceType", "touchScreen");
9252 prepareDisplay(DISPLAY_ORIENTATION_0);
9253 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9254 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9255
9256 NotifyMotionArgs motionArgs;
9257
9258 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9259 // First finger down.
9260 processId(mapper, FIRST_TRACKING_ID);
9261 processPosition(mapper, x1, y1);
9262 processSync(mapper);
9263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9264 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9265 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9266
9267 // Second finger down.
9268 processSlot(mapper, SECOND_SLOT);
9269 processId(mapper, SECOND_TRACKING_ID);
9270 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009271 processSync(mapper);
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009273 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9275
arthurhungcc7f9802020-04-30 17:55:40 +08009276 // If the tool type of the first finger changes to MT_TOOL_PALM,
9277 // we expect to receive ACTION_POINTER_UP with cancel flag.
9278 processSlot(mapper, FIRST_SLOT);
9279 processId(mapper, FIRST_TRACKING_ID);
9280 processToolType(mapper, MT_TOOL_PALM);
9281 processSync(mapper);
9282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009283 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009284 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9285
9286 // Second finger keeps moving.
9287 processSlot(mapper, SECOND_SLOT);
9288 processId(mapper, SECOND_TRACKING_ID);
9289 processPosition(mapper, x2 + 1, y2 + 1);
9290 processSync(mapper);
9291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9293
9294 // second finger becomes palm, receive cancel due to only 1 finger is active.
9295 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009296 processToolType(mapper, MT_TOOL_PALM);
9297 processSync(mapper);
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9299 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9300
arthurhungcc7f9802020-04-30 17:55:40 +08009301 // third finger down.
9302 processSlot(mapper, THIRD_SLOT);
9303 processId(mapper, THIRD_TRACKING_ID);
9304 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009305 processPosition(mapper, x3, y3);
9306 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9308 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009310 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9311
9312 // third finger move
9313 processId(mapper, THIRD_TRACKING_ID);
9314 processPosition(mapper, x3 + 1, y3 + 1);
9315 processSync(mapper);
9316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9318
9319 // first finger up, third finger receive move.
9320 processSlot(mapper, FIRST_SLOT);
9321 processId(mapper, INVALID_TRACKING_ID);
9322 processSync(mapper);
9323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9325 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9326
9327 // second finger up, third finger receive move.
9328 processSlot(mapper, SECOND_SLOT);
9329 processId(mapper, INVALID_TRACKING_ID);
9330 processSync(mapper);
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9333 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9334
9335 // third finger up.
9336 processSlot(mapper, THIRD_SLOT);
9337 processId(mapper, INVALID_TRACKING_ID);
9338 processSync(mapper);
9339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9340 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9341 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9342}
9343
9344/**
9345 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9346 * and the active finger could still be allowed to receive the events
9347 */
9348TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9349 addConfigurationProperty("touch.deviceType", "touchScreen");
9350 prepareDisplay(DISPLAY_ORIENTATION_0);
9351 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9352 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9353
9354 NotifyMotionArgs motionArgs;
9355
9356 // default tool type is finger
9357 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9358 processId(mapper, FIRST_TRACKING_ID);
9359 processPosition(mapper, x1, y1);
9360 processSync(mapper);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9362 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9364
9365 // Second finger down.
9366 processSlot(mapper, SECOND_SLOT);
9367 processId(mapper, SECOND_TRACKING_ID);
9368 processPosition(mapper, x2, y2);
9369 processSync(mapper);
9370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009371 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9373
9374 // If the tool type of the second finger changes to MT_TOOL_PALM,
9375 // we expect to receive ACTION_POINTER_UP with cancel flag.
9376 processId(mapper, SECOND_TRACKING_ID);
9377 processToolType(mapper, MT_TOOL_PALM);
9378 processSync(mapper);
9379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009380 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009381 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9382
9383 // The following MOVE event should be processed.
9384 processSlot(mapper, FIRST_SLOT);
9385 processId(mapper, FIRST_TRACKING_ID);
9386 processPosition(mapper, x1 + 1, y1 + 1);
9387 processSync(mapper);
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9390 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9391
9392 // second finger up.
9393 processSlot(mapper, SECOND_SLOT);
9394 processId(mapper, INVALID_TRACKING_ID);
9395 processSync(mapper);
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9398
9399 // first finger keep moving
9400 processSlot(mapper, FIRST_SLOT);
9401 processId(mapper, FIRST_TRACKING_ID);
9402 processPosition(mapper, x1 + 2, y1 + 2);
9403 processSync(mapper);
9404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9406
9407 // first finger up.
9408 processId(mapper, INVALID_TRACKING_ID);
9409 processSync(mapper);
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9411 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9412 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009413}
9414
Arthur Hung9ad18942021-06-19 02:04:46 +00009415/**
9416 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9417 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9418 * cause slot be valid again.
9419 */
9420TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9421 addConfigurationProperty("touch.deviceType", "touchScreen");
9422 prepareDisplay(DISPLAY_ORIENTATION_0);
9423 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9424 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9425
9426 NotifyMotionArgs motionArgs;
9427
9428 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9429 // First finger down.
9430 processId(mapper, FIRST_TRACKING_ID);
9431 processPosition(mapper, x1, y1);
9432 processPressure(mapper, RAW_PRESSURE_MAX);
9433 processSync(mapper);
9434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9436 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9437
9438 // First finger move.
9439 processId(mapper, FIRST_TRACKING_ID);
9440 processPosition(mapper, x1 + 1, y1 + 1);
9441 processPressure(mapper, RAW_PRESSURE_MAX);
9442 processSync(mapper);
9443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9445 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9446
9447 // Second finger down.
9448 processSlot(mapper, SECOND_SLOT);
9449 processId(mapper, SECOND_TRACKING_ID);
9450 processPosition(mapper, x2, y2);
9451 processPressure(mapper, RAW_PRESSURE_MAX);
9452 processSync(mapper);
9453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009454 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009455 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9456
9457 // second finger up with some unexpected data.
9458 processSlot(mapper, SECOND_SLOT);
9459 processId(mapper, INVALID_TRACKING_ID);
9460 processPosition(mapper, x2, y2);
9461 processSync(mapper);
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009463 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009464 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9465
9466 // first finger up with some unexpected data.
9467 processSlot(mapper, FIRST_SLOT);
9468 processId(mapper, INVALID_TRACKING_ID);
9469 processPosition(mapper, x2, y2);
9470 processPressure(mapper, RAW_PRESSURE_MAX);
9471 processSync(mapper);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9473 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9474 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9475}
9476
Prabir Pradhan6c7fd132022-09-27 19:32:43 +00009477TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9478 addConfigurationProperty("touch.deviceType", "touchScreen");
9479 prepareDisplay(DISPLAY_ORIENTATION_0);
9480 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9481 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9482
9483 NotifyMotionArgs motionArgs;
9484
9485 // First finger down.
9486 processId(mapper, FIRST_TRACKING_ID);
9487 processPosition(mapper, 100, 200);
9488 processPressure(mapper, RAW_PRESSURE_MAX);
9489 processSync(mapper);
9490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9491 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9492
9493 // Second finger down.
9494 processSlot(mapper, SECOND_SLOT);
9495 processId(mapper, SECOND_TRACKING_ID);
9496 processPosition(mapper, 300, 400);
9497 processPressure(mapper, RAW_PRESSURE_MAX);
9498 processSync(mapper);
9499 ASSERT_NO_FATAL_FAILURE(
9500 mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9501 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
9502
9503 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
9504 // preserved. Resetting should not generate any events.
9505 mapper.reset(ARBITRARY_TIME);
9506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9507
9508 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9509 // the existing touch state to generate a down event.
9510 processPosition(mapper, 301, 302);
9511 processSync(mapper);
9512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9513 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9515 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
9516
9517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9518}
9519
9520TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9521 addConfigurationProperty("touch.deviceType", "touchScreen");
9522 prepareDisplay(DISPLAY_ORIENTATION_0);
9523 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9524 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9525
9526 NotifyMotionArgs motionArgs;
9527
9528 // First finger touches down and releases.
9529 processId(mapper, FIRST_TRACKING_ID);
9530 processPosition(mapper, 100, 200);
9531 processPressure(mapper, RAW_PRESSURE_MAX);
9532 processSync(mapper);
9533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9534 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9535 processId(mapper, INVALID_TRACKING_ID);
9536 processSync(mapper);
9537 ASSERT_NO_FATAL_FAILURE(
9538 mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9539 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9540
9541 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9542 // raw state where no pointers are down.
9543 mapper.reset(ARBITRARY_TIME);
9544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9545
9546 // Send an empty sync frame. Since there are no pointers, no events are generated.
9547 processSync(mapper);
9548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9549}
9550
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009551// --- MultiTouchInputMapperTest_ExternalDevice ---
9552
9553class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9554protected:
Chris Yea52ade12020-08-27 16:49:20 -07009555 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009556};
9557
9558/**
9559 * Expect fallback to internal viewport if device is external and external viewport is not present.
9560 */
9561TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9562 prepareAxes(POSITION);
9563 addConfigurationProperty("touch.deviceType", "touchScreen");
9564 prepareDisplay(DISPLAY_ORIENTATION_0);
9565 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9566
9567 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9568
9569 NotifyMotionArgs motionArgs;
9570
9571 // Expect the event to be sent to the internal viewport,
9572 // because an external viewport is not present.
9573 processPosition(mapper, 100, 100);
9574 processSync(mapper);
9575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9576 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9577
9578 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009579 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009580 processPosition(mapper, 100, 100);
9581 processSync(mapper);
9582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9583 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9584}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009585
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009586TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9587 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9588 std::shared_ptr<FakePointerController> fakePointerController =
9589 std::make_shared<FakePointerController>();
9590 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9591 fakePointerController->setPosition(0, 0);
9592 fakePointerController->setButtonState(0);
9593
9594 // prepare device and capture
9595 prepareDisplay(DISPLAY_ORIENTATION_0);
9596 prepareAxes(POSITION | ID | SLOT);
9597 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9598 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9599 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009600 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009601 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9602
9603 // captured touchpad should be a touchpad source
9604 NotifyDeviceResetArgs resetArgs;
9605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9606 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9607
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009608 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009609
9610 const InputDeviceInfo::MotionRange* relRangeX =
9611 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9612 ASSERT_NE(relRangeX, nullptr);
9613 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9614 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9615 const InputDeviceInfo::MotionRange* relRangeY =
9616 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9617 ASSERT_NE(relRangeY, nullptr);
9618 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9619 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9620
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009621 // run captured pointer tests - note that this is unscaled, so input listener events should be
9622 // identical to what the hardware sends (accounting for any
9623 // calibration).
9624 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009625 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009626 processId(mapper, 1);
9627 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9628 processKey(mapper, BTN_TOUCH, 1);
9629 processSync(mapper);
9630
9631 // expect coord[0] to contain initial location of touch 0
9632 NotifyMotionArgs args;
9633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9634 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9635 ASSERT_EQ(1U, args.pointerCount);
9636 ASSERT_EQ(0, args.pointerProperties[0].id);
9637 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9638 ASSERT_NO_FATAL_FAILURE(
9639 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9640
9641 // FINGER 1 DOWN
9642 processSlot(mapper, 1);
9643 processId(mapper, 2);
9644 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9645 processSync(mapper);
9646
9647 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009649 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009650 ASSERT_EQ(2U, args.pointerCount);
9651 ASSERT_EQ(0, args.pointerProperties[0].id);
9652 ASSERT_EQ(1, args.pointerProperties[1].id);
9653 ASSERT_NO_FATAL_FAILURE(
9654 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9655 ASSERT_NO_FATAL_FAILURE(
9656 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9657
9658 // FINGER 1 MOVE
9659 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9660 processSync(mapper);
9661
9662 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9663 // from move
9664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9666 ASSERT_NO_FATAL_FAILURE(
9667 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9668 ASSERT_NO_FATAL_FAILURE(
9669 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9670
9671 // FINGER 0 MOVE
9672 processSlot(mapper, 0);
9673 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9674 processSync(mapper);
9675
9676 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9679 ASSERT_NO_FATAL_FAILURE(
9680 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9681 ASSERT_NO_FATAL_FAILURE(
9682 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9683
9684 // BUTTON DOWN
9685 processKey(mapper, BTN_LEFT, 1);
9686 processSync(mapper);
9687
9688 // touchinputmapper design sends a move before button press
9689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9693
9694 // BUTTON UP
9695 processKey(mapper, BTN_LEFT, 0);
9696 processSync(mapper);
9697
9698 // touchinputmapper design sends a move after button release
9699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9700 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9703
9704 // FINGER 0 UP
9705 processId(mapper, -1);
9706 processSync(mapper);
9707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9708 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9709
9710 // FINGER 1 MOVE
9711 processSlot(mapper, 1);
9712 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9713 processSync(mapper);
9714
9715 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9718 ASSERT_EQ(1U, args.pointerCount);
9719 ASSERT_EQ(1, args.pointerProperties[0].id);
9720 ASSERT_NO_FATAL_FAILURE(
9721 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9722
9723 // FINGER 1 UP
9724 processId(mapper, -1);
9725 processKey(mapper, BTN_TOUCH, 0);
9726 processSync(mapper);
9727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9728 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9729
9730 // non captured touchpad should be a mouse source
9731 mFakePolicy->setPointerCapture(false);
9732 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9734 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9735}
9736
9737TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9738 std::shared_ptr<FakePointerController> fakePointerController =
9739 std::make_shared<FakePointerController>();
9740 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9741 fakePointerController->setPosition(0, 0);
9742 fakePointerController->setButtonState(0);
9743
9744 // prepare device and capture
9745 prepareDisplay(DISPLAY_ORIENTATION_0);
9746 prepareAxes(POSITION | ID | SLOT);
9747 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9748 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009749 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009750 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9751 // run uncaptured pointer tests - pushes out generic events
9752 // FINGER 0 DOWN
9753 processId(mapper, 3);
9754 processPosition(mapper, 100, 100);
9755 processKey(mapper, BTN_TOUCH, 1);
9756 processSync(mapper);
9757
9758 // start at (100,100), cursor should be at (0,0) * scale
9759 NotifyMotionArgs args;
9760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9761 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9762 ASSERT_NO_FATAL_FAILURE(
9763 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9764
9765 // FINGER 0 MOVE
9766 processPosition(mapper, 200, 200);
9767 processSync(mapper);
9768
9769 // compute scaling to help with touch position checking
9770 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9771 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9772 float scale =
9773 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9774
9775 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9777 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9779 0, 0, 0, 0, 0, 0, 0));
9780}
9781
9782TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9783 std::shared_ptr<FakePointerController> fakePointerController =
9784 std::make_shared<FakePointerController>();
9785
9786 prepareDisplay(DISPLAY_ORIENTATION_0);
9787 prepareAxes(POSITION | ID | SLOT);
9788 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009789 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009790 mFakePolicy->setPointerCapture(false);
9791 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9792
9793 // uncaptured touchpad should be a pointer device
9794 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9795
9796 // captured touchpad should be a touchpad device
9797 mFakePolicy->setPointerCapture(true);
9798 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9799 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9800}
9801
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009802// --- JoystickInputMapperTest ---
9803
9804class JoystickInputMapperTest : public InputMapperTest {
9805protected:
9806 static const int32_t RAW_X_MIN;
9807 static const int32_t RAW_X_MAX;
9808 static const int32_t RAW_Y_MIN;
9809 static const int32_t RAW_Y_MAX;
9810
9811 void SetUp() override {
9812 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9813 }
9814 void prepareAxes() {
9815 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9816 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9817 }
9818
9819 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9821 }
9822
9823 void processSync(JoystickInputMapper& mapper) {
9824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9825 }
9826
9827 void prepareVirtualDisplay(int32_t orientation) {
9828 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9829 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9830 NO_PORT, ViewportType::VIRTUAL);
9831 }
9832};
9833
9834const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9835const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9836const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9837const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9838
9839TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9840 prepareAxes();
9841 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9842
9843 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9844
9845 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9846
9847 // Send an axis event
9848 processAxis(mapper, ABS_X, 100);
9849 processSync(mapper);
9850
9851 NotifyMotionArgs args;
9852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9853 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9854
9855 // Send another axis event
9856 processAxis(mapper, ABS_Y, 100);
9857 processSync(mapper);
9858
9859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9860 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9861}
9862
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009863// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009864
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009865class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009866protected:
9867 static const char* DEVICE_NAME;
9868 static const char* DEVICE_LOCATION;
9869 static const int32_t DEVICE_ID;
9870 static const int32_t DEVICE_GENERATION;
9871 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009872 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009873 static const int32_t EVENTHUB_ID;
9874
9875 std::shared_ptr<FakeEventHub> mFakeEventHub;
9876 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009877 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009878 std::unique_ptr<InstrumentedInputReader> mReader;
9879 std::shared_ptr<InputDevice> mDevice;
9880
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009881 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009882 mFakeEventHub = std::make_unique<FakeEventHub>();
9883 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009884 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009885 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009886 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009887 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9888 }
9889
9890 void SetUp() override { SetUp(DEVICE_CLASSES); }
9891
9892 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009893 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009894 mFakePolicy.clear();
9895 }
9896
9897 void configureDevice(uint32_t changes) {
9898 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9899 mReader->requestRefreshConfiguration(changes);
9900 mReader->loopOnce();
9901 }
9902 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9903 }
9904
9905 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9906 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009907 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009908 InputDeviceIdentifier identifier;
9909 identifier.name = name;
9910 identifier.location = location;
9911 std::shared_ptr<InputDevice> device =
9912 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9913 identifier);
9914 mReader->pushNextDevice(device);
9915 mFakeEventHub->addDevice(eventHubId, name, classes);
9916 mReader->loopOnce();
9917 return device;
9918 }
9919
9920 template <class T, typename... Args>
9921 T& addControllerAndConfigure(Args... args) {
9922 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9923
9924 return controller;
9925 }
9926};
9927
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009928const char* PeripheralControllerTest::DEVICE_NAME = "device";
9929const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9930const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9931const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9932const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009933const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9934 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009935const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009936
9937// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009938class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009939protected:
9940 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009941 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009942 }
9943};
9944
9945TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009946 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009947
9948 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9949 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9950}
9951
9952TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009953 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009954
9955 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9956 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9957}
9958
9959// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009960class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009961protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009962 void SetUp() override {
9963 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9964 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009965};
9966
Chris Ye85758332021-05-16 23:05:17 -07009967TEST_F(LightControllerTest, MonoLight) {
9968 RawLightInfo infoMono = {.id = 1,
9969 .name = "Mono",
9970 .maxBrightness = 255,
9971 .flags = InputLightClass::BRIGHTNESS,
9972 .path = ""};
9973 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009974
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009975 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009976 InputDeviceInfo info;
9977 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009978 std::vector<InputDeviceLightInfo> lights = info.getLights();
9979 ASSERT_EQ(1U, lights.size());
9980 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009981
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009982 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9983 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009984}
9985
9986TEST_F(LightControllerTest, RGBLight) {
9987 RawLightInfo infoRed = {.id = 1,
9988 .name = "red",
9989 .maxBrightness = 255,
9990 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9991 .path = ""};
9992 RawLightInfo infoGreen = {.id = 2,
9993 .name = "green",
9994 .maxBrightness = 255,
9995 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9996 .path = ""};
9997 RawLightInfo infoBlue = {.id = 3,
9998 .name = "blue",
9999 .maxBrightness = 255,
10000 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10001 .path = ""};
10002 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10003 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10004 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10005
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010006 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010007 InputDeviceInfo info;
10008 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010009 std::vector<InputDeviceLightInfo> lights = info.getLights();
10010 ASSERT_EQ(1U, lights.size());
10011 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010012
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010013 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10014 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010015}
10016
10017TEST_F(LightControllerTest, MultiColorRGBLight) {
10018 RawLightInfo infoColor = {.id = 1,
10019 .name = "red",
10020 .maxBrightness = 255,
10021 .flags = InputLightClass::BRIGHTNESS |
10022 InputLightClass::MULTI_INTENSITY |
10023 InputLightClass::MULTI_INDEX,
10024 .path = ""};
10025
10026 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10027
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010028 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010029 InputDeviceInfo info;
10030 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010031 std::vector<InputDeviceLightInfo> lights = info.getLights();
10032 ASSERT_EQ(1U, lights.size());
10033 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010034
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010035 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10036 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010037}
10038
10039TEST_F(LightControllerTest, PlayerIdLight) {
10040 RawLightInfo info1 = {.id = 1,
10041 .name = "player1",
10042 .maxBrightness = 255,
10043 .flags = InputLightClass::BRIGHTNESS,
10044 .path = ""};
10045 RawLightInfo info2 = {.id = 2,
10046 .name = "player2",
10047 .maxBrightness = 255,
10048 .flags = InputLightClass::BRIGHTNESS,
10049 .path = ""};
10050 RawLightInfo info3 = {.id = 3,
10051 .name = "player3",
10052 .maxBrightness = 255,
10053 .flags = InputLightClass::BRIGHTNESS,
10054 .path = ""};
10055 RawLightInfo info4 = {.id = 4,
10056 .name = "player4",
10057 .maxBrightness = 255,
10058 .flags = InputLightClass::BRIGHTNESS,
10059 .path = ""};
10060 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10061 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10062 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10063 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10064
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010065 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010066 InputDeviceInfo info;
10067 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010068 std::vector<InputDeviceLightInfo> lights = info.getLights();
10069 ASSERT_EQ(1U, lights.size());
10070 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010071
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010072 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10073 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10074 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010075}
10076
Michael Wrightd02c5b62014-02-10 15:10:22 -080010077} // namespace android