blob: 594c1cb8ef858f4808d00df52d8bde92be814c12 [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
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000041#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000042#include "input/DisplayViewport.h"
43#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010044
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000045using android::hardware::input::InputDeviceCountryCode;
46
Michael Wrightd02c5b62014-02-10 15:10:22 -080047namespace android {
48
Dominik Laskowski2f01d772022-03-23 16:01:29 -070049using namespace ftl::flag_operators;
50
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070051using std::chrono_literals::operator""ms;
52
53// Timeout for waiting for an expected event
54static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
55
Michael Wrightd02c5b62014-02-10 15:10:22 -080056// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000057static constexpr nsecs_t ARBITRARY_TIME = 1234;
58static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
60// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080061static constexpr int32_t DISPLAY_ID = 0;
62static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
63static constexpr int32_t DISPLAY_WIDTH = 480;
64static constexpr int32_t DISPLAY_HEIGHT = 800;
65static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
66static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
67static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070068static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070069static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080070
arthurhungcc7f9802020-04-30 17:55:40 +080071static constexpr int32_t FIRST_SLOT = 0;
72static constexpr int32_t SECOND_SLOT = 1;
73static constexpr int32_t THIRD_SLOT = 2;
74static constexpr int32_t INVALID_TRACKING_ID = -1;
75static constexpr int32_t FIRST_TRACKING_ID = 0;
76static constexpr int32_t SECOND_TRACKING_ID = 1;
77static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080078static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080079static constexpr int32_t BATTERY_STATUS = 4;
80static constexpr int32_t BATTERY_CAPACITY = 66;
Prabir Pradhane287ecd2022-09-07 21:18:05 +000081static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
Chris Ye3fdbfef2021-01-06 18:45:18 -080082static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
83static constexpr int32_t LIGHT_COLOR = 0x7F448866;
84static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080085
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080086static constexpr int32_t ACTION_POINTER_0_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_0_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94
Michael Wrightd02c5b62014-02-10 15:10:22 -080095// Error tolerance for floating point assertions.
96static const float EPSILON = 0.001f;
97
98template<typename T>
99static inline T min(T a, T b) {
100 return a < b ? a : b;
101}
102
103static inline float avg(float x, float y) {
104 return (x + y) / 2;
105}
106
Chris Ye3fdbfef2021-01-06 18:45:18 -0800107// Mapping for light color name and the light color
108const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
109 {"green", LightColor::GREEN},
110 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700112static int32_t getInverseRotation(int32_t orientation) {
113 switch (orientation) {
114 case DISPLAY_ORIENTATION_90:
115 return DISPLAY_ORIENTATION_270;
116 case DISPLAY_ORIENTATION_270:
117 return DISPLAY_ORIENTATION_90;
118 default:
119 return orientation;
120 }
121}
122
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800123static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
124 InputDeviceInfo info;
125 mapper.populateDeviceInfo(&info);
126
127 const InputDeviceInfo::MotionRange* motionRange =
128 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
129 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
130}
131
132static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
133 InputDeviceInfo info;
134 mapper.populateDeviceInfo(&info);
135
136 const InputDeviceInfo::MotionRange* motionRange =
137 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
138 ASSERT_EQ(nullptr, motionRange);
139}
140
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141// --- FakePointerController ---
142
143class FakePointerController : public PointerControllerInterface {
144 bool mHaveBounds;
145 float mMinX, mMinY, mMaxX, mMaxY;
146 float mX, mY;
147 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800148 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150public:
151 FakePointerController() :
152 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800153 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 }
155
Michael Wright17db18e2020-06-26 20:51:44 +0100156 virtual ~FakePointerController() {}
157
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158 void setBounds(float minX, float minY, float maxX, float maxY) {
159 mHaveBounds = true;
160 mMinX = minX;
161 mMinY = minY;
162 mMaxX = maxX;
163 mMaxY = maxY;
164 }
165
Chris Yea52ade12020-08-27 16:49:20 -0700166 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167 mX = x;
168 mY = y;
169 }
170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172
Chris Yea52ade12020-08-27 16:49:20 -0700173 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174
Chris Yea52ade12020-08-27 16:49:20 -0700175 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176 *outX = mX;
177 *outY = mY;
178 }
179
Chris Yea52ade12020-08-27 16:49:20 -0700180 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800181
Chris Yea52ade12020-08-27 16:49:20 -0700182 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800183 mDisplayId = viewport.displayId;
184 }
185
Arthur Hung7c645402019-01-25 17:45:42 +0800186 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
187 return mSpotsByDisplay;
188 }
189
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190private:
Chris Yea52ade12020-08-27 16:49:20 -0700191 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 *outMinX = mMinX;
193 *outMinY = mMinY;
194 *outMaxX = mMaxX;
195 *outMaxY = mMaxY;
196 return mHaveBounds;
197 }
198
Chris Yea52ade12020-08-27 16:49:20 -0700199 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200 mX += deltaX;
201 if (mX < mMinX) mX = mMinX;
202 if (mX > mMaxX) mX = mMaxX;
203 mY += deltaY;
204 if (mY < mMinY) mY = mMinY;
205 if (mY > mMaxY) mY = mMaxY;
206 }
207
Chris Yea52ade12020-08-27 16:49:20 -0700208 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Chris Yea52ade12020-08-27 16:49:20 -0700210 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211
Chris Yea52ade12020-08-27 16:49:20 -0700212 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
Chris Yea52ade12020-08-27 16:49:20 -0700214 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
215 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800216 std::vector<int32_t> newSpots;
217 // Add spots for fingers that are down.
218 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
219 uint32_t id = idBits.clearFirstMarkedBit();
220 newSpots.push_back(id);
221 }
222
223 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224 }
225
Prabir Pradhan197e0862022-07-01 14:28:00 +0000226 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800227
228 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229};
230
231
232// --- FakeInputReaderPolicy ---
233
234class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700235 std::mutex mLock;
236 std::condition_variable mDevicesChangedCondition;
237
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000239 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700240 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
241 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100242 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700243 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244
245protected:
Chris Yea52ade12020-08-27 16:49:20 -0700246 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247
248public:
249 FakeInputReaderPolicy() {
250 }
251
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700252 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800253 waitForInputDevices([](bool devicesChanged) {
254 if (!devicesChanged) {
255 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
256 }
257 });
258 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700259
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800260 void assertInputDevicesNotChanged() {
261 waitForInputDevices([](bool devicesChanged) {
262 if (devicesChanged) {
263 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
264 }
265 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700266 }
267
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700268 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100269 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100270 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700271 }
272
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700273 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
274 return mConfig.getDisplayViewportByUniqueId(uniqueId);
275 }
276 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
277 return mConfig.getDisplayViewportByType(type);
278 }
279
280 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
281 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700282 }
283
Prabir Pradhan5632d622021-09-06 07:57:20 -0700284 void addDisplayViewport(DisplayViewport viewport) {
285 mViewports.push_back(std::move(viewport));
286 mConfig.setDisplayViewports(mViewports);
287 }
288
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700289 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000290 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700291 std::optional<uint8_t> physicalPort, ViewportType type) {
292 const bool isRotated =
293 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
294 DisplayViewport v;
295 v.displayId = displayId;
296 v.orientation = orientation;
297 v.logicalLeft = 0;
298 v.logicalTop = 0;
299 v.logicalRight = isRotated ? height : width;
300 v.logicalBottom = isRotated ? width : height;
301 v.physicalLeft = 0;
302 v.physicalTop = 0;
303 v.physicalRight = isRotated ? height : width;
304 v.physicalBottom = isRotated ? width : height;
305 v.deviceWidth = isRotated ? height : width;
306 v.deviceHeight = isRotated ? width : height;
307 v.isActive = isActive;
308 v.uniqueId = uniqueId;
309 v.physicalPort = physicalPort;
310 v.type = type;
311
312 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 }
314
Arthur Hung6cd19a42019-08-30 19:04:12 +0800315 bool updateViewport(const DisplayViewport& viewport) {
316 size_t count = mViewports.size();
317 for (size_t i = 0; i < count; i++) {
318 const DisplayViewport& currentViewport = mViewports[i];
319 if (currentViewport.displayId == viewport.displayId) {
320 mViewports[i] = viewport;
321 mConfig.setDisplayViewports(mViewports);
322 return true;
323 }
324 }
325 // no viewport found.
326 return false;
327 }
328
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100329 void addExcludedDeviceName(const std::string& deviceName) {
330 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 }
332
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700333 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
334 mConfig.portAssociations.insert({inputPort, displayPort});
335 }
336
Christine Franks1ba71cc2021-04-07 14:37:42 -0700337 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
338 const std::string& displayUniqueId) {
339 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
340 }
341
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000342 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700343
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000344 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700345
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000346 void setPointerController(std::shared_ptr<FakePointerController> controller) {
347 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348 }
349
350 const InputReaderConfiguration* getReaderConfiguration() const {
351 return &mConfig;
352 }
353
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800354 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355 return mInputDevices;
356 }
357
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100358 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700359 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700360 return transform;
361 }
362
363 void setTouchAffineTransformation(const TouchAffineTransformation t) {
364 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800365 }
366
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000367 PointerCaptureRequest setPointerCapture(bool enabled) {
368 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
369 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800370 }
371
Arthur Hung7c645402019-01-25 17:45:42 +0800372 void setShowTouches(bool enabled) {
373 mConfig.showTouches = enabled;
374 }
375
Garfield Tan888a6a42020-01-09 11:39:16 -0800376 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
377 mConfig.defaultPointerDisplayId = pointerDisplayId;
378 }
379
HQ Liue6983c72022-04-19 22:14:56 +0000380 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
381
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800382 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
383
HQ Liue6983c72022-04-19 22:14:56 +0000384 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
385
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000386 void setVelocityControlParams(const VelocityControlParameters& params) {
387 mConfig.pointerVelocityControlParameters = params;
388 mConfig.wheelVelocityControlParameters = params;
389 }
390
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000392 uint32_t mNextPointerCaptureSequenceNumber = 0;
393
Chris Yea52ade12020-08-27 16:49:20 -0700394 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 *outConfig = mConfig;
396 }
397
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000398 std::shared_ptr<PointerControllerInterface> obtainPointerController(
399 int32_t /*deviceId*/) override {
400 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 }
402
Chris Yea52ade12020-08-27 16:49:20 -0700403 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700404 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700406 mInputDevicesChanged = true;
407 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 }
409
Chris Yea52ade12020-08-27 16:49:20 -0700410 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
411 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700412 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413 }
414
Chris Yea52ade12020-08-27 16:49:20 -0700415 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800416
417 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
418 std::unique_lock<std::mutex> lock(mLock);
419 base::ScopedLockAssertion assumeLocked(mLock);
420
421 const bool devicesChanged =
422 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
423 return mInputDevicesChanged;
424 });
425 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
426 mInputDevicesChanged = false;
427 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428};
429
Michael Wrightd02c5b62014-02-10 15:10:22 -0800430// --- FakeEventHub ---
431
432class FakeEventHub : public EventHubInterface {
433 struct KeyInfo {
434 int32_t keyCode;
435 uint32_t flags;
436 };
437
Chris Yef59a2f42020-10-16 12:55:26 -0700438 struct SensorInfo {
439 InputDeviceSensorType sensorType;
440 int32_t sensorDataIndex;
441 };
442
Michael Wrightd02c5b62014-02-10 15:10:22 -0800443 struct Device {
444 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700445 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800446 PropertyMap configuration;
447 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
448 KeyedVector<int, bool> relativeAxes;
449 KeyedVector<int32_t, int32_t> keyCodeStates;
450 KeyedVector<int32_t, int32_t> scanCodeStates;
451 KeyedVector<int32_t, int32_t> switchStates;
452 KeyedVector<int32_t, int32_t> absoluteAxisValue;
453 KeyedVector<int32_t, KeyInfo> keysByScanCode;
454 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
455 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100456 // fake mapping which would normally come from keyCharacterMap
457 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700458 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
459 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800460 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700461 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000462 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700463
464 status_t enable() {
465 enabled = true;
466 return OK;
467 }
468
469 status_t disable() {
470 enabled = false;
471 return OK;
472 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700474 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 };
476
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700477 std::mutex mLock;
478 std::condition_variable mEventsCondition;
479
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100481 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000482 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600483 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000484 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800485 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
486 // Simulates a device light brightness, from light id to light brightness.
487 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
488 // Simulates a device light intensities, from light id to light intensities map.
489 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
490 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700492public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493 virtual ~FakeEventHub() {
494 for (size_t i = 0; i < mDevices.size(); i++) {
495 delete mDevices.valueAt(i);
496 }
497 }
498
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 FakeEventHub() { }
500
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700501 void addDevice(int32_t deviceId, const std::string& name,
502 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 Device* device = new Device(classes);
504 device->identifier.name = name;
505 mDevices.add(deviceId, device);
506
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000507 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508 }
509
510 void removeDevice(int32_t deviceId) {
511 delete mDevices.valueFor(deviceId);
512 mDevices.removeItem(deviceId);
513
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000514 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515 }
516
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000517 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700518 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700519 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700520 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
521 return false;
522 }
523 return device->enabled;
524 }
525
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000526 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700527 status_t result;
528 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700529 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700530 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
531 return BAD_VALUE;
532 }
533 if (device->enabled) {
534 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
535 return OK;
536 }
537 result = device->enable();
538 return result;
539 }
540
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000541 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700542 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700543 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700544 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
545 return BAD_VALUE;
546 }
547 if (!device->enabled) {
548 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
549 return OK;
550 }
551 return device->disable();
552 }
553
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000555 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556 }
557
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700558 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 Device* device = getDevice(deviceId);
560 device->configuration.addProperty(key, value);
561 }
562
563 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
564 Device* device = getDevice(deviceId);
565 device->configuration.addAll(configuration);
566 }
567
568 void addAbsoluteAxis(int32_t deviceId, int axis,
569 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
570 Device* device = getDevice(deviceId);
571
572 RawAbsoluteAxisInfo info;
573 info.valid = true;
574 info.minValue = minValue;
575 info.maxValue = maxValue;
576 info.flat = flat;
577 info.fuzz = fuzz;
578 info.resolution = resolution;
579 device->absoluteAxes.add(axis, info);
580 }
581
582 void addRelativeAxis(int32_t deviceId, int32_t axis) {
583 Device* device = getDevice(deviceId);
584 device->relativeAxes.add(axis, true);
585 }
586
587 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
588 Device* device = getDevice(deviceId);
589 device->keyCodeStates.replaceValueFor(keyCode, state);
590 }
591
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000592 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
593 Device* device = getDevice(deviceId);
594 device->countryCode = countryCode;
595 }
596
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
598 Device* device = getDevice(deviceId);
599 device->scanCodeStates.replaceValueFor(scanCode, state);
600 }
601
602 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
603 Device* device = getDevice(deviceId);
604 device->switchStates.replaceValueFor(switchCode, state);
605 }
606
607 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
608 Device* device = getDevice(deviceId);
609 device->absoluteAxisValue.replaceValueFor(axis, value);
610 }
611
612 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
613 int32_t keyCode, uint32_t flags) {
614 Device* device = getDevice(deviceId);
615 KeyInfo info;
616 info.keyCode = keyCode;
617 info.flags = flags;
618 if (scanCode) {
619 device->keysByScanCode.add(scanCode, info);
620 }
621 if (usageCode) {
622 device->keysByUsageCode.add(usageCode, info);
623 }
624 }
625
Philip Junker4af3b3d2021-12-14 10:36:55 +0100626 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
627 Device* device = getDevice(deviceId);
628 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
629 }
630
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631 void addLed(int32_t deviceId, int32_t led, bool initialState) {
632 Device* device = getDevice(deviceId);
633 device->leds.add(led, initialState);
634 }
635
Chris Yef59a2f42020-10-16 12:55:26 -0700636 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
637 int32_t sensorDataIndex) {
638 Device* device = getDevice(deviceId);
639 SensorInfo info;
640 info.sensorType = sensorType;
641 info.sensorDataIndex = sensorDataIndex;
642 device->sensorsByAbsCode.emplace(absCode, info);
643 }
644
645 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
646 Device* device = getDevice(deviceId);
647 typename BitArray<MSC_MAX>::Buffer buffer;
648 buffer[mscEvent / 32] = 1 << mscEvent % 32;
649 device->mscBitmask.loadFromBuffer(buffer);
650 }
651
Chris Ye3fdbfef2021-01-06 18:45:18 -0800652 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
653 mRawLightInfos.emplace(rawId, std::move(info));
654 }
655
656 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
657 mLightBrightness.emplace(rawId, brightness);
658 }
659
660 void fakeLightIntensities(int32_t rawId,
661 const std::unordered_map<LightColor, int32_t> intensities) {
662 mLightIntensities.emplace(rawId, std::move(intensities));
663 }
664
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665 bool getLedState(int32_t deviceId, int32_t led) {
666 Device* device = getDevice(deviceId);
667 return device->leds.valueFor(led);
668 }
669
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100670 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 return mExcludedDevices;
672 }
673
674 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
675 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800676 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 }
678
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000679 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
680 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700681 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 RawEvent event;
683 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000684 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 event.deviceId = deviceId;
686 event.type = type;
687 event.code = code;
688 event.value = value;
689 mEvents.push_back(event);
690
691 if (type == EV_ABS) {
692 setAbsoluteAxisValue(deviceId, code, value);
693 }
694 }
695
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600696 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
697 std::vector<TouchVideoFrame>> videoFrames) {
698 mVideoFrames = std::move(videoFrames);
699 }
700
Michael Wrightd02c5b62014-02-10 15:10:22 -0800701 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700702 std::unique_lock<std::mutex> lock(mLock);
703 base::ScopedLockAssertion assumeLocked(mLock);
704 const bool queueIsEmpty =
705 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
706 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
707 if (!queueIsEmpty) {
708 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
709 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 }
711
712private:
713 Device* getDevice(int32_t deviceId) const {
714 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100715 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 }
717
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700718 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700720 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 }
722
Chris Yea52ade12020-08-27 16:49:20 -0700723 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 Device* device = getDevice(deviceId);
725 return device ? device->identifier : InputDeviceIdentifier();
726 }
727
Chris Yea52ade12020-08-27 16:49:20 -0700728 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729
Chris Yea52ade12020-08-27 16:49:20 -0700730 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 Device* device = getDevice(deviceId);
732 if (device) {
733 *outConfiguration = device->configuration;
734 }
735 }
736
Chris Yea52ade12020-08-27 16:49:20 -0700737 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
738 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800740 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741 ssize_t index = device->absoluteAxes.indexOfKey(axis);
742 if (index >= 0) {
743 *outAxisInfo = device->absoluteAxes.valueAt(index);
744 return OK;
745 }
746 }
747 outAxisInfo->clear();
748 return -1;
749 }
750
Chris Yea52ade12020-08-27 16:49:20 -0700751 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 Device* device = getDevice(deviceId);
753 if (device) {
754 return device->relativeAxes.indexOfKey(axis) >= 0;
755 }
756 return false;
757 }
758
Chris Yea52ade12020-08-27 16:49:20 -0700759 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800760
Chris Yef59a2f42020-10-16 12:55:26 -0700761 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
762 Device* device = getDevice(deviceId);
763 if (device) {
764 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
765 }
766 return false;
767 }
768
Chris Yea52ade12020-08-27 16:49:20 -0700769 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
770 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771 Device* device = getDevice(deviceId);
772 if (device) {
773 const KeyInfo* key = getKey(device, scanCode, usageCode);
774 if (key) {
775 if (outKeycode) {
776 *outKeycode = key->keyCode;
777 }
778 if (outFlags) {
779 *outFlags = key->flags;
780 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700781 if (outMetaState) {
782 *outMetaState = metaState;
783 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784 return OK;
785 }
786 }
787 return NAME_NOT_FOUND;
788 }
789
790 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
791 if (usageCode) {
792 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
793 if (index >= 0) {
794 return &device->keysByUsageCode.valueAt(index);
795 }
796 }
797 if (scanCode) {
798 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
799 if (index >= 0) {
800 return &device->keysByScanCode.valueAt(index);
801 }
802 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700803 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804 }
805
Chris Yea52ade12020-08-27 16:49:20 -0700806 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000808 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
809 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700810 Device* device = getDevice(deviceId);
811 if (!device) {
812 return Errorf("Sensor device not found.");
813 }
814 auto it = device->sensorsByAbsCode.find(absCode);
815 if (it == device->sensorsByAbsCode.end()) {
816 return Errorf("Sensor map not found.");
817 }
818 const SensorInfo& info = it->second;
819 return std::make_pair(info.sensorType, info.sensorDataIndex);
820 }
821
Chris Yea52ade12020-08-27 16:49:20 -0700822 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 mExcludedDevices = devices;
824 }
825
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000826 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
827 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000829 const size_t filledSize = std::min(mEvents.size(), bufferSize);
830 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
831
832 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700833 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000834 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835 }
836
Chris Yea52ade12020-08-27 16:49:20 -0700837 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600838 auto it = mVideoFrames.find(deviceId);
839 if (it != mVideoFrames.end()) {
840 std::vector<TouchVideoFrame> frames = std::move(it->second);
841 mVideoFrames.erase(deviceId);
842 return frames;
843 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800844 return {};
845 }
846
Chris Yea52ade12020-08-27 16:49:20 -0700847 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848 Device* device = getDevice(deviceId);
849 if (device) {
850 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
851 if (index >= 0) {
852 return device->scanCodeStates.valueAt(index);
853 }
854 }
855 return AKEY_STATE_UNKNOWN;
856 }
857
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000858 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
859 Device* device = getDevice(deviceId);
860 if (device) {
861 return device->countryCode;
862 }
863 return InputDeviceCountryCode::INVALID;
864 }
865
Chris Yea52ade12020-08-27 16:49:20 -0700866 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867 Device* device = getDevice(deviceId);
868 if (device) {
869 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
870 if (index >= 0) {
871 return device->keyCodeStates.valueAt(index);
872 }
873 }
874 return AKEY_STATE_UNKNOWN;
875 }
876
Chris Yea52ade12020-08-27 16:49:20 -0700877 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878 Device* device = getDevice(deviceId);
879 if (device) {
880 ssize_t index = device->switchStates.indexOfKey(sw);
881 if (index >= 0) {
882 return device->switchStates.valueAt(index);
883 }
884 }
885 return AKEY_STATE_UNKNOWN;
886 }
887
Chris Yea52ade12020-08-27 16:49:20 -0700888 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
889 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890 Device* device = getDevice(deviceId);
891 if (device) {
892 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
893 if (index >= 0) {
894 *outValue = device->absoluteAxisValue.valueAt(index);
895 return OK;
896 }
897 }
898 *outValue = 0;
899 return -1;
900 }
901
Philip Junker4af3b3d2021-12-14 10:36:55 +0100902 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
903 Device* device = getDevice(deviceId);
904 if (!device) {
905 return AKEYCODE_UNKNOWN;
906 }
907 auto it = device->keyCodeMapping.find(locationKeyCode);
908 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
909 }
910
Chris Yea52ade12020-08-27 16:49:20 -0700911 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700912 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700913 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914 bool result = false;
915 Device* device = getDevice(deviceId);
916 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700917 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700918 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800919 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
920 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
921 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922 }
923 }
924 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
925 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
926 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 }
928 }
929 }
930 }
931 return result;
932 }
933
Chris Yea52ade12020-08-27 16:49:20 -0700934 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 Device* device = getDevice(deviceId);
936 if (device) {
937 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
938 return index >= 0;
939 }
940 return false;
941 }
942
Arthur Hungcb40a002021-08-03 14:31:01 +0000943 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
944 Device* device = getDevice(deviceId);
945 if (!device) {
946 return false;
947 }
948 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
949 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
950 return true;
951 }
952 }
953 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
954 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
955 return true;
956 }
957 }
958 return false;
959 }
960
Chris Yea52ade12020-08-27 16:49:20 -0700961 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800962 Device* device = getDevice(deviceId);
963 return device && device->leds.indexOfKey(led) >= 0;
964 }
965
Chris Yea52ade12020-08-27 16:49:20 -0700966 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967 Device* device = getDevice(deviceId);
968 if (device) {
969 ssize_t index = device->leds.indexOfKey(led);
970 if (index >= 0) {
971 device->leds.replaceValueAt(led, on);
972 } else {
973 ADD_FAILURE()
974 << "Attempted to set the state of an LED that the EventHub declared "
975 "was not present. led=" << led;
976 }
977 }
978 }
979
Chris Yea52ade12020-08-27 16:49:20 -0700980 void getVirtualKeyDefinitions(
981 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 outVirtualKeys.clear();
983
984 Device* device = getDevice(deviceId);
985 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800986 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987 }
988 }
989
Chris Yea52ade12020-08-27 16:49:20 -0700990 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700991 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992 }
993
Chris Yea52ade12020-08-27 16:49:20 -0700994 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995 return false;
996 }
997
Chris Yea52ade12020-08-27 16:49:20 -0700998 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999
Chris Yea52ade12020-08-27 16:49:20 -07001000 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001002 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001003
Chris Yee2b1e5c2021-03-10 22:45:12 -08001004 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1005 return BATTERY_CAPACITY;
1006 }
Kim Low03ea0352020-11-06 12:45:07 -08001007
Chris Yee2b1e5c2021-03-10 22:45:12 -08001008 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1009 return BATTERY_STATUS;
1010 }
1011
Andy Chenf9f1a022022-08-29 20:07:10 -04001012 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1013 return {DEFAULT_BATTERY};
1014 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001015
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001016 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1017 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001018 if (batteryId != DEFAULT_BATTERY) return {};
1019 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1020 .name = "default battery",
1021 .flags = InputBatteryClass::CAPACITY,
1022 .path = BATTERY_DEVPATH};
1023 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001024 }
Kim Low03ea0352020-11-06 12:45:07 -08001025
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001026 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001027 std::vector<int32_t> ids;
1028 for (const auto& [rawId, info] : mRawLightInfos) {
1029 ids.push_back(rawId);
1030 }
1031 return ids;
1032 }
1033
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001034 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001035 auto it = mRawLightInfos.find(lightId);
1036 if (it == mRawLightInfos.end()) {
1037 return std::nullopt;
1038 }
1039 return it->second;
1040 }
1041
1042 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1043 mLightBrightness.emplace(lightId, brightness);
1044 }
1045
1046 void setLightIntensities(int32_t deviceId, int32_t lightId,
1047 std::unordered_map<LightColor, int32_t> intensities) override {
1048 mLightIntensities.emplace(lightId, intensities);
1049 };
1050
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001051 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001052 auto lightIt = mLightBrightness.find(lightId);
1053 if (lightIt == mLightBrightness.end()) {
1054 return std::nullopt;
1055 }
1056 return lightIt->second;
1057 }
1058
1059 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001060 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001061 auto lightIt = mLightIntensities.find(lightId);
1062 if (lightIt == mLightIntensities.end()) {
1063 return std::nullopt;
1064 }
1065 return lightIt->second;
1066 };
1067
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001068 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001070 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071
Chris Yea52ade12020-08-27 16:49:20 -07001072 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073
Chris Yea52ade12020-08-27 16:49:20 -07001074 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075};
1076
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077// --- FakeInputMapper ---
1078
1079class FakeInputMapper : public InputMapper {
1080 uint32_t mSources;
1081 int32_t mKeyboardType;
1082 int32_t mMetaState;
1083 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1084 KeyedVector<int32_t, int32_t> mScanCodeStates;
1085 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001086 // fake mapping which would normally come from keyCharacterMap
1087 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001088 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001090 std::mutex mLock;
1091 std::condition_variable mStateChangedCondition;
1092 bool mConfigureWasCalled GUARDED_BY(mLock);
1093 bool mResetWasCalled GUARDED_BY(mLock);
1094 bool mProcessWasCalled GUARDED_BY(mLock);
1095 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001096
Arthur Hungc23540e2018-11-29 20:42:11 +08001097 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001099 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1100 : InputMapper(deviceContext),
1101 mSources(sources),
1102 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001104 mConfigureWasCalled(false),
1105 mResetWasCalled(false),
1106 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107
Chris Yea52ade12020-08-27 16:49:20 -07001108 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
1110 void setKeyboardType(int32_t keyboardType) {
1111 mKeyboardType = keyboardType;
1112 }
1113
1114 void setMetaState(int32_t metaState) {
1115 mMetaState = metaState;
1116 }
1117
1118 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001119 std::unique_lock<std::mutex> lock(mLock);
1120 base::ScopedLockAssertion assumeLocked(mLock);
1121 const bool configureCalled =
1122 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1123 return mConfigureWasCalled;
1124 });
1125 if (!configureCalled) {
1126 FAIL() << "Expected configure() to have been called.";
1127 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 mConfigureWasCalled = false;
1129 }
1130
1131 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001132 std::unique_lock<std::mutex> lock(mLock);
1133 base::ScopedLockAssertion assumeLocked(mLock);
1134 const bool resetCalled =
1135 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1136 return mResetWasCalled;
1137 });
1138 if (!resetCalled) {
1139 FAIL() << "Expected reset() to have been called.";
1140 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141 mResetWasCalled = false;
1142 }
1143
Yi Kong9b14ac62018-07-17 13:48:38 -07001144 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001145 std::unique_lock<std::mutex> lock(mLock);
1146 base::ScopedLockAssertion assumeLocked(mLock);
1147 const bool processCalled =
1148 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1149 return mProcessWasCalled;
1150 });
1151 if (!processCalled) {
1152 FAIL() << "Expected process() to have been called.";
1153 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154 if (outLastEvent) {
1155 *outLastEvent = mLastEvent;
1156 }
1157 mProcessWasCalled = false;
1158 }
1159
1160 void setKeyCodeState(int32_t keyCode, int32_t state) {
1161 mKeyCodeStates.replaceValueFor(keyCode, state);
1162 }
1163
1164 void setScanCodeState(int32_t scanCode, int32_t state) {
1165 mScanCodeStates.replaceValueFor(scanCode, state);
1166 }
1167
1168 void setSwitchState(int32_t switchCode, int32_t state) {
1169 mSwitchStates.replaceValueFor(switchCode, state);
1170 }
1171
1172 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001173 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174 }
1175
Philip Junker4af3b3d2021-12-14 10:36:55 +01001176 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1177 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1178 }
1179
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001181 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182
Chris Yea52ade12020-08-27 16:49:20 -07001183 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 InputMapper::populateDeviceInfo(deviceInfo);
1185
1186 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1187 deviceInfo->setKeyboardType(mKeyboardType);
1188 }
1189 }
1190
Chris Yea52ade12020-08-27 16:49:20 -07001191 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001192 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001194
1195 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001196 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001197 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1198 mViewport = config->getDisplayViewportByPort(*displayPort);
1199 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001200
1201 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 }
1203
Chris Yea52ade12020-08-27 16:49:20 -07001204 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001205 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001207 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 }
1209
Chris Yea52ade12020-08-27 16:49:20 -07001210 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001211 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001212 mLastEvent = *rawEvent;
1213 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001214 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 }
1216
Chris Yea52ade12020-08-27 16:49:20 -07001217 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1219 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1220 }
1221
Philip Junker4af3b3d2021-12-14 10:36:55 +01001222 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1223 auto it = mKeyCodeMapping.find(locationKeyCode);
1224 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1225 }
1226
Chris Yea52ade12020-08-27 16:49:20 -07001227 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001228 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1229 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1230 }
1231
Chris Yea52ade12020-08-27 16:49:20 -07001232 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001233 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1234 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1235 }
1236
Chris Yea52ade12020-08-27 16:49:20 -07001237 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001238 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001239 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001240 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1242 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1243 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244 }
1245 }
1246 }
Chris Yea52ade12020-08-27 16:49:20 -07001247 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001248 return result;
1249 }
1250
1251 virtual int32_t getMetaState() {
1252 return mMetaState;
1253 }
1254
1255 virtual void fadePointer() {
1256 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001257
1258 virtual std::optional<int32_t> getAssociatedDisplay() {
1259 if (mViewport) {
1260 return std::make_optional(mViewport->displayId);
1261 }
1262 return std::nullopt;
1263 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001264};
1265
1266
1267// --- InstrumentedInputReader ---
1268
1269class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001270 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001271
1272public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001273 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1274 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001275 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001276 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001278 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001280 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001282 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001283 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001284 InputDeviceIdentifier identifier;
1285 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001286 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001287 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001288 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001289 }
1290
Prabir Pradhan28efc192019-11-05 01:10:04 +00001291 // Make the protected loopOnce method accessible to tests.
1292 using InputReader::loopOnce;
1293
Michael Wrightd02c5b62014-02-10 15:10:22 -08001294protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001295 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1296 const InputDeviceIdentifier& identifier)
1297 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001298 if (!mNextDevices.empty()) {
1299 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1300 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001301 return device;
1302 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001303 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001304 }
1305
arthurhungdcef2dc2020-08-11 14:47:50 +08001306 // --- FakeInputReaderContext ---
1307 class FakeInputReaderContext : public ContextImpl {
1308 int32_t mGlobalMetaState;
1309 bool mUpdateGlobalMetaStateWasCalled;
1310 int32_t mGeneration;
1311
1312 public:
1313 FakeInputReaderContext(InputReader* reader)
1314 : ContextImpl(reader),
1315 mGlobalMetaState(0),
1316 mUpdateGlobalMetaStateWasCalled(false),
1317 mGeneration(1) {}
1318
1319 virtual ~FakeInputReaderContext() {}
1320
1321 void assertUpdateGlobalMetaStateWasCalled() {
1322 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1323 << "Expected updateGlobalMetaState() to have been called.";
1324 mUpdateGlobalMetaStateWasCalled = false;
1325 }
1326
1327 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1328
1329 uint32_t getGeneration() { return mGeneration; }
1330
1331 void updateGlobalMetaState() override {
1332 mUpdateGlobalMetaStateWasCalled = true;
1333 ContextImpl::updateGlobalMetaState();
1334 }
1335
1336 int32_t getGlobalMetaState() override {
1337 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1338 }
1339
1340 int32_t bumpGeneration() override {
1341 mGeneration = ContextImpl::bumpGeneration();
1342 return mGeneration;
1343 }
1344 } mFakeContext;
1345
Michael Wrightd02c5b62014-02-10 15:10:22 -08001346 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001347
1348public:
1349 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001350};
1351
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352// --- InputReaderPolicyTest ---
1353class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001354protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355 sp<FakeInputReaderPolicy> mFakePolicy;
1356
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001357 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001358 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359};
1360
1361/**
1362 * Check that empty set of viewports is an acceptable configuration.
1363 * Also try to get internal viewport two different ways - by type and by uniqueId.
1364 *
1365 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1366 * Such configuration is not currently allowed.
1367 */
1368TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001369 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370
1371 // We didn't add any viewports yet, so there shouldn't be any.
1372 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001373 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374 ASSERT_FALSE(internalViewport);
1375
1376 // Add an internal viewport, then clear it
1377 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001378 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001379 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001380
1381 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001382 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001383 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001384 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001385
1386 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001387 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001389 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390
1391 mFakePolicy->clearViewports();
1392 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001393 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001394 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001395 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396 ASSERT_FALSE(internalViewport);
1397}
1398
1399TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1400 const std::string internalUniqueId = "local:0";
1401 const std::string externalUniqueId = "local:1";
1402 const std::string virtualUniqueId1 = "virtual:2";
1403 const std::string virtualUniqueId2 = "virtual:3";
1404 constexpr int32_t virtualDisplayId1 = 2;
1405 constexpr int32_t virtualDisplayId2 = 3;
1406
1407 // Add an internal viewport
1408 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001409 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1410 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411 // Add an external viewport
1412 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001413 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1414 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 // Add an virtual viewport
1416 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001417 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1418 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419 // Add another virtual viewport
1420 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001421 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1422 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001423
1424 // Check matching by type for internal
1425 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001426 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_TRUE(internalViewport);
1428 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1429
1430 // Check matching by type for external
1431 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001432 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001433 ASSERT_TRUE(externalViewport);
1434 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1435
1436 // Check matching by uniqueId for virtual viewport #1
1437 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001438 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001439 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001440 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1442 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1443
1444 // Check matching by uniqueId for virtual viewport #2
1445 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001446 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001447 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001448 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001449 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1450 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1451}
1452
1453
1454/**
1455 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1456 * that lookup works by checking display id.
1457 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1458 */
1459TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1460 const std::string uniqueId1 = "uniqueId1";
1461 const std::string uniqueId2 = "uniqueId2";
1462 constexpr int32_t displayId1 = 2;
1463 constexpr int32_t displayId2 = 3;
1464
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001465 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1466 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001467 for (const ViewportType& type : types) {
1468 mFakePolicy->clearViewports();
1469 // Add a viewport
1470 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001471 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1472 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001473 // Add another viewport
1474 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001475 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1476 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001477
1478 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001479 std::optional<DisplayViewport> viewport1 =
1480 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001481 ASSERT_TRUE(viewport1);
1482 ASSERT_EQ(displayId1, viewport1->displayId);
1483 ASSERT_EQ(type, viewport1->type);
1484
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001485 std::optional<DisplayViewport> viewport2 =
1486 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001487 ASSERT_TRUE(viewport2);
1488 ASSERT_EQ(displayId2, viewport2->displayId);
1489 ASSERT_EQ(type, viewport2->type);
1490
1491 // When there are multiple viewports of the same kind, and uniqueId is not specified
1492 // in the call to getDisplayViewport, then that situation is not supported.
1493 // The viewports can be stored in any order, so we cannot rely on the order, since that
1494 // is just implementation detail.
1495 // However, we can check that it still returns *a* viewport, we just cannot assert
1496 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001497 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001498 ASSERT_TRUE(someViewport);
1499 }
1500}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001501
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001502/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001503 * When we have multiple internal displays make sure we always return the default display when
1504 * querying by type.
1505 */
1506TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1507 const std::string uniqueId1 = "uniqueId1";
1508 const std::string uniqueId2 = "uniqueId2";
1509 constexpr int32_t nonDefaultDisplayId = 2;
1510 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1511 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1512
1513 // Add the default display first and ensure it gets returned.
1514 mFakePolicy->clearViewports();
1515 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001516 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001517 ViewportType::INTERNAL);
1518 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001519 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001520 ViewportType::INTERNAL);
1521
1522 std::optional<DisplayViewport> viewport =
1523 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1524 ASSERT_TRUE(viewport);
1525 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1526 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1527
1528 // Add the default display second to make sure order doesn't matter.
1529 mFakePolicy->clearViewports();
1530 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001531 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001532 ViewportType::INTERNAL);
1533 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001534 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001535 ViewportType::INTERNAL);
1536
1537 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1538 ASSERT_TRUE(viewport);
1539 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1540 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1541}
1542
1543/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001544 * Check getDisplayViewportByPort
1545 */
1546TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001547 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001548 const std::string uniqueId1 = "uniqueId1";
1549 const std::string uniqueId2 = "uniqueId2";
1550 constexpr int32_t displayId1 = 1;
1551 constexpr int32_t displayId2 = 2;
1552 const uint8_t hdmi1 = 0;
1553 const uint8_t hdmi2 = 1;
1554 const uint8_t hdmi3 = 2;
1555
1556 mFakePolicy->clearViewports();
1557 // Add a viewport that's associated with some display port that's not of interest.
1558 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001559 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1560 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001561 // Add another viewport, connected to HDMI1 port
1562 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001563 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1564 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001565
1566 // Check that correct display viewport was returned by comparing the display ports.
1567 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1568 ASSERT_TRUE(hdmi1Viewport);
1569 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1570 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1571
1572 // Check that we can still get the same viewport using the uniqueId
1573 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1574 ASSERT_TRUE(hdmi1Viewport);
1575 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1576 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1577 ASSERT_EQ(type, hdmi1Viewport->type);
1578
1579 // Check that we cannot find a port with "HDMI2", because we never added one
1580 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1581 ASSERT_FALSE(hdmi2Viewport);
1582}
1583
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584// --- InputReaderTest ---
1585
1586class InputReaderTest : public testing::Test {
1587protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001588 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001589 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001590 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001591 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592
Chris Yea52ade12020-08-27 16:49:20 -07001593 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001594 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001595 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001596 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001597
Prabir Pradhan28efc192019-11-05 01:10:04 +00001598 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001599 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001600 }
1601
Chris Yea52ade12020-08-27 16:49:20 -07001602 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001603 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001605 }
1606
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001607 void addDevice(int32_t eventHubId, const std::string& name,
1608 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001609 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001610
1611 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001612 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613 }
1614 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001615 mReader->loopOnce();
1616 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001617 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1618 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619 }
1620
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001621 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001622 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001623 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001624 }
1625
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001626 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001627 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001628 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001629 }
1630
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001631 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001632 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001633 ftl::Flags<InputDeviceClass> classes,
1634 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001635 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001636 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1637 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001638 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640 return mapper;
1641 }
1642};
1643
Chris Ye98d3f532020-10-01 21:48:59 -07001644TEST_F(InputReaderTest, PolicyGetInputDevices) {
1645 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001646 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001647 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648
1649 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001650 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001652 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001653 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1655 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001656 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657}
1658
Chris Yee7310032020-09-22 15:36:28 -07001659TEST_F(InputReaderTest, GetMergedInputDevices) {
1660 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1661 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1662 // Add two subdevices to device
1663 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1664 // Must add at least one mapper or the device will be ignored!
1665 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1666 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1667
1668 // Push same device instance for next device to be added, so they'll have same identifier.
1669 mReader->pushNextDevice(device);
1670 mReader->pushNextDevice(device);
1671 ASSERT_NO_FATAL_FAILURE(
1672 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1673 ASSERT_NO_FATAL_FAILURE(
1674 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1675
1676 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001677 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001678}
1679
Chris Yee14523a2020-12-19 13:46:00 -08001680TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1681 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1682 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1683 // Add two subdevices to device
1684 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1685 // Must add at least one mapper or the device will be ignored!
1686 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1687 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1688
1689 // Push same device instance for next device to be added, so they'll have same identifier.
1690 mReader->pushNextDevice(device);
1691 mReader->pushNextDevice(device);
1692 // Sensor device is initially disabled
1693 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1694 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1695 nullptr));
1696 // Device is disabled because the only sub device is a sensor device and disabled initially.
1697 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1698 ASSERT_FALSE(device->isEnabled());
1699 ASSERT_NO_FATAL_FAILURE(
1700 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1701 // The merged device is enabled if any sub device is enabled
1702 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1703 ASSERT_TRUE(device->isEnabled());
1704}
1705
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001706TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001708 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 constexpr int32_t eventHubId = 1;
1710 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001711 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001713 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001714 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001715
Yi Kong9b14ac62018-07-17 13:48:38 -07001716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717
1718 NotifyDeviceResetArgs resetArgs;
1719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001720 ASSERT_EQ(deviceId, resetArgs.deviceId);
1721
1722 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001723 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001724 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001725
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001727 ASSERT_EQ(deviceId, resetArgs.deviceId);
1728 ASSERT_EQ(device->isEnabled(), false);
1729
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001730 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001731 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001734 ASSERT_EQ(device->isEnabled(), false);
1735
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001736 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001737 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001739 ASSERT_EQ(deviceId, resetArgs.deviceId);
1740 ASSERT_EQ(device->isEnabled(), true);
1741}
1742
Michael Wrightd02c5b62014-02-10 15:10:22 -08001743TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001744 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001745 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001746 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001747 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001748 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001749 AINPUT_SOURCE_KEYBOARD, nullptr);
1750 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001751
1752 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1753 AINPUT_SOURCE_ANY, AKEYCODE_A))
1754 << "Should return unknown when the device id is >= 0 but unknown.";
1755
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001756 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1757 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1758 << "Should return unknown when the device id is valid but the sources are not "
1759 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001760
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001761 ASSERT_EQ(AKEY_STATE_DOWN,
1762 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1763 AKEYCODE_A))
1764 << "Should return value provided by mapper when device id is valid and the device "
1765 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001766
1767 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1768 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1769 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1770
1771 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1772 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1773 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1774}
1775
Philip Junker4af3b3d2021-12-14 10:36:55 +01001776TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1777 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1778 constexpr int32_t eventHubId = 1;
1779 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1780 InputDeviceClass::KEYBOARD,
1781 AINPUT_SOURCE_KEYBOARD, nullptr);
1782 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1783
1784 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1785 << "Should return unknown when the device with the specified id is not found.";
1786
1787 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1788 << "Should return correct mapping when device id is valid and mapping exists.";
1789
1790 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1791 << "Should return the location key code when device id is valid and there's no "
1792 "mapping.";
1793}
1794
1795TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1796 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1797 constexpr int32_t eventHubId = 1;
1798 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1799 InputDeviceClass::JOYSTICK,
1800 AINPUT_SOURCE_GAMEPAD, nullptr);
1801 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1802
1803 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1804 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1805}
1806
Michael Wrightd02c5b62014-02-10 15:10:22 -08001807TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001809 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001811 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001813 AINPUT_SOURCE_KEYBOARD, nullptr);
1814 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001815
1816 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1817 AINPUT_SOURCE_ANY, KEY_A))
1818 << "Should return unknown when the device id is >= 0 but unknown.";
1819
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001820 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1821 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1822 << "Should return unknown when the device id is valid but the sources are not "
1823 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001825 ASSERT_EQ(AKEY_STATE_DOWN,
1826 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1827 KEY_A))
1828 << "Should return value provided by mapper when device id is valid and the device "
1829 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001830
1831 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1832 AINPUT_SOURCE_TRACKBALL, KEY_A))
1833 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1834
1835 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1836 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1837 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1838}
1839
1840TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001841 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001842 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001843 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001844 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001845 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001846 AINPUT_SOURCE_KEYBOARD, nullptr);
1847 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001848
1849 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1850 AINPUT_SOURCE_ANY, SW_LID))
1851 << "Should return unknown when the device id is >= 0 but unknown.";
1852
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001853 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1854 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1855 << "Should return unknown when the device id is valid but the sources are not "
1856 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001858 ASSERT_EQ(AKEY_STATE_DOWN,
1859 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1860 SW_LID))
1861 << "Should return value provided by mapper when device id is valid and the device "
1862 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863
1864 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1865 AINPUT_SOURCE_TRACKBALL, SW_LID))
1866 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1867
1868 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1869 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1870 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1871}
1872
1873TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001874 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001875 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001876 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001877 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001878 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001879 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001880
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001881 mapper.addSupportedKeyCode(AKEYCODE_A);
1882 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001883
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001884 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001885 uint8_t flags[4] = { 0, 0, 0, 1 };
1886
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001887 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001888 << "Should return false when device id is >= 0 but unknown.";
1889 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1890
1891 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001892 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001893 << "Should return false when device id is valid but the sources are not supported by "
1894 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001895 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1896
1897 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001898 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001899 keyCodes, flags))
1900 << "Should return value provided by mapper when device id is valid and the device "
1901 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001902 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1903
1904 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001905 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1906 << "Should return false when the device id is < 0 but the sources are not supported by "
1907 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1909
1910 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001911 ASSERT_TRUE(
1912 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1913 << "Should return value provided by mapper when device id is < 0 and one of the "
1914 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1916}
1917
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001918TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001919 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001920 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001921
1922 NotifyConfigurationChangedArgs args;
1923
1924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1925 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1926}
1927
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001928TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001929 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001930 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001931 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001932 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001933 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001934 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001935 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001936 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001937
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001938 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001939 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001940 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1941
1942 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001943 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001944 ASSERT_EQ(when, event.when);
1945 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001946 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001947 ASSERT_EQ(EV_KEY, event.type);
1948 ASSERT_EQ(KEY_A, event.code);
1949 ASSERT_EQ(1, event.value);
1950}
1951
Garfield Tan1c7bc862020-01-28 13:24:04 -08001952TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001954 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001955 constexpr int32_t eventHubId = 1;
1956 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001957 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001958 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001959 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001960 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001961
1962 NotifyDeviceResetArgs resetArgs;
1963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001964 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001966 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001967 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001969 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001970 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001972 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001973 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001975 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001976 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001978 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001979 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001981 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001982 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001983}
1984
Garfield Tan1c7bc862020-01-28 13:24:04 -08001985TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1986 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001987 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001988 constexpr int32_t eventHubId = 1;
1989 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1990 // Must add at least one mapper or the device will be ignored!
1991 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001992 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001993 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1994
1995 NotifyDeviceResetArgs resetArgs;
1996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1997 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1998}
1999
Arthur Hungc23540e2018-11-29 20:42:11 +08002000TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002001 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002002 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002003 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002004 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002005 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2006 FakeInputMapper& mapper =
2007 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002008 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002009
2010 const uint8_t hdmi1 = 1;
2011
2012 // Associated touch screen with second display.
2013 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2014
2015 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002016 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002017 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002018 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002019 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002020 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002021 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002022 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002023 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002024 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002025
2026 // Add the device, and make sure all of the callbacks are triggered.
2027 // The device is added after the input port associations are processed since
2028 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002029 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002032 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002033
Arthur Hung2c9a3342019-07-23 14:18:59 +08002034 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002035 ASSERT_EQ(deviceId, device->getId());
2036 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2037 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002038
2039 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002040 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002041 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002042 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002043}
2044
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002045TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2046 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002047 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002048 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2049 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2050 // Must add at least one mapper or the device will be ignored!
2051 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2052 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2053 mReader->pushNextDevice(device);
2054 mReader->pushNextDevice(device);
2055 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2056 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2057
2058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2059
2060 NotifyDeviceResetArgs resetArgs;
2061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2062 ASSERT_EQ(deviceId, resetArgs.deviceId);
2063 ASSERT_TRUE(device->isEnabled());
2064 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2065 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2066
2067 disableDevice(deviceId);
2068 mReader->loopOnce();
2069
2070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2071 ASSERT_EQ(deviceId, resetArgs.deviceId);
2072 ASSERT_FALSE(device->isEnabled());
2073 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2074 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2075
2076 enableDevice(deviceId);
2077 mReader->loopOnce();
2078
2079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2080 ASSERT_EQ(deviceId, resetArgs.deviceId);
2081 ASSERT_TRUE(device->isEnabled());
2082 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2083 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2084}
2085
2086TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2087 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002088 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002089 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2090 // Add two subdevices to device
2091 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2092 FakeInputMapper& mapperDevice1 =
2093 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2094 FakeInputMapper& mapperDevice2 =
2095 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2096 mReader->pushNextDevice(device);
2097 mReader->pushNextDevice(device);
2098 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2099 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2100
2101 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2102 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2103
2104 ASSERT_EQ(AKEY_STATE_DOWN,
2105 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2106 ASSERT_EQ(AKEY_STATE_DOWN,
2107 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2108 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2109 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2110}
2111
Prabir Pradhan7e186182020-11-10 13:56:45 -08002112TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2113 NotifyPointerCaptureChangedArgs args;
2114
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002115 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002116 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2117 mReader->loopOnce();
2118 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002119 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2120 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002121
2122 mFakePolicy->setPointerCapture(false);
2123 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2124 mReader->loopOnce();
2125 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002126 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002127
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002128 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002129 // does not change.
2130 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2131 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002132 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002133}
2134
Chris Ye87143712020-11-10 05:05:58 +00002135class FakeVibratorInputMapper : public FakeInputMapper {
2136public:
2137 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2138 : FakeInputMapper(deviceContext, sources) {}
2139
2140 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2141};
2142
2143TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2144 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002145 ftl::Flags<InputDeviceClass> deviceClass =
2146 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002147 constexpr int32_t eventHubId = 1;
2148 const char* DEVICE_LOCATION = "BLUETOOTH";
2149 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2150 FakeVibratorInputMapper& mapper =
2151 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2152 mReader->pushNextDevice(device);
2153
2154 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2155 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2156
2157 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2158 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2159}
2160
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002161// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002162
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002163class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002164public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002165 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002166
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002167 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002168
Andy Chenf9f1a022022-08-29 20:07:10 -04002169 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2170
Chris Yee2b1e5c2021-03-10 22:45:12 -08002171 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2172
2173 void dump(std::string& dump) override {}
2174
2175 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2176 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002177 }
2178
Chris Yee2b1e5c2021-03-10 22:45:12 -08002179 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2180 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002181 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002182
2183 bool setLightColor(int32_t lightId, int32_t color) override {
2184 getDeviceContext().setLightBrightness(lightId, color >> 24);
2185 return true;
2186 }
2187
2188 std::optional<int32_t> getLightColor(int32_t lightId) override {
2189 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2190 if (!result.has_value()) {
2191 return std::nullopt;
2192 }
2193 return result.value() << 24;
2194 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002195
2196 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2197
2198 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2199
2200private:
2201 InputDeviceContext& mDeviceContext;
2202 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2203 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002204 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002205};
2206
Chris Yee2b1e5c2021-03-10 22:45:12 -08002207TEST_F(InputReaderTest, BatteryGetCapacity) {
2208 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002209 ftl::Flags<InputDeviceClass> deviceClass =
2210 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002211 constexpr int32_t eventHubId = 1;
2212 const char* DEVICE_LOCATION = "BLUETOOTH";
2213 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002214 FakePeripheralController& controller =
2215 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002216 mReader->pushNextDevice(device);
2217
2218 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2219
2220 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2221 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2222}
2223
2224TEST_F(InputReaderTest, BatteryGetStatus) {
2225 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002226 ftl::Flags<InputDeviceClass> deviceClass =
2227 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002228 constexpr int32_t eventHubId = 1;
2229 const char* DEVICE_LOCATION = "BLUETOOTH";
2230 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002231 FakePeripheralController& controller =
2232 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002233 mReader->pushNextDevice(device);
2234
2235 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2236
2237 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2238 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2239}
2240
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002241TEST_F(InputReaderTest, BatteryGetDevicePath) {
2242 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2243 ftl::Flags<InputDeviceClass> deviceClass =
2244 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2245 constexpr int32_t eventHubId = 1;
2246 const char* DEVICE_LOCATION = "BLUETOOTH";
2247 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2248 device->addController<FakePeripheralController>(eventHubId);
2249 mReader->pushNextDevice(device);
2250
2251 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2252
2253 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2254}
2255
Chris Ye3fdbfef2021-01-06 18:45:18 -08002256TEST_F(InputReaderTest, LightGetColor) {
2257 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002258 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002259 constexpr int32_t eventHubId = 1;
2260 const char* DEVICE_LOCATION = "BLUETOOTH";
2261 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002262 FakePeripheralController& controller =
2263 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002264 mReader->pushNextDevice(device);
2265 RawLightInfo info = {.id = 1,
2266 .name = "Mono",
2267 .maxBrightness = 255,
2268 .flags = InputLightClass::BRIGHTNESS,
2269 .path = ""};
2270 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2271 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2272
2273 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002274
Chris Yee2b1e5c2021-03-10 22:45:12 -08002275 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2276 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002277 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2278 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2279}
2280
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002281// --- InputReaderIntegrationTest ---
2282
2283// These tests create and interact with the InputReader only through its interface.
2284// The InputReader is started during SetUp(), which starts its processing in its own
2285// thread. The tests use linux uinput to emulate input devices.
2286// NOTE: Interacting with the physical device while these tests are running may cause
2287// the tests to fail.
2288class InputReaderIntegrationTest : public testing::Test {
2289protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002290 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002291 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002292 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002293
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002294 std::shared_ptr<FakePointerController> mFakePointerController;
2295
Chris Yea52ade12020-08-27 16:49:20 -07002296 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002297 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002298 mFakePointerController = std::make_shared<FakePointerController>();
2299 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002300 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2301 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002302
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002303 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2304 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002305 ASSERT_EQ(mReader->start(), OK);
2306
2307 // Since this test is run on a real device, all the input devices connected
2308 // to the test device will show up in mReader. We wait for those input devices to
2309 // show up before beginning the tests.
2310 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2311 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2312 }
2313
Chris Yea52ade12020-08-27 16:49:20 -07002314 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002315 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002316 mReader.reset();
2317 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002318 mFakePolicy.clear();
2319 }
2320};
2321
2322TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2323 // An invalid input device that is only used for this test.
2324 class InvalidUinputDevice : public UinputDevice {
2325 public:
2326 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2327
2328 private:
2329 void configureDevice(int fd, uinput_user_dev* device) override {}
2330 };
2331
2332 const size_t numDevices = mFakePolicy->getInputDevices().size();
2333
2334 // UinputDevice does not set any event or key bits, so InputReader should not
2335 // consider it as a valid device.
2336 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2337 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2339 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2340
2341 invalidDevice.reset();
2342 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2343 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2344 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2345}
2346
2347TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2348 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2349
2350 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2351 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2353 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2354
2355 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002356 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002357 const auto& it =
2358 std::find_if(inputDevices.begin(), inputDevices.end(),
2359 [&keyboard](const InputDeviceInfo& info) {
2360 return info.getIdentifier().name == keyboard->getName();
2361 });
2362
2363 ASSERT_NE(it, inputDevices.end());
2364 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2365 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2366 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002367
2368 keyboard.reset();
2369 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2370 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2371 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2372}
2373
2374TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2375 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2376 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2377
2378 NotifyConfigurationChangedArgs configChangedArgs;
2379 ASSERT_NO_FATAL_FAILURE(
2380 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002381 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002382 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2383
2384 NotifyKeyArgs keyArgs;
2385 keyboard->pressAndReleaseHomeKey();
2386 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2387 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002388 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002389 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002390 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002391 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002392 prevTimestamp = keyArgs.eventTime;
2393
2394 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2395 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002396 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002397 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002398 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002399}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002401/**
2402 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2403 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2404 * are passed to the listener.
2405 */
2406static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2407TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2408 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2409 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2410 NotifyKeyArgs keyArgs;
2411
2412 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2414 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2415 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2416
2417 controller->pressAndReleaseKey(BTN_GEAR_UP);
2418 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2420 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2421}
2422
Arthur Hungaab25622020-01-16 11:22:11 +08002423// --- TouchProcessTest ---
2424class TouchIntegrationTest : public InputReaderIntegrationTest {
2425protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002426 const std::string UNIQUE_ID = "local:0";
2427
Chris Yea52ade12020-08-27 16:49:20 -07002428 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002429 InputReaderIntegrationTest::SetUp();
2430 // At least add an internal display.
2431 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2432 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002433 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002434
2435 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2436 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2438 }
2439
2440 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2441 int32_t orientation, const std::string& uniqueId,
2442 std::optional<uint8_t> physicalPort,
2443 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002444 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2445 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002446 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2447 }
2448
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002449 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2450 NotifyMotionArgs args;
2451 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2452 EXPECT_EQ(action, args.action);
2453 ASSERT_EQ(points.size(), args.pointerCount);
2454 for (size_t i = 0; i < args.pointerCount; i++) {
2455 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2456 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2457 }
2458 }
2459
Arthur Hungaab25622020-01-16 11:22:11 +08002460 std::unique_ptr<UinputTouchScreen> mDevice;
2461};
2462
2463TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2464 NotifyMotionArgs args;
2465 const Point centerPoint = mDevice->getCenterPoint();
2466
2467 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002468 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002469 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002470 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2472 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2473
2474 // ACTION_MOVE
2475 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2479
2480 // ACTION_UP
2481 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002482 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002483 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2484 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2485}
2486
2487TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2488 NotifyMotionArgs args;
2489 const Point centerPoint = mDevice->getCenterPoint();
2490
2491 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002492 mDevice->sendSlot(FIRST_SLOT);
2493 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002494 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002495 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2497 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2498
2499 // ACTION_POINTER_DOWN (Second slot)
2500 const Point secondPoint = centerPoint + Point(100, 100);
2501 mDevice->sendSlot(SECOND_SLOT);
2502 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002503 mDevice->sendDown(secondPoint);
2504 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002505 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002506 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002507
2508 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002509 mDevice->sendMove(secondPoint + Point(1, 1));
2510 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002511 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2513
2514 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002515 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002516 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002518 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002519
2520 // ACTION_UP
2521 mDevice->sendSlot(FIRST_SLOT);
2522 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002523 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002524 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2525 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2526}
2527
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002528/**
2529 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2530 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2531 * data?
2532 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2533 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2534 * for Pointer 0 only is generated after.
2535 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2536 * events, we will not miss any information.
2537 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2538 * event generated afterwards that contains the newest movement of pointer 0.
2539 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2540 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2541 * losing information about non-palm pointers.
2542 */
2543TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2544 NotifyMotionArgs args;
2545 const Point centerPoint = mDevice->getCenterPoint();
2546
2547 // ACTION_DOWN
2548 mDevice->sendSlot(FIRST_SLOT);
2549 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2550 mDevice->sendDown(centerPoint);
2551 mDevice->sendSync();
2552 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2553
2554 // ACTION_POINTER_DOWN (Second slot)
2555 const Point secondPoint = centerPoint + Point(100, 100);
2556 mDevice->sendSlot(SECOND_SLOT);
2557 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2558 mDevice->sendDown(secondPoint);
2559 mDevice->sendSync();
2560 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2561
2562 // ACTION_MOVE (First slot)
2563 mDevice->sendSlot(FIRST_SLOT);
2564 mDevice->sendMove(centerPoint + Point(5, 5));
2565 // ACTION_POINTER_UP (Second slot)
2566 mDevice->sendSlot(SECOND_SLOT);
2567 mDevice->sendPointerUp();
2568 // Send a single sync for the above 2 pointer updates
2569 mDevice->sendSync();
2570
2571 // First, we should get POINTER_UP for the second pointer
2572 assertReceivedMotion(ACTION_POINTER_1_UP,
2573 {/*first pointer */ centerPoint + Point(5, 5),
2574 /*second pointer*/ secondPoint});
2575
2576 // Next, the MOVE event for the first pointer
2577 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2578}
2579
2580/**
2581 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2582 * move, and then it will go up, all in the same frame.
2583 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2584 * gets sent to the listener.
2585 */
2586TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2587 NotifyMotionArgs args;
2588 const Point centerPoint = mDevice->getCenterPoint();
2589
2590 // ACTION_DOWN
2591 mDevice->sendSlot(FIRST_SLOT);
2592 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2593 mDevice->sendDown(centerPoint);
2594 mDevice->sendSync();
2595 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2596
2597 // ACTION_POINTER_DOWN (Second slot)
2598 const Point secondPoint = centerPoint + Point(100, 100);
2599 mDevice->sendSlot(SECOND_SLOT);
2600 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2601 mDevice->sendDown(secondPoint);
2602 mDevice->sendSync();
2603 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2604
2605 // ACTION_MOVE (First slot)
2606 mDevice->sendSlot(FIRST_SLOT);
2607 mDevice->sendMove(centerPoint + Point(5, 5));
2608 // ACTION_POINTER_UP (Second slot)
2609 mDevice->sendSlot(SECOND_SLOT);
2610 mDevice->sendMove(secondPoint + Point(6, 6));
2611 mDevice->sendPointerUp();
2612 // Send a single sync for the above 2 pointer updates
2613 mDevice->sendSync();
2614
2615 // First, we should get POINTER_UP for the second pointer
2616 // The movement of the second pointer during the liftoff frame is ignored.
2617 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2618 assertReceivedMotion(ACTION_POINTER_1_UP,
2619 {/*first pointer */ centerPoint + Point(5, 5),
2620 /*second pointer*/ secondPoint});
2621
2622 // Next, the MOVE event for the first pointer
2623 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2624}
2625
Arthur Hungaab25622020-01-16 11:22:11 +08002626TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2627 NotifyMotionArgs args;
2628 const Point centerPoint = mDevice->getCenterPoint();
2629
2630 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002631 mDevice->sendSlot(FIRST_SLOT);
2632 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002633 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002634 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002635 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2636 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2637
arthurhungcc7f9802020-04-30 17:55:40 +08002638 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002639 const Point secondPoint = centerPoint + Point(100, 100);
2640 mDevice->sendSlot(SECOND_SLOT);
2641 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2642 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002643 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002644 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002645 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002646
arthurhungcc7f9802020-04-30 17:55:40 +08002647 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002648 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002649 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002650 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2652
arthurhungcc7f9802020-04-30 17:55:40 +08002653 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2654 // a palm event.
2655 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002656 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002657 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002658 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002659 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002660 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002661
arthurhungcc7f9802020-04-30 17:55:40 +08002662 // Send up to second slot, expect first slot send moving.
2663 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002664 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002665 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002667
arthurhungcc7f9802020-04-30 17:55:40 +08002668 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002669 mDevice->sendSlot(FIRST_SLOT);
2670 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002671 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002672
arthurhungcc7f9802020-04-30 17:55:40 +08002673 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2674 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002675}
2676
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678class InputDeviceTest : public testing::Test {
2679protected:
2680 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002681 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682 static const int32_t DEVICE_ID;
2683 static const int32_t DEVICE_GENERATION;
2684 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002685 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002686 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002688 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002690 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002691 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002692 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693
Chris Yea52ade12020-08-27 16:49:20 -07002694 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002695 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002696 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002697 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002698 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002699 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700 InputDeviceIdentifier identifier;
2701 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002702 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002703 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002704 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002705 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002706 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002707 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 }
2709
Chris Yea52ade12020-08-27 16:49:20 -07002710 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002711 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 }
2714};
2715
2716const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002717const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002718const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002719const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2720const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002721const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002722 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002723const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002724
2725TEST_F(InputDeviceTest, ImmutableProperties) {
2726 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002727 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002728 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729}
2730
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002731TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2732 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2733
2734 // Configuration
2735 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2736 InputReaderConfiguration config;
2737 mDevice->configure(ARBITRARY_TIME, &config, 0);
2738
2739 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2740}
2741
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002742TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2743 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002744}
2745
Michael Wrightd02c5b62014-02-10 15:10:22 -08002746TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2747 // Configuration.
2748 InputReaderConfiguration config;
2749 mDevice->configure(ARBITRARY_TIME, &config, 0);
2750
2751 // Reset.
2752 mDevice->reset(ARBITRARY_TIME);
2753
2754 NotifyDeviceResetArgs resetArgs;
2755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2756 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2757 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2758
2759 // Metadata.
2760 ASSERT_TRUE(mDevice->isIgnored());
2761 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2762
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002763 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002765 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2767 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2768
2769 // State queries.
2770 ASSERT_EQ(0, mDevice->getMetaState());
2771
2772 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2773 << "Ignored device should return unknown key code state.";
2774 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2775 << "Ignored device should return unknown scan code state.";
2776 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2777 << "Ignored device should return unknown switch state.";
2778
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002779 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002781 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 << "Ignored device should never mark any key codes.";
2783 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2784 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2785}
2786
2787TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2788 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002789 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002790
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002791 FakeInputMapper& mapper1 =
2792 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002793 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2794 mapper1.setMetaState(AMETA_ALT_ON);
2795 mapper1.addSupportedKeyCode(AKEYCODE_A);
2796 mapper1.addSupportedKeyCode(AKEYCODE_B);
2797 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2798 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2799 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2800 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2801 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002803 FakeInputMapper& mapper2 =
2804 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
2807 InputReaderConfiguration config;
2808 mDevice->configure(ARBITRARY_TIME, &config, 0);
2809
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002810 std::string propertyValue;
2811 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002813 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002815 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2816 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817
2818 // Reset
2819 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002820 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2821 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822
2823 NotifyDeviceResetArgs resetArgs;
2824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2825 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2826 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2827
2828 // Metadata.
2829 ASSERT_FALSE(mDevice->isIgnored());
2830 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2831
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002832 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002834 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2836 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2837
2838 // State queries.
2839 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2840 << "Should query mappers and combine meta states.";
2841
2842 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2843 << "Should return unknown key code state when source not supported.";
2844 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2845 << "Should return unknown scan code state when source not supported.";
2846 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2847 << "Should return unknown switch state when source not supported.";
2848
2849 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2850 << "Should query mapper when source is supported.";
2851 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2852 << "Should query mapper when source is supported.";
2853 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2854 << "Should query mapper when source is supported.";
2855
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002856 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002858 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859 << "Should do nothing when source is unsupported.";
2860 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2861 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2862 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2863 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2864
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002865 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866 << "Should query mapper when source is supported.";
2867 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2868 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2869 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2870 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2871
2872 // Event handling.
2873 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002874 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002875 mDevice->process(&event, 1);
2876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002877 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2878 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879}
2880
Arthur Hung2c9a3342019-07-23 14:18:59 +08002881// A single input device is associated with a specific display. Check that:
2882// 1. Device is disabled if the viewport corresponding to the associated display is not found
2883// 2. Device is disabled when setEnabled API is called
2884TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002885 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002886
2887 // First Configuration.
2888 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2889
2890 // Device should be enabled by default.
2891 ASSERT_TRUE(mDevice->isEnabled());
2892
2893 // Prepare associated info.
2894 constexpr uint8_t hdmi = 1;
2895 const std::string UNIQUE_ID = "local:1";
2896
2897 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2898 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2899 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2900 // Device should be disabled because it is associated with a specific display via
2901 // input port <-> display port association, but the corresponding display is not found
2902 ASSERT_FALSE(mDevice->isEnabled());
2903
2904 // Prepare displays.
2905 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002906 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2907 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002908 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2909 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2910 ASSERT_TRUE(mDevice->isEnabled());
2911
2912 // Device should be disabled after set disable.
2913 mFakePolicy->addDisabledDevice(mDevice->getId());
2914 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2915 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2916 ASSERT_FALSE(mDevice->isEnabled());
2917
2918 // Device should still be disabled even found the associated display.
2919 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2920 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2921 ASSERT_FALSE(mDevice->isEnabled());
2922}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923
Christine Franks1ba71cc2021-04-07 14:37:42 -07002924TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2925 // Device should be enabled by default.
2926 mFakePolicy->clearViewports();
2927 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2928 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2929 ASSERT_TRUE(mDevice->isEnabled());
2930
2931 // Device should be disabled because it is associated with a specific display, but the
2932 // corresponding display is not found.
2933 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002934 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002935 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2936 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2937 ASSERT_FALSE(mDevice->isEnabled());
2938
2939 // Device should be enabled when a display is found.
2940 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2941 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2942 NO_PORT, ViewportType::INTERNAL);
2943 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2944 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2945 ASSERT_TRUE(mDevice->isEnabled());
2946
2947 // Device should be disabled after set disable.
2948 mFakePolicy->addDisabledDevice(mDevice->getId());
2949 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2950 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2951 ASSERT_FALSE(mDevice->isEnabled());
2952
2953 // Device should still be disabled even found the associated display.
2954 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2955 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2956 ASSERT_FALSE(mDevice->isEnabled());
2957}
2958
Christine Franks2a2293c2022-01-18 11:51:16 -08002959TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2960 mFakePolicy->clearViewports();
2961 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2962 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2963
2964 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2965 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2966 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2967 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2968 NO_PORT, ViewportType::INTERNAL);
2969 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2970 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2971 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2972}
2973
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974// --- InputMapperTest ---
2975
2976class InputMapperTest : public testing::Test {
2977protected:
2978 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002979 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980 static const int32_t DEVICE_ID;
2981 static const int32_t DEVICE_GENERATION;
2982 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002983 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002984 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002986 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002988 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002989 std::unique_ptr<InstrumentedInputReader> mReader;
2990 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002992 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002993 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002994 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002995 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002996 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002997 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002998 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002999 // Consume the device reset notification generated when adding a new device.
3000 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 }
3002
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003003 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003004 SetUp(DEVICE_CLASSES);
3005 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003006
Chris Yea52ade12020-08-27 16:49:20 -07003007 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003008 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 }
3011
3012 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003013 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 }
3015
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003016 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003017 if (!changes ||
3018 (changes &
3019 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3020 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003021 mReader->requestRefreshConfiguration(changes);
3022 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003023 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003024 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003025 // Loop the reader to flush the input listener queue.
3026 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003027 }
3028
arthurhungdcef2dc2020-08-11 14:47:50 +08003029 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3030 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003031 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003032 InputDeviceIdentifier identifier;
3033 identifier.name = name;
3034 identifier.location = location;
3035 std::shared_ptr<InputDevice> device =
3036 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3037 identifier);
3038 mReader->pushNextDevice(device);
3039 mFakeEventHub->addDevice(eventHubId, name, classes);
3040 mReader->loopOnce();
3041 return device;
3042 }
3043
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003044 template <class T, typename... Args>
3045 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003046 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003047 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003049 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003050 // Loop the reader to flush the input listener queue.
3051 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003052 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 }
3054
3055 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003056 int32_t orientation, const std::string& uniqueId,
3057 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003058 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3059 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003060 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3061 }
3062
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003063 void clearViewports() {
3064 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 }
3066
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003067 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3068 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069 RawEvent event;
3070 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003071 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003072 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 event.type = type;
3074 event.code = code;
3075 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003077 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003078 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 }
3080
3081 static void assertMotionRange(const InputDeviceInfo& info,
3082 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3083 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003084 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3086 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3087 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3088 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3089 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3090 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3091 }
3092
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003093 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3094 float size, float touchMajor, float touchMinor, float toolMajor,
3095 float toolMinor, float orientation, float distance,
3096 float scaledAxisEpsilon = 1.f) {
3097 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3098 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3100 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003101 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3102 scaledAxisEpsilon);
3103 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3104 scaledAxisEpsilon);
3105 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3106 scaledAxisEpsilon);
3107 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3108 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3110 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3111 }
3112
Michael Wright17db18e2020-06-26 20:51:44 +01003113 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003115 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116 ASSERT_NEAR(x, actualX, 1);
3117 ASSERT_NEAR(y, actualY, 1);
3118 }
3119};
3120
3121const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003122const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003123const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3125const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003126const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3127 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003128const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129
3130// --- SwitchInputMapperTest ---
3131
3132class SwitchInputMapperTest : public InputMapperTest {
3133protected:
3134};
3135
3136TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003137 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003139 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140}
3141
3142TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003143 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003145 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003146 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003148 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003149 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150}
3151
3152TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003153 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159
3160 NotifySwitchArgs args;
3161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3162 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003163 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3164 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165 args.switchMask);
3166 ASSERT_EQ(uint32_t(0), args.policyFlags);
3167}
3168
Chris Ye87143712020-11-10 05:05:58 +00003169// --- VibratorInputMapperTest ---
3170class VibratorInputMapperTest : public InputMapperTest {
3171protected:
3172 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3173};
3174
3175TEST_F(VibratorInputMapperTest, GetSources) {
3176 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3177
3178 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3179}
3180
3181TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3182 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3183
3184 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3185}
3186
3187TEST_F(VibratorInputMapperTest, Vibrate) {
3188 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003189 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003190 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3191
3192 VibrationElement pattern(2);
3193 VibrationSequence sequence(2);
3194 pattern.duration = std::chrono::milliseconds(200);
3195 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3196 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3197 sequence.addElement(pattern);
3198 pattern.duration = std::chrono::milliseconds(500);
3199 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3200 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3201 sequence.addElement(pattern);
3202
3203 std::vector<int64_t> timings = {0, 1};
3204 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3205
3206 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003207 // Start vibrating
3208 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003209 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003210 // Verify vibrator state listener was notified.
3211 mReader->loopOnce();
3212 NotifyVibratorStateArgs args;
3213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3214 ASSERT_EQ(DEVICE_ID, args.deviceId);
3215 ASSERT_TRUE(args.isOn);
3216 // Stop vibrating
3217 mapper.cancelVibrate(VIBRATION_TOKEN);
3218 ASSERT_FALSE(mapper.isVibrating());
3219 // Verify vibrator state listener was notified.
3220 mReader->loopOnce();
3221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3222 ASSERT_EQ(DEVICE_ID, args.deviceId);
3223 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003224}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225
Chris Yef59a2f42020-10-16 12:55:26 -07003226// --- SensorInputMapperTest ---
3227
3228class SensorInputMapperTest : public InputMapperTest {
3229protected:
3230 static const int32_t ACCEL_RAW_MIN;
3231 static const int32_t ACCEL_RAW_MAX;
3232 static const int32_t ACCEL_RAW_FUZZ;
3233 static const int32_t ACCEL_RAW_FLAT;
3234 static const int32_t ACCEL_RAW_RESOLUTION;
3235
3236 static const int32_t GYRO_RAW_MIN;
3237 static const int32_t GYRO_RAW_MAX;
3238 static const int32_t GYRO_RAW_FUZZ;
3239 static const int32_t GYRO_RAW_FLAT;
3240 static const int32_t GYRO_RAW_RESOLUTION;
3241
3242 static const float GRAVITY_MS2_UNIT;
3243 static const float DEGREE_RADIAN_UNIT;
3244
3245 void prepareAccelAxes();
3246 void prepareGyroAxes();
3247 void setAccelProperties();
3248 void setGyroProperties();
3249 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3250};
3251
3252const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3253const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3254const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3255const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3256const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3257
3258const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3259const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3260const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3261const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3262const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3263
3264const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3265const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3266
3267void SensorInputMapperTest::prepareAccelAxes() {
3268 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3269 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3270 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3271 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3272 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3273 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3274}
3275
3276void SensorInputMapperTest::prepareGyroAxes() {
3277 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3278 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3279 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3280 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3281 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3282 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3283}
3284
3285void SensorInputMapperTest::setAccelProperties() {
3286 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3287 /* sensorDataIndex */ 0);
3288 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3289 /* sensorDataIndex */ 1);
3290 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3291 /* sensorDataIndex */ 2);
3292 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3293 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3294 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3295 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3296 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3297}
3298
3299void SensorInputMapperTest::setGyroProperties() {
3300 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3301 /* sensorDataIndex */ 0);
3302 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3303 /* sensorDataIndex */ 1);
3304 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3305 /* sensorDataIndex */ 2);
3306 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3307 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3308 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3309 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3310 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3311}
3312
3313TEST_F(SensorInputMapperTest, GetSources) {
3314 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3315
3316 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3317}
3318
3319TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3320 setAccelProperties();
3321 prepareAccelAxes();
3322 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3323
3324 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3325 std::chrono::microseconds(10000),
3326 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003327 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003333
3334 NotifySensorArgs args;
3335 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3336 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3337 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3338
3339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3340 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3341 ASSERT_EQ(args.deviceId, DEVICE_ID);
3342 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3343 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3344 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3345 ASSERT_EQ(args.values, values);
3346 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3347}
3348
3349TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3350 setGyroProperties();
3351 prepareGyroAxes();
3352 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3353
3354 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3355 std::chrono::microseconds(10000),
3356 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003357 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003363
3364 NotifySensorArgs args;
3365 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3366 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3367 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3368
3369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3370 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3371 ASSERT_EQ(args.deviceId, DEVICE_ID);
3372 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3373 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3374 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3375 ASSERT_EQ(args.values, values);
3376 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3377}
3378
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379// --- KeyboardInputMapperTest ---
3380
3381class KeyboardInputMapperTest : public InputMapperTest {
3382protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003383 const std::string UNIQUE_ID = "local:0";
3384
3385 void prepareDisplay(int32_t orientation);
3386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003387 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003388 int32_t originalKeyCode, int32_t rotatedKeyCode,
3389 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390};
3391
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003392/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3393 * orientation.
3394 */
3395void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003396 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3397 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003398}
3399
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003400void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003401 int32_t originalScanCode, int32_t originalKeyCode,
3402 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 NotifyKeyArgs args;
3404
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3407 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3408 ASSERT_EQ(originalScanCode, args.scanCode);
3409 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003410 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3414 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3415 ASSERT_EQ(originalScanCode, args.scanCode);
3416 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003417 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418}
3419
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003421 KeyboardInputMapper& mapper =
3422 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3423 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003425 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426}
3427
3428TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3429 const int32_t USAGE_A = 0x070004;
3430 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003431 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3432 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003433 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3434 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3435 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003437 KeyboardInputMapper& mapper =
3438 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3439 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003440 // Initial metastate is AMETA_NONE.
3441 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442
3443 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445 NotifyKeyArgs args;
3446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3447 ASSERT_EQ(DEVICE_ID, args.deviceId);
3448 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3449 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3450 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3451 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3452 ASSERT_EQ(KEY_HOME, args.scanCode);
3453 ASSERT_EQ(AMETA_NONE, args.metaState);
3454 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3455 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3456 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3457
3458 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003459 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
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 + 1, args.eventTime);
3464 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3465 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3466 ASSERT_EQ(KEY_HOME, 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 down 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, READ_TIME, EV_KEY, 0, 1);
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, args.eventTime);
3479 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, 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 up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3489 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
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 + 1, args.eventTime);
3494 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3495 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3496 ASSERT_EQ(0, args.scanCode);
3497 ASSERT_EQ(AMETA_NONE, args.metaState);
3498 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3499 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3500 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3501
3502 // Key down 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, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
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, args.eventTime);
3509 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, 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 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3519 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3521 ASSERT_EQ(DEVICE_ID, args.deviceId);
3522 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3523 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3524 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3525 ASSERT_EQ(0, args.keyCode);
3526 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3527 ASSERT_EQ(AMETA_NONE, args.metaState);
3528 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3529 ASSERT_EQ(0U, args.policyFlags);
3530 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3531}
3532
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003533/**
3534 * Ensure that the readTime is set to the time when the EV_KEY is received.
3535 */
3536TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3537 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3538
3539 KeyboardInputMapper& mapper =
3540 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3541 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3542 NotifyKeyArgs args;
3543
3544 // Key down
3545 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3547 ASSERT_EQ(12, args.readTime);
3548
3549 // Key up
3550 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3552 ASSERT_EQ(15, args.readTime);
3553}
3554
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003558 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3559 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3560 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003562 KeyboardInputMapper& mapper =
3563 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3564 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565
Arthur Hung95f68612022-04-07 14:08:22 +08003566 // Initial metastate is AMETA_NONE.
3567 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568
3569 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003571 NotifyKeyArgs args;
3572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3573 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003575 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576
3577 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003578 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3580 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003581 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003582
3583 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003584 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3586 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588
3589 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003590 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3592 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003593 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003594 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595}
3596
3597TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3599 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3600 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3601 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003603 KeyboardInputMapper& mapper =
3604 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3605 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003607 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3609 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3610 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3611 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3613 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3615 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3616}
3617
3618TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003619 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3620 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3621 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3622 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003625 KeyboardInputMapper& mapper =
3626 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3627 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003629 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003630 ASSERT_NO_FATAL_FAILURE(
3631 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3633 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3635 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3636 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3637 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003639 clearViewports();
3640 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003641 ASSERT_NO_FATAL_FAILURE(
3642 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3644 AKEYCODE_DPAD_UP, DISPLAY_ID));
3645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3646 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3648 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003649
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003650 clearViewports();
3651 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003652 ASSERT_NO_FATAL_FAILURE(
3653 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3654 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3655 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3656 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3657 AKEYCODE_DPAD_UP, DISPLAY_ID));
3658 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3659 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003660
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003661 clearViewports();
3662 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003663 ASSERT_NO_FATAL_FAILURE(
3664 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3665 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3666 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3667 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3668 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3670 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671
3672 // Special case: if orientation changes while key is down, we still emit the same keycode
3673 // in the key up as we did in the key down.
3674 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003675 clearViewports();
3676 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3679 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3680 ASSERT_EQ(KEY_UP, args.scanCode);
3681 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3682
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003683 clearViewports();
3684 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3687 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3688 ASSERT_EQ(KEY_UP, args.scanCode);
3689 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3690}
3691
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003692TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3693 // If the keyboard is not orientation aware,
3694 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003695 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003696
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003697 KeyboardInputMapper& mapper =
3698 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3699 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003700 NotifyKeyArgs args;
3701
3702 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3707 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3708
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003709 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3714 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3715}
3716
3717TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3718 // If the keyboard is orientation aware,
3719 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003720 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003721
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003722 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003723 KeyboardInputMapper& mapper =
3724 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3725 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003726 NotifyKeyArgs args;
3727
3728 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3729 // ^--- already checked by the previous test
3730
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003731 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003732 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003735 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3737 ASSERT_EQ(DISPLAY_ID, args.displayId);
3738
3739 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003740 clearViewports();
3741 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003742 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3747 ASSERT_EQ(newDisplayId, args.displayId);
3748}
3749
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 KeyboardInputMapper& mapper =
3752 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3753 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003755 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003756 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003759 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760}
3761
Philip Junker4af3b3d2021-12-14 10:36:55 +01003762TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3763 KeyboardInputMapper& mapper =
3764 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3765 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3766
3767 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3768 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3769 << "If a mapping is available, the result is equal to the mapping";
3770
3771 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3772 << "If no mapping is available, the result is the key location";
3773}
3774
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776 KeyboardInputMapper& mapper =
3777 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3778 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003779
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003781 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003783 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003784 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003785}
3786
3787TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003788 KeyboardInputMapper& mapper =
3789 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3790 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003792 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003795 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796 ASSERT_TRUE(flags[0]);
3797 ASSERT_FALSE(flags[1]);
3798}
3799
3800TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003801 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3802 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3803 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3804 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3805 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3806 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003808 KeyboardInputMapper& mapper =
3809 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3810 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003811 // Initial metastate is AMETA_NONE.
3812 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813
3814 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003815 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818
3819 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003822 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3823 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003825 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003826
3827 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003830 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3831 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3832 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003833 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834
3835 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003838 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3839 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3840 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003841 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842
3843 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003846 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3847 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3848 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003849 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850
3851 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003854 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3855 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3856 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003857 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858
3859 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003862 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3863 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3864 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866}
3867
Chris Yea52ade12020-08-27 16:49:20 -07003868TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3869 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3870 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3871 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3872 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3873
3874 KeyboardInputMapper& mapper =
3875 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3876 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3877
Chris Yea52ade12020-08-27 16:49:20 -07003878 // Meta state should be AMETA_NONE after reset
3879 mapper.reset(ARBITRARY_TIME);
3880 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3881 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3882 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3883 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3884
3885 NotifyKeyArgs args;
3886 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3889 ASSERT_EQ(AMETA_NONE, args.metaState);
3890 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3891 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3892 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3893
3894 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003895 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3897 ASSERT_EQ(AMETA_NONE, args.metaState);
3898 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3899 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3900 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3901}
3902
Arthur Hung2c9a3342019-07-23 14:18:59 +08003903TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3904 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003905 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3906 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3907 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3908 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003909
3910 // keyboard 2.
3911 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003912 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003913 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003914 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003915 std::shared_ptr<InputDevice> device2 =
3916 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003917 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003918
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003919 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3920 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3921 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3922 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003923
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003924 KeyboardInputMapper& mapper =
3925 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3926 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003927
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003928 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003929 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003930 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003931 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3932 device2->reset(ARBITRARY_TIME);
3933
3934 // Prepared displays and associated info.
3935 constexpr uint8_t hdmi1 = 0;
3936 constexpr uint8_t hdmi2 = 1;
3937 const std::string SECONDARY_UNIQUE_ID = "local:1";
3938
3939 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3940 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3941
3942 // No associated display viewport found, should disable the device.
3943 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3944 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3945 ASSERT_FALSE(device2->isEnabled());
3946
3947 // Prepare second display.
3948 constexpr int32_t newDisplayId = 2;
3949 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003950 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003951 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003952 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003953 // Default device will reconfigure above, need additional reconfiguration for another device.
3954 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3955 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3956
3957 // Device should be enabled after the associated display is found.
3958 ASSERT_TRUE(mDevice->isEnabled());
3959 ASSERT_TRUE(device2->isEnabled());
3960
3961 // Test pad key events
3962 ASSERT_NO_FATAL_FAILURE(
3963 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3964 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3965 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3966 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3967 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3968 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3969 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3970
3971 ASSERT_NO_FATAL_FAILURE(
3972 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3973 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3974 AKEYCODE_DPAD_RIGHT, newDisplayId));
3975 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3976 AKEYCODE_DPAD_DOWN, newDisplayId));
3977 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3978 AKEYCODE_DPAD_LEFT, newDisplayId));
3979}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980
arthurhungc903df12020-08-11 15:08:42 +08003981TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3982 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3983 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3984 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3985 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3986 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3987 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3988
3989 KeyboardInputMapper& mapper =
3990 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3991 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003992 // Initial metastate is AMETA_NONE.
3993 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003994
3995 // Initialization should have turned all of the lights off.
3996 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3997 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3998 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3999
4000 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004003 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4004 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4005
4006 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004009 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4010 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4011
4012 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004015 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4016 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4017
4018 mFakeEventHub->removeDevice(EVENTHUB_ID);
4019 mReader->loopOnce();
4020
4021 // keyboard 2 should default toggle keys.
4022 const std::string USB2 = "USB2";
4023 const std::string DEVICE_NAME2 = "KEYBOARD2";
4024 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4025 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4026 std::shared_ptr<InputDevice> device2 =
4027 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004028 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004029 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4030 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4031 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4032 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4033 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4034 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4035
arthurhung6fe95782020-10-05 22:41:16 +08004036 KeyboardInputMapper& mapper2 =
4037 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4038 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004039 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4040 device2->reset(ARBITRARY_TIME);
4041
4042 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4043 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4044 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004045 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4046 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004047}
4048
Arthur Hungcb40a002021-08-03 14:31:01 +00004049TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4050 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4052 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4053
4054 // Suppose we have two mappers. (DPAD + KEYBOARD)
4055 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4056 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4057 KeyboardInputMapper& mapper =
4058 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4059 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004060 // Initial metastate is AMETA_NONE.
4061 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004062
4063 mReader->toggleCapsLockState(DEVICE_ID);
4064 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4065}
4066
Arthur Hungfb3cc112022-04-13 07:39:50 +00004067TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4068 // keyboard 1.
4069 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4070 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4071 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4072 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4073 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4074 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4075
4076 KeyboardInputMapper& mapper1 =
4077 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4078 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4079
4080 // keyboard 2.
4081 const std::string USB2 = "USB2";
4082 const std::string DEVICE_NAME2 = "KEYBOARD2";
4083 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4084 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4085 std::shared_ptr<InputDevice> device2 =
4086 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4087 ftl::Flags<InputDeviceClass>(0));
4088 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4089 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4090 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4091 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4092 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4093 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4094
4095 KeyboardInputMapper& mapper2 =
4096 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4097 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4098 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4099 device2->reset(ARBITRARY_TIME);
4100
Arthur Hung95f68612022-04-07 14:08:22 +08004101 // Initial metastate is AMETA_NONE.
4102 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4103 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4104
4105 // Toggle num lock on and off.
4106 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4107 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004108 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4109 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4110 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4111
4112 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4113 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4114 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4115 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4116 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4117
4118 // Toggle caps lock on and off.
4119 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4120 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4121 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4122 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4123 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4124
4125 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4126 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4127 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4128 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4129 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4130
4131 // Toggle scroll lock on and off.
4132 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4133 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4134 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4135 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4136 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4137
4138 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4139 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4140 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4141 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4142 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4143}
4144
Arthur Hung2141d542022-08-23 07:45:21 +00004145TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4146 const int32_t USAGE_A = 0x070004;
4147 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4148 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4149
4150 KeyboardInputMapper& mapper =
4151 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4152 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4153 // Key down by scan code.
4154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4155 NotifyKeyArgs args;
4156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4157 ASSERT_EQ(DEVICE_ID, args.deviceId);
4158 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4159 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4160 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4161 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4162 ASSERT_EQ(KEY_HOME, args.scanCode);
4163 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4164
4165 // Disable device, it should synthesize cancellation events for down events.
4166 mFakePolicy->addDisabledDevice(DEVICE_ID);
4167 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4168
4169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4170 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4171 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4172 ASSERT_EQ(KEY_HOME, args.scanCode);
4173 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4174}
4175
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004176// --- KeyboardInputMapperTest_ExternalDevice ---
4177
4178class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4179protected:
Chris Yea52ade12020-08-27 16:49:20 -07004180 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004181};
4182
4183TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004184 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4185 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004186
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004187 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4188 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4189 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4190 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004192 KeyboardInputMapper& mapper =
4193 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4194 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004195
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004197 NotifyKeyArgs args;
4198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4199 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4200
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4203 ASSERT_EQ(uint32_t(0), args.policyFlags);
4204
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4207 ASSERT_EQ(uint32_t(0), args.policyFlags);
4208
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004209 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4211 ASSERT_EQ(uint32_t(0), args.policyFlags);
4212
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4215 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4216
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4219 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4220}
4221
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004222TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004223 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004224
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004225 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4226 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4227 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004228
Powei Fengd041c5d2019-05-03 17:11:33 -07004229 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004230 KeyboardInputMapper& mapper =
4231 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4232 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004233
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004235 NotifyKeyArgs args;
4236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4237 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4238
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004239 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4241 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4242
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4245 ASSERT_EQ(uint32_t(0), args.policyFlags);
4246
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004247 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4249 ASSERT_EQ(uint32_t(0), args.policyFlags);
4250
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4253 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4254
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004255 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4257 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4258}
4259
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260// --- CursorInputMapperTest ---
4261
4262class CursorInputMapperTest : public InputMapperTest {
4263protected:
4264 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4265
Michael Wright17db18e2020-06-26 20:51:44 +01004266 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267
Chris Yea52ade12020-08-27 16:49:20 -07004268 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 InputMapperTest::SetUp();
4270
Michael Wright17db18e2020-06-26 20:51:44 +01004271 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004272 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 }
4274
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004275 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4276 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004277
4278 void prepareDisplay(int32_t orientation) {
4279 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004280 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004281 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4282 orientation, uniqueId, NO_PORT, viewportType);
4283 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004284
4285 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4286 float pressure) {
4287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4288 0.0f, 0.0f, 0.0f, EPSILON));
4289 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290};
4291
4292const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4293
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004294void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4295 int32_t originalY, int32_t rotatedX,
4296 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 NotifyMotionArgs args;
4298
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004304 ASSERT_NO_FATAL_FAILURE(
4305 assertCursorPointerCoords(args.pointerCoords[0],
4306 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4307 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308}
4309
4310TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004312 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004314 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315}
4316
4317TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004319 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004321 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322}
4323
4324TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004326 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327
4328 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004329 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330
4331 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004332 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4333 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4335 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4336
4337 // When the bounds are set, then there should be a valid motion range.
4338 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4339
4340 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004341 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342
4343 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4344 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4345 1, 800 - 1, 0.0f, 0.0f));
4346 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4347 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4348 2, 480 - 1, 0.0f, 0.0f));
4349 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4350 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4351 0.0f, 1.0f, 0.0f, 0.0f));
4352}
4353
4354TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004356 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357
4358 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004359 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360
4361 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4362 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4363 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4364 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4365 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4366 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4367 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4368 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4369 0.0f, 1.0f, 0.0f, 0.0f));
4370}
4371
4372TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004374 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375
arthurhungdcef2dc2020-08-11 14:47:50 +08004376 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377
4378 NotifyMotionArgs args;
4379
4380 // Button press.
4381 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4385 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4386 ASSERT_EQ(DEVICE_ID, args.deviceId);
4387 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4388 ASSERT_EQ(uint32_t(0), args.policyFlags);
4389 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4390 ASSERT_EQ(0, args.flags);
4391 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4392 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4393 ASSERT_EQ(0, args.edgeFlags);
4394 ASSERT_EQ(uint32_t(1), args.pointerCount);
4395 ASSERT_EQ(0, args.pointerProperties[0].id);
4396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004397 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4399 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4400 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4401
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4403 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4404 ASSERT_EQ(DEVICE_ID, args.deviceId);
4405 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4406 ASSERT_EQ(uint32_t(0), args.policyFlags);
4407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4408 ASSERT_EQ(0, args.flags);
4409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4410 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4411 ASSERT_EQ(0, args.edgeFlags);
4412 ASSERT_EQ(uint32_t(1), args.pointerCount);
4413 ASSERT_EQ(0, args.pointerProperties[0].id);
4414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4417 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4418 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4419
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004421 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4422 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4424 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4425 ASSERT_EQ(DEVICE_ID, args.deviceId);
4426 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4427 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4429 ASSERT_EQ(0, args.flags);
4430 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4431 ASSERT_EQ(0, args.buttonState);
4432 ASSERT_EQ(0, args.edgeFlags);
4433 ASSERT_EQ(uint32_t(1), args.pointerCount);
4434 ASSERT_EQ(0, args.pointerProperties[0].id);
4435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004436 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4438 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4439 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4440
4441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4442 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4443 ASSERT_EQ(DEVICE_ID, args.deviceId);
4444 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4445 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4447 ASSERT_EQ(0, args.flags);
4448 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4449 ASSERT_EQ(0, args.buttonState);
4450 ASSERT_EQ(0, args.edgeFlags);
4451 ASSERT_EQ(uint32_t(1), args.pointerCount);
4452 ASSERT_EQ(0, args.pointerProperties[0].id);
4453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004454 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4456 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4458}
4459
4460TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004462 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463
4464 NotifyMotionArgs args;
4465
4466 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004471 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4472 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4473 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474
4475 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004480 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4481 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482}
4483
4484TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004486 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487
4488 NotifyMotionArgs args;
4489
4490 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4494 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004495 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004499 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004500
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004506 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004507
4508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004510 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511}
4512
4513TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004515 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516
4517 NotifyMotionArgs args;
4518
4519 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4525 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004526 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4527 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4528 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004532 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4533 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4534 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004535
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004542 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4543 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4544 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545
4546 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004551 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004552
4553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004555 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556}
4557
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004558TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004560 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4561 // need to be rotated.
4562 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004563 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004565 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4567 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4568 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4569 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4570 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4571 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4574}
4575
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004576TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004578 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4579 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004580 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004582 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4584 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4585 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4586 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4587 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4588 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4589 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4590 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4591
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004592 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004593 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4594 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4595 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4596 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4597 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4598 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4599 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4600 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004602 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4604 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4605 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4606 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4607 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4608 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4609 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4610 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4611
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004612 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004613 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4614 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4615 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4616 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4617 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4618 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4619 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4620 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621}
4622
4623TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004625 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626
4627 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4628 mFakePointerController->setPosition(100, 200);
4629 mFakePointerController->setButtonState(0);
4630
4631 NotifyMotionArgs motionArgs;
4632 NotifyKeyArgs keyArgs;
4633
4634 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4638 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4639 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4640 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004641 ASSERT_NO_FATAL_FAILURE(
4642 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4645 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4646 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4647 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004648 ASSERT_NO_FATAL_FAILURE(
4649 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004650
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655 ASSERT_EQ(0, motionArgs.buttonState);
4656 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004657 ASSERT_NO_FATAL_FAILURE(
4658 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_EQ(0, motionArgs.buttonState);
4663 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004664 ASSERT_NO_FATAL_FAILURE(
4665 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004666
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004669 ASSERT_EQ(0, motionArgs.buttonState);
4670 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004671 ASSERT_NO_FATAL_FAILURE(
4672 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673
4674 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4680 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4681 motionArgs.buttonState);
4682 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4683 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004684 ASSERT_NO_FATAL_FAILURE(
4685 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4688 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4689 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4690 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4691 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004692 ASSERT_NO_FATAL_FAILURE(
4693 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004694
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4696 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4697 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4698 motionArgs.buttonState);
4699 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4700 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004701 ASSERT_NO_FATAL_FAILURE(
4702 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004703
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004707 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4709 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004710 ASSERT_NO_FATAL_FAILURE(
4711 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004712
4713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004715 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4716 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004717 ASSERT_NO_FATAL_FAILURE(
4718 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4724 ASSERT_EQ(0, motionArgs.buttonState);
4725 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004726 ASSERT_NO_FATAL_FAILURE(
4727 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4729 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004730
4731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 ASSERT_EQ(0, motionArgs.buttonState);
4733 ASSERT_EQ(0, mFakePointerController->getButtonState());
4734 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004735 ASSERT_NO_FATAL_FAILURE(
4736 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4739 ASSERT_EQ(0, motionArgs.buttonState);
4740 ASSERT_EQ(0, mFakePointerController->getButtonState());
4741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004742 ASSERT_NO_FATAL_FAILURE(
4743 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744
4745 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4749 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4750 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004751
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4755 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004756 ASSERT_NO_FATAL_FAILURE(
4757 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004758
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4761 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4762 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004763 ASSERT_NO_FATAL_FAILURE(
4764 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004769 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 ASSERT_EQ(0, motionArgs.buttonState);
4771 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004772 ASSERT_NO_FATAL_FAILURE(
4773 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004774
4775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004777 ASSERT_EQ(0, motionArgs.buttonState);
4778 ASSERT_EQ(0, mFakePointerController->getButtonState());
4779
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004780 ASSERT_NO_FATAL_FAILURE(
4781 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4783 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4784 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4785
4786 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4790 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4791 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004792
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4796 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004797 ASSERT_NO_FATAL_FAILURE(
4798 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4802 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4803 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004804 ASSERT_NO_FATAL_FAILURE(
4805 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004810 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811 ASSERT_EQ(0, motionArgs.buttonState);
4812 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004813 ASSERT_NO_FATAL_FAILURE(
4814 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004815
4816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4817 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4818 ASSERT_EQ(0, motionArgs.buttonState);
4819 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004820 ASSERT_NO_FATAL_FAILURE(
4821 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004822
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4824 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4825 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4826
4827 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4831 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4832 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004833
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004835 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4837 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004838 ASSERT_NO_FATAL_FAILURE(
4839 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004840
4841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4842 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4843 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4844 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004845 ASSERT_NO_FATAL_FAILURE(
4846 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852 ASSERT_EQ(0, motionArgs.buttonState);
4853 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004854 ASSERT_NO_FATAL_FAILURE(
4855 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004856
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4859 ASSERT_EQ(0, motionArgs.buttonState);
4860 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004861 ASSERT_NO_FATAL_FAILURE(
4862 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004863
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4865 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4866 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4867
4868 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4872 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4873 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004874
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004876 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4878 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004879 ASSERT_NO_FATAL_FAILURE(
4880 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004881
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4884 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4885 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004886 ASSERT_NO_FATAL_FAILURE(
4887 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004892 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893 ASSERT_EQ(0, motionArgs.buttonState);
4894 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004895 ASSERT_NO_FATAL_FAILURE(
4896 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004897
4898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4899 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4900 ASSERT_EQ(0, motionArgs.buttonState);
4901 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004902 ASSERT_NO_FATAL_FAILURE(
4903 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004904
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4906 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4907 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4908}
4909
4910TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004912 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913
4914 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4915 mFakePointerController->setPosition(100, 200);
4916 mFakePointerController->setButtonState(0);
4917
4918 NotifyMotionArgs args;
4919
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004924 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4925 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4927 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 +01004928 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004929}
4930
4931TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004932 addConfigurationProperty("cursor.mode", "pointer");
4933 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004934 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004935
4936 NotifyDeviceResetArgs resetArgs;
4937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4938 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4939 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4940
4941 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4942 mFakePointerController->setPosition(100, 200);
4943 mFakePointerController->setButtonState(0);
4944
4945 NotifyMotionArgs args;
4946
4947 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4952 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4955 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 +01004956 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004957
4958 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4962 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4963 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4965 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4967 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4970 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4971
4972 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004973 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4974 process(mapper, ARBITRARY_TIME + 2, 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_RELATIVE, args.source);
4977 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4979 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4981 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4982 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4984 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4985
4986 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4991 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4994 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 +01004995 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004996
4997 // Disable pointer capture and check that the device generation got bumped
4998 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004999 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005000 mFakePolicy->setPointerCapture(false);
5001 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005002 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005003
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005005 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5006
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5011 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5014 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 +01005015 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016}
5017
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005018/**
5019 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5020 * pointer acceleration or speed processing should not be applied.
5021 */
5022TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5023 addConfigurationProperty("cursor.mode", "pointer");
5024 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5025 100.f /*high threshold*/, 10.f /*acceleration*/);
5026 mFakePolicy->setVelocityControlParams(testParams);
5027 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5028
5029 NotifyDeviceResetArgs resetArgs;
5030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5031 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5032 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5033
5034 NotifyMotionArgs args;
5035
5036 // Move and verify scale is applied.
5037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5041 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5043 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5044 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5045 ASSERT_GT(relX, 10);
5046 ASSERT_GT(relY, 20);
5047
5048 // Enable Pointer Capture
5049 mFakePolicy->setPointerCapture(true);
5050 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5051 NotifyPointerCaptureChangedArgs captureArgs;
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5053 ASSERT_TRUE(captureArgs.request.enable);
5054
5055 // Move and verify scale is not applied.
5056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5060 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5062 ASSERT_EQ(10, args.pointerCoords[0].getX());
5063 ASSERT_EQ(20, args.pointerCoords[0].getY());
5064}
5065
Prabir Pradhan208360b2022-06-24 18:37:04 +00005066TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5067 addConfigurationProperty("cursor.mode", "pointer");
5068 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5069
5070 NotifyDeviceResetArgs resetArgs;
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5072 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5073 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5074
5075 // Ensure the display is rotated.
5076 prepareDisplay(DISPLAY_ORIENTATION_90);
5077
5078 NotifyMotionArgs args;
5079
5080 // Verify that the coordinates are rotated.
5081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5085 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5086 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5087 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5088 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5089
5090 // Enable Pointer Capture.
5091 mFakePolicy->setPointerCapture(true);
5092 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5093 NotifyPointerCaptureChangedArgs captureArgs;
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5095 ASSERT_TRUE(captureArgs.request.enable);
5096
5097 // Move and verify rotation is not applied.
5098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5102 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5104 ASSERT_EQ(10, args.pointerCoords[0].getX());
5105 ASSERT_EQ(20, args.pointerCoords[0].getY());
5106}
5107
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005108TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005109 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005110
Garfield Tan888a6a42020-01-09 11:39:16 -08005111 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005112 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005113 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5114 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005115 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5116 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005117 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5118 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5119
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005120 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5121 mFakePointerController->setPosition(100, 200);
5122 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005123
5124 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5129 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5130 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5132 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 +01005133 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005134 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5135}
5136
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137// --- TouchInputMapperTest ---
5138
5139class TouchInputMapperTest : public InputMapperTest {
5140protected:
5141 static const int32_t RAW_X_MIN;
5142 static const int32_t RAW_X_MAX;
5143 static const int32_t RAW_Y_MIN;
5144 static const int32_t RAW_Y_MAX;
5145 static const int32_t RAW_TOUCH_MIN;
5146 static const int32_t RAW_TOUCH_MAX;
5147 static const int32_t RAW_TOOL_MIN;
5148 static const int32_t RAW_TOOL_MAX;
5149 static const int32_t RAW_PRESSURE_MIN;
5150 static const int32_t RAW_PRESSURE_MAX;
5151 static const int32_t RAW_ORIENTATION_MIN;
5152 static const int32_t RAW_ORIENTATION_MAX;
5153 static const int32_t RAW_DISTANCE_MIN;
5154 static const int32_t RAW_DISTANCE_MAX;
5155 static const int32_t RAW_TILT_MIN;
5156 static const int32_t RAW_TILT_MAX;
5157 static const int32_t RAW_ID_MIN;
5158 static const int32_t RAW_ID_MAX;
5159 static const int32_t RAW_SLOT_MIN;
5160 static const int32_t RAW_SLOT_MAX;
5161 static const float X_PRECISION;
5162 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005163 static const float X_PRECISION_VIRTUAL;
5164 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165
5166 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005167 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168
5169 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5170
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005171 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005172 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005173
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174 enum Axes {
5175 POSITION = 1 << 0,
5176 TOUCH = 1 << 1,
5177 TOOL = 1 << 2,
5178 PRESSURE = 1 << 3,
5179 ORIENTATION = 1 << 4,
5180 MINOR = 1 << 5,
5181 ID = 1 << 6,
5182 DISTANCE = 1 << 7,
5183 TILT = 1 << 8,
5184 SLOT = 1 << 9,
5185 TOOL_TYPE = 1 << 10,
5186 };
5187
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005188 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5189 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005190 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005192 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005193 int32_t toRawX(float displayX);
5194 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005195 int32_t toRotatedRawX(float displayX);
5196 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005197 float toCookedX(float rawX, float rawY);
5198 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005200 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005202 float toDisplayY(int32_t rawY, int32_t displayHeight);
5203
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204};
5205
5206const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5207const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5208const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5209const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5210const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5211const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5212const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5213const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005214const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5215const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5217const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5218const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5219const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5220const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5221const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5222const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5223const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5224const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5225const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5226const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5227const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005228const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5229 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5230const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5231 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005232const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5233 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234
5235const float TouchInputMapperTest::GEOMETRIC_SCALE =
5236 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5237 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5238
5239const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5240 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5241 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5242};
5243
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005244void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005245 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5246 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005247}
5248
5249void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5250 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5251 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252}
5253
Santos Cordonfa5cf462017-04-05 10:37:00 -07005254void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005255 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5256 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5257 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005258}
5259
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005261 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5262 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5263 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5264 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005265}
5266
Jason Gerecke489fda82012-09-07 17:19:40 -07005267void TouchInputMapperTest::prepareLocationCalibration() {
5268 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5269}
5270
Michael Wrightd02c5b62014-02-10 15:10:22 -08005271int32_t TouchInputMapperTest::toRawX(float displayX) {
5272 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5273}
5274
5275int32_t TouchInputMapperTest::toRawY(float displayY) {
5276 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5277}
5278
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005279int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5280 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5281}
5282
5283int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5284 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5285}
5286
Jason Gerecke489fda82012-09-07 17:19:40 -07005287float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5288 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5289 return rawX;
5290}
5291
5292float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5293 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5294 return rawY;
5295}
5296
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005298 return toDisplayX(rawX, DISPLAY_WIDTH);
5299}
5300
5301float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5302 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303}
5304
5305float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005306 return toDisplayY(rawY, DISPLAY_HEIGHT);
5307}
5308
5309float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5310 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311}
5312
5313
5314// --- SingleTouchInputMapperTest ---
5315
5316class SingleTouchInputMapperTest : public TouchInputMapperTest {
5317protected:
5318 void prepareButtons();
5319 void prepareAxes(int axes);
5320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005321 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5322 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5323 void processUp(SingleTouchInputMapper& mappery);
5324 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5325 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5326 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5327 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5328 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5329 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330};
5331
5332void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005333 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334}
5335
5336void SingleTouchInputMapperTest::prepareAxes(int axes) {
5337 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005338 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5339 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 }
5341 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005342 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5343 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344 }
5345 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005346 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5347 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348 }
5349 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005350 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5351 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352 }
5353 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005354 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5355 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356 }
5357}
5358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005359void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363}
5364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005365void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368}
5369
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005370void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372}
5373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376}
5377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005378void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5379 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381}
5382
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005383void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385}
5386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005387void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5388 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391}
5392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5394 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396}
5397
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005398void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400}
5401
Michael Wrightd02c5b62014-02-10 15:10:22 -08005402TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403 prepareButtons();
5404 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005405 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005407 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408}
5409
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 prepareButtons();
5412 prepareAxes(POSITION);
5413 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005414 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005416 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417}
5418
5419TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420 addConfigurationProperty("touch.deviceType", "touchScreen");
5421 prepareDisplay(DISPLAY_ORIENTATION_0);
5422 prepareButtons();
5423 prepareAxes(POSITION);
5424 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005425 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426
5427 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005428 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429
5430 // Virtual key is down.
5431 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5432 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5433 processDown(mapper, x, y);
5434 processSync(mapper);
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5436
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005437 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438
5439 // Virtual key is up.
5440 processUp(mapper);
5441 processSync(mapper);
5442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5443
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005444 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445}
5446
5447TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 addConfigurationProperty("touch.deviceType", "touchScreen");
5449 prepareDisplay(DISPLAY_ORIENTATION_0);
5450 prepareButtons();
5451 prepareAxes(POSITION);
5452 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005453 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005454
5455 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005456 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457
5458 // Virtual key is down.
5459 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5460 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5461 processDown(mapper, x, y);
5462 processSync(mapper);
5463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5464
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005465 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466
5467 // Virtual key is up.
5468 processUp(mapper);
5469 processSync(mapper);
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005472 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473}
5474
5475TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476 addConfigurationProperty("touch.deviceType", "touchScreen");
5477 prepareDisplay(DISPLAY_ORIENTATION_0);
5478 prepareButtons();
5479 prepareAxes(POSITION);
5480 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005481 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005484 ASSERT_TRUE(
5485 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486 ASSERT_TRUE(flags[0]);
5487 ASSERT_FALSE(flags[1]);
5488}
5489
5490TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 addConfigurationProperty("touch.deviceType", "touchScreen");
5492 prepareDisplay(DISPLAY_ORIENTATION_0);
5493 prepareButtons();
5494 prepareAxes(POSITION);
5495 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005496 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497
arthurhungdcef2dc2020-08-11 14:47:50 +08005498 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499
5500 NotifyKeyArgs args;
5501
5502 // Press virtual key.
5503 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5504 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5505 processDown(mapper, x, y);
5506 processSync(mapper);
5507
5508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5509 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5510 ASSERT_EQ(DEVICE_ID, args.deviceId);
5511 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5512 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5513 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5514 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5515 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5516 ASSERT_EQ(KEY_HOME, args.scanCode);
5517 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5518 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5519
5520 // Release virtual key.
5521 processUp(mapper);
5522 processSync(mapper);
5523
5524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5525 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5526 ASSERT_EQ(DEVICE_ID, args.deviceId);
5527 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5528 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5529 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5530 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5531 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5532 ASSERT_EQ(KEY_HOME, args.scanCode);
5533 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5534 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5535
5536 // Should not have sent any motions.
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5538}
5539
5540TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005541 addConfigurationProperty("touch.deviceType", "touchScreen");
5542 prepareDisplay(DISPLAY_ORIENTATION_0);
5543 prepareButtons();
5544 prepareAxes(POSITION);
5545 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005546 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547
arthurhungdcef2dc2020-08-11 14:47:50 +08005548 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549
5550 NotifyKeyArgs keyArgs;
5551
5552 // Press virtual key.
5553 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5554 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5555 processDown(mapper, x, y);
5556 processSync(mapper);
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5559 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5560 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5561 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5562 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5563 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5564 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5565 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5566 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5568 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5569
5570 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5571 // into the display area.
5572 y -= 100;
5573 processMove(mapper, x, y);
5574 processSync(mapper);
5575
5576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5577 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5578 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5579 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5580 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5581 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5582 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5583 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5584 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5585 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5586 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5587 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5588
5589 NotifyMotionArgs motionArgs;
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5592 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5593 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5594 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5595 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5596 ASSERT_EQ(0, motionArgs.flags);
5597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5598 ASSERT_EQ(0, motionArgs.buttonState);
5599 ASSERT_EQ(0, motionArgs.edgeFlags);
5600 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5604 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5605 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5606 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5607 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5608
5609 // Keep moving out of bounds. Should generate a pointer move.
5610 y -= 50;
5611 processMove(mapper, x, y);
5612 processSync(mapper);
5613
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5615 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5616 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5617 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5618 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5620 ASSERT_EQ(0, motionArgs.flags);
5621 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5622 ASSERT_EQ(0, motionArgs.buttonState);
5623 ASSERT_EQ(0, motionArgs.edgeFlags);
5624 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5625 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5628 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5629 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5630 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5631 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5632
5633 // Release out of bounds. Should generate a pointer up.
5634 processUp(mapper);
5635 processSync(mapper);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5639 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5640 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5641 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5642 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5643 ASSERT_EQ(0, motionArgs.flags);
5644 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5645 ASSERT_EQ(0, motionArgs.buttonState);
5646 ASSERT_EQ(0, motionArgs.edgeFlags);
5647 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5651 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5652 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5653 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5655
5656 // Should not have sent any more keys or motions.
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5659}
5660
5661TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005662 addConfigurationProperty("touch.deviceType", "touchScreen");
5663 prepareDisplay(DISPLAY_ORIENTATION_0);
5664 prepareButtons();
5665 prepareAxes(POSITION);
5666 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005667 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668
arthurhungdcef2dc2020-08-11 14:47:50 +08005669 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005670
5671 NotifyMotionArgs motionArgs;
5672
5673 // Initially go down out of bounds.
5674 int32_t x = -10;
5675 int32_t y = -10;
5676 processDown(mapper, x, y);
5677 processSync(mapper);
5678
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5680
5681 // Move into the display area. Should generate a pointer down.
5682 x = 50;
5683 y = 75;
5684 processMove(mapper, x, y);
5685 processSync(mapper);
5686
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5688 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5689 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5690 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5691 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5692 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5693 ASSERT_EQ(0, motionArgs.flags);
5694 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5695 ASSERT_EQ(0, motionArgs.buttonState);
5696 ASSERT_EQ(0, motionArgs.edgeFlags);
5697 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5698 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5701 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5702 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5703 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5704 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5705
5706 // Release. Should generate a pointer up.
5707 processUp(mapper);
5708 processSync(mapper);
5709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5712 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5713 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5714 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5715 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5716 ASSERT_EQ(0, motionArgs.flags);
5717 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5718 ASSERT_EQ(0, motionArgs.buttonState);
5719 ASSERT_EQ(0, motionArgs.edgeFlags);
5720 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5724 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5725 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5726 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5728
5729 // Should not have sent any more keys or motions.
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5732}
5733
Santos Cordonfa5cf462017-04-05 10:37:00 -07005734TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005735 addConfigurationProperty("touch.deviceType", "touchScreen");
5736 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5737
5738 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5739 prepareButtons();
5740 prepareAxes(POSITION);
5741 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005742 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005743
arthurhungdcef2dc2020-08-11 14:47:50 +08005744 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005745
5746 NotifyMotionArgs motionArgs;
5747
5748 // Down.
5749 int32_t x = 100;
5750 int32_t y = 125;
5751 processDown(mapper, x, y);
5752 processSync(mapper);
5753
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5755 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5756 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5757 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5758 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5759 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5760 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5761 ASSERT_EQ(0, motionArgs.flags);
5762 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5763 ASSERT_EQ(0, motionArgs.buttonState);
5764 ASSERT_EQ(0, motionArgs.edgeFlags);
5765 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5766 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5767 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5769 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5770 1, 0, 0, 0, 0, 0, 0, 0));
5771 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5772 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5773 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5774
5775 // Move.
5776 x += 50;
5777 y += 75;
5778 processMove(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_MOVE, 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 // Up.
5803 processUp(mapper);
5804 processSync(mapper);
5805
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5808 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5809 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5810 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5811 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5812 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5813 ASSERT_EQ(0, motionArgs.flags);
5814 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5815 ASSERT_EQ(0, motionArgs.buttonState);
5816 ASSERT_EQ(0, motionArgs.edgeFlags);
5817 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5818 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5821 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5822 1, 0, 0, 0, 0, 0, 0, 0));
5823 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5824 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5825 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5826
5827 // Should not have sent any more keys or motions.
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5830}
5831
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005833 addConfigurationProperty("touch.deviceType", "touchScreen");
5834 prepareDisplay(DISPLAY_ORIENTATION_0);
5835 prepareButtons();
5836 prepareAxes(POSITION);
5837 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005838 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839
arthurhungdcef2dc2020-08-11 14:47:50 +08005840 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005841
5842 NotifyMotionArgs motionArgs;
5843
5844 // Down.
5845 int32_t x = 100;
5846 int32_t y = 125;
5847 processDown(mapper, x, y);
5848 processSync(mapper);
5849
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5851 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5852 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5853 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5854 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5855 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5856 ASSERT_EQ(0, motionArgs.flags);
5857 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5858 ASSERT_EQ(0, motionArgs.buttonState);
5859 ASSERT_EQ(0, motionArgs.edgeFlags);
5860 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5861 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5864 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5865 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5866 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5867 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5868
5869 // Move.
5870 x += 50;
5871 y += 75;
5872 processMove(mapper, x, y);
5873 processSync(mapper);
5874
5875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5876 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5877 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5878 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5879 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5881 ASSERT_EQ(0, motionArgs.flags);
5882 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5883 ASSERT_EQ(0, motionArgs.buttonState);
5884 ASSERT_EQ(0, motionArgs.edgeFlags);
5885 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5886 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5889 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5890 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5891 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5892 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5893
5894 // Up.
5895 processUp(mapper);
5896 processSync(mapper);
5897
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5899 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5900 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5901 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5902 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5903 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5904 ASSERT_EQ(0, motionArgs.flags);
5905 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5906 ASSERT_EQ(0, motionArgs.buttonState);
5907 ASSERT_EQ(0, motionArgs.edgeFlags);
5908 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5909 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5912 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5913 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5914 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5915 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5916
5917 // Should not have sent any more keys or motions.
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5920}
5921
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005922TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923 addConfigurationProperty("touch.deviceType", "touchScreen");
5924 prepareButtons();
5925 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005926 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5927 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005928 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929
5930 NotifyMotionArgs args;
5931
5932 // Rotation 90.
5933 prepareDisplay(DISPLAY_ORIENTATION_90);
5934 processDown(mapper, toRawX(50), toRawY(75));
5935 processSync(mapper);
5936
5937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5938 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5939 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5940
5941 processUp(mapper);
5942 processSync(mapper);
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5944}
5945
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005946TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947 addConfigurationProperty("touch.deviceType", "touchScreen");
5948 prepareButtons();
5949 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005950 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5951 // orientation-aware are affected by display rotation.
5952 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005953 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954
5955 NotifyMotionArgs args;
5956
5957 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005958 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959 prepareDisplay(DISPLAY_ORIENTATION_0);
5960 processDown(mapper, toRawX(50), toRawY(75));
5961 processSync(mapper);
5962
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5964 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5965 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5966
5967 processUp(mapper);
5968 processSync(mapper);
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5970
5971 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005972 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005973 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005974 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005975 processSync(mapper);
5976
5977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5978 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5979 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5980
5981 processUp(mapper);
5982 processSync(mapper);
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5984
5985 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005986 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005987 prepareDisplay(DISPLAY_ORIENTATION_180);
5988 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5989 processSync(mapper);
5990
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5992 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5993 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5994
5995 processUp(mapper);
5996 processSync(mapper);
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5998
5999 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006000 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006001 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006002 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 processSync(mapper);
6004
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6006 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6007 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6008
6009 processUp(mapper);
6010 processSync(mapper);
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6012}
6013
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006014TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6015 addConfigurationProperty("touch.deviceType", "touchScreen");
6016 prepareButtons();
6017 prepareAxes(POSITION);
6018 addConfigurationProperty("touch.orientationAware", "1");
6019 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6020 clearViewports();
6021 prepareDisplay(DISPLAY_ORIENTATION_0);
6022 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6023 NotifyMotionArgs args;
6024
6025 // Orientation 0.
6026 processDown(mapper, toRawX(50), toRawY(75));
6027 processSync(mapper);
6028
6029 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6030 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6031 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6032
6033 processUp(mapper);
6034 processSync(mapper);
6035 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6036}
6037
6038TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6039 addConfigurationProperty("touch.deviceType", "touchScreen");
6040 prepareButtons();
6041 prepareAxes(POSITION);
6042 addConfigurationProperty("touch.orientationAware", "1");
6043 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6044 clearViewports();
6045 prepareDisplay(DISPLAY_ORIENTATION_0);
6046 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6047 NotifyMotionArgs args;
6048
6049 // Orientation 90.
6050 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6051 processSync(mapper);
6052
6053 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6054 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6055 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6056
6057 processUp(mapper);
6058 processSync(mapper);
6059 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6060}
6061
6062TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6063 addConfigurationProperty("touch.deviceType", "touchScreen");
6064 prepareButtons();
6065 prepareAxes(POSITION);
6066 addConfigurationProperty("touch.orientationAware", "1");
6067 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6068 clearViewports();
6069 prepareDisplay(DISPLAY_ORIENTATION_0);
6070 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6071 NotifyMotionArgs args;
6072
6073 // Orientation 180.
6074 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6075 processSync(mapper);
6076
6077 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6078 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6079 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6080
6081 processUp(mapper);
6082 processSync(mapper);
6083 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6084}
6085
6086TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6087 addConfigurationProperty("touch.deviceType", "touchScreen");
6088 prepareButtons();
6089 prepareAxes(POSITION);
6090 addConfigurationProperty("touch.orientationAware", "1");
6091 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6092 clearViewports();
6093 prepareDisplay(DISPLAY_ORIENTATION_0);
6094 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6095 NotifyMotionArgs args;
6096
6097 // Orientation 270.
6098 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6099 processSync(mapper);
6100
6101 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6102 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6103 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6104
6105 processUp(mapper);
6106 processSync(mapper);
6107 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6108}
6109
6110TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6111 addConfigurationProperty("touch.deviceType", "touchScreen");
6112 prepareButtons();
6113 prepareAxes(POSITION);
6114 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6115 // orientation-aware are affected by display rotation.
6116 addConfigurationProperty("touch.orientationAware", "0");
6117 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6118 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6119
6120 NotifyMotionArgs args;
6121
6122 // Orientation 90, Rotation 0.
6123 clearViewports();
6124 prepareDisplay(DISPLAY_ORIENTATION_0);
6125 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
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 // Orientation 90, Rotation 90.
6137 clearViewports();
6138 prepareDisplay(DISPLAY_ORIENTATION_90);
6139 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6140 processSync(mapper);
6141
6142 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6143 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6144 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6145
6146 processUp(mapper);
6147 processSync(mapper);
6148 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6149
6150 // Orientation 90, Rotation 180.
6151 clearViewports();
6152 prepareDisplay(DISPLAY_ORIENTATION_180);
6153 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6154 processSync(mapper);
6155
6156 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6157 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6158 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6159
6160 processUp(mapper);
6161 processSync(mapper);
6162 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6163
6164 // Orientation 90, Rotation 270.
6165 clearViewports();
6166 prepareDisplay(DISPLAY_ORIENTATION_270);
6167 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6168 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6169 processSync(mapper);
6170
6171 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6172 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6173 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6174
6175 processUp(mapper);
6176 processSync(mapper);
6177 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6178}
6179
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181 addConfigurationProperty("touch.deviceType", "touchScreen");
6182 prepareDisplay(DISPLAY_ORIENTATION_0);
6183 prepareButtons();
6184 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006185 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186
6187 // These calculations are based on the input device calibration documentation.
6188 int32_t rawX = 100;
6189 int32_t rawY = 200;
6190 int32_t rawPressure = 10;
6191 int32_t rawToolMajor = 12;
6192 int32_t rawDistance = 2;
6193 int32_t rawTiltX = 30;
6194 int32_t rawTiltY = 110;
6195
6196 float x = toDisplayX(rawX);
6197 float y = toDisplayY(rawY);
6198 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6199 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6200 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6201 float distance = float(rawDistance);
6202
6203 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6204 float tiltScale = M_PI / 180;
6205 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6206 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6207 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6208 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6209
6210 processDown(mapper, rawX, rawY);
6211 processPressure(mapper, rawPressure);
6212 processToolMajor(mapper, rawToolMajor);
6213 processDistance(mapper, rawDistance);
6214 processTilt(mapper, rawTiltX, rawTiltY);
6215 processSync(mapper);
6216
6217 NotifyMotionArgs args;
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6220 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6221 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6222}
6223
Jason Gerecke489fda82012-09-07 17:19:40 -07006224TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006225 addConfigurationProperty("touch.deviceType", "touchScreen");
6226 prepareDisplay(DISPLAY_ORIENTATION_0);
6227 prepareLocationCalibration();
6228 prepareButtons();
6229 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006230 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006231
6232 int32_t rawX = 100;
6233 int32_t rawY = 200;
6234
6235 float x = toDisplayX(toCookedX(rawX, rawY));
6236 float y = toDisplayY(toCookedY(rawX, rawY));
6237
6238 processDown(mapper, rawX, rawY);
6239 processSync(mapper);
6240
6241 NotifyMotionArgs args;
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6244 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6245}
6246
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248 addConfigurationProperty("touch.deviceType", "touchScreen");
6249 prepareDisplay(DISPLAY_ORIENTATION_0);
6250 prepareButtons();
6251 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006252 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253
6254 NotifyMotionArgs motionArgs;
6255 NotifyKeyArgs keyArgs;
6256
6257 processDown(mapper, 100, 200);
6258 processSync(mapper);
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6261 ASSERT_EQ(0, motionArgs.buttonState);
6262
6263 // press BTN_LEFT, release BTN_LEFT
6264 processKey(mapper, BTN_LEFT, 1);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6268 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6269
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6272 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6273
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274 processKey(mapper, BTN_LEFT, 0);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283
6284 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6285 processKey(mapper, BTN_RIGHT, 1);
6286 processKey(mapper, BTN_MIDDLE, 1);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6290 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6291 motionArgs.buttonState);
6292
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6295 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6296
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6299 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6300 motionArgs.buttonState);
6301
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302 processKey(mapper, BTN_RIGHT, 0);
6303 processSync(mapper);
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006310 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311
6312 processKey(mapper, BTN_MIDDLE, 0);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321
6322 // press BTN_BACK, release BTN_BACK
6323 processKey(mapper, BTN_BACK, 1);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6326 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6327 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006328
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006331 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6332
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6334 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6335 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336
6337 processKey(mapper, BTN_BACK, 0);
6338 processSync(mapper);
6339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006340 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006345 ASSERT_EQ(0, motionArgs.buttonState);
6346
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6348 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6349 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6350
6351 // press BTN_SIDE, release BTN_SIDE
6352 processKey(mapper, BTN_SIDE, 1);
6353 processSync(mapper);
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6355 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6356 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006357
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6361
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6364 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365
6366 processKey(mapper, BTN_SIDE, 0);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006371
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006374 ASSERT_EQ(0, motionArgs.buttonState);
6375
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6377 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6378 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6379
6380 // press BTN_FORWARD, release BTN_FORWARD
6381 processKey(mapper, BTN_FORWARD, 1);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6384 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6385 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006386
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6390
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6393 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394
6395 processKey(mapper, BTN_FORWARD, 0);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006403 ASSERT_EQ(0, motionArgs.buttonState);
6404
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6406 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6407 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6408
6409 // press BTN_EXTRA, release BTN_EXTRA
6410 processKey(mapper, BTN_EXTRA, 1);
6411 processSync(mapper);
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6413 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6414 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006418 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6419
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6422 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423
6424 processKey(mapper, BTN_EXTRA, 0);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006432 ASSERT_EQ(0, motionArgs.buttonState);
6433
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6435 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6436 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6437
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6439
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440 // press BTN_STYLUS, release BTN_STYLUS
6441 processKey(mapper, BTN_STYLUS, 1);
6442 processSync(mapper);
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006445 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, 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_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450
6451 processKey(mapper, BTN_STYLUS, 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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460
6461 // press BTN_STYLUS2, release BTN_STYLUS2
6462 processKey(mapper, BTN_STYLUS2, 1);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006466 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6467
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6470 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471
6472 processKey(mapper, BTN_STYLUS2, 0);
6473 processSync(mapper);
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006476 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006477
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006480 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481
6482 // release touch
6483 processUp(mapper);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6487 ASSERT_EQ(0, motionArgs.buttonState);
6488}
6489
6490TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006491 addConfigurationProperty("touch.deviceType", "touchScreen");
6492 prepareDisplay(DISPLAY_ORIENTATION_0);
6493 prepareButtons();
6494 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006495 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496
6497 NotifyMotionArgs motionArgs;
6498
6499 // default tool type is finger
6500 processDown(mapper, 100, 200);
6501 processSync(mapper);
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6505
6506 // eraser
6507 processKey(mapper, BTN_TOOL_RUBBER, 1);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6512
6513 // stylus
6514 processKey(mapper, BTN_TOOL_RUBBER, 0);
6515 processKey(mapper, BTN_TOOL_PEN, 1);
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6520
6521 // brush
6522 processKey(mapper, BTN_TOOL_PEN, 0);
6523 processKey(mapper, BTN_TOOL_BRUSH, 1);
6524 processSync(mapper);
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6528
6529 // pencil
6530 processKey(mapper, BTN_TOOL_BRUSH, 0);
6531 processKey(mapper, BTN_TOOL_PENCIL, 1);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6536
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006537 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538 processKey(mapper, BTN_TOOL_PENCIL, 0);
6539 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6544
6545 // mouse
6546 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6547 processKey(mapper, BTN_TOOL_MOUSE, 1);
6548 processSync(mapper);
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6552
6553 // lens
6554 processKey(mapper, BTN_TOOL_MOUSE, 0);
6555 processKey(mapper, BTN_TOOL_LENS, 1);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6560
6561 // double-tap
6562 processKey(mapper, BTN_TOOL_LENS, 0);
6563 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6568
6569 // triple-tap
6570 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6571 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6576
6577 // quad-tap
6578 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6579 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6580 processSync(mapper);
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6584
6585 // finger
6586 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6587 processKey(mapper, BTN_TOOL_FINGER, 1);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6592
6593 // stylus trumps finger
6594 processKey(mapper, BTN_TOOL_PEN, 1);
6595 processSync(mapper);
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6599
6600 // eraser trumps stylus
6601 processKey(mapper, BTN_TOOL_RUBBER, 1);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6606
6607 // mouse trumps eraser
6608 processKey(mapper, BTN_TOOL_MOUSE, 1);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6613
6614 // back to default tool type
6615 processKey(mapper, BTN_TOOL_MOUSE, 0);
6616 processKey(mapper, BTN_TOOL_RUBBER, 0);
6617 processKey(mapper, BTN_TOOL_PEN, 0);
6618 processKey(mapper, BTN_TOOL_FINGER, 0);
6619 processSync(mapper);
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6623}
6624
6625TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006626 addConfigurationProperty("touch.deviceType", "touchScreen");
6627 prepareDisplay(DISPLAY_ORIENTATION_0);
6628 prepareButtons();
6629 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006630 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006631 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006632
6633 NotifyMotionArgs motionArgs;
6634
6635 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6636 processKey(mapper, BTN_TOOL_FINGER, 1);
6637 processMove(mapper, 100, 200);
6638 processSync(mapper);
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6640 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6642 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6643
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6645 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6647 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6648
6649 // move a little
6650 processMove(mapper, 150, 250);
6651 processSync(mapper);
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6655 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6656
6657 // down when BTN_TOUCH is pressed, pressure defaults to 1
6658 processKey(mapper, BTN_TOUCH, 1);
6659 processSync(mapper);
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6663 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6664
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6666 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6668 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6669
6670 // up when BTN_TOUCH is released, hover restored
6671 processKey(mapper, BTN_TOUCH, 0);
6672 processSync(mapper);
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6676 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6677
6678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6681 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6682
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6684 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6686 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6687
6688 // exit hover when pointer goes away
6689 processKey(mapper, BTN_TOOL_FINGER, 0);
6690 processSync(mapper);
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6694 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6695}
6696
6697TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006698 addConfigurationProperty("touch.deviceType", "touchScreen");
6699 prepareDisplay(DISPLAY_ORIENTATION_0);
6700 prepareButtons();
6701 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006702 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006703
6704 NotifyMotionArgs motionArgs;
6705
6706 // initially hovering because pressure is 0
6707 processDown(mapper, 100, 200);
6708 processPressure(mapper, 0);
6709 processSync(mapper);
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6711 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6713 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6714
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6718 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6719
6720 // move a little
6721 processMove(mapper, 150, 250);
6722 processSync(mapper);
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6724 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6726 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6727
6728 // down when pressure is non-zero
6729 processPressure(mapper, RAW_PRESSURE_MAX);
6730 processSync(mapper);
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6732 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6734 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6735
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6737 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6739 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6740
6741 // up when pressure becomes 0, hover restored
6742 processPressure(mapper, 0);
6743 processSync(mapper);
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6745 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6747 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6748
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6750 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6752 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6753
6754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6755 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6757 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6758
6759 // exit hover when pointer goes away
6760 processUp(mapper);
6761 processSync(mapper);
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6763 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6765 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6766}
6767
lilinnan687e58f2022-07-19 16:00:50 +08006768TEST_F(SingleTouchInputMapperTest,
6769 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6770 addConfigurationProperty("touch.deviceType", "touchScreen");
6771 prepareDisplay(DISPLAY_ORIENTATION_0);
6772 prepareButtons();
6773 prepareAxes(POSITION);
6774 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6775 NotifyMotionArgs motionArgs;
6776
6777 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006778 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006779 processSync(mapper);
6780
6781 // We should receive a down event
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6783 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6784
6785 // Change display id
6786 clearViewports();
6787 prepareSecondaryDisplay(ViewportType::INTERNAL);
6788
6789 // We should receive a cancel event
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6791 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6792 // Then receive reset called
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6794}
6795
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006796TEST_F(SingleTouchInputMapperTest,
6797 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6798 addConfigurationProperty("touch.deviceType", "touchScreen");
6799 prepareDisplay(DISPLAY_ORIENTATION_0);
6800 prepareButtons();
6801 prepareAxes(POSITION);
6802 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6804 NotifyMotionArgs motionArgs;
6805
6806 // Start a new gesture.
6807 processDown(mapper, 100, 200);
6808 processSync(mapper);
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6810 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6811
6812 // Make the viewport inactive. This will put the device in disabled mode.
6813 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6814 viewport->isActive = false;
6815 mFakePolicy->updateViewport(*viewport);
6816 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6817
6818 // We should receive a cancel event for the ongoing gesture.
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6820 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6821 // Then we should be notified that the device was reset.
6822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6823
6824 // No events are generated while the viewport is inactive.
6825 processMove(mapper, 101, 201);
6826 processSync(mapper);
6827 processDown(mapper, 102, 202);
6828 processSync(mapper);
6829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6830
6831 // Make the viewport active again. The device should resume processing events.
6832 viewport->isActive = true;
6833 mFakePolicy->updateViewport(*viewport);
6834 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6835
6836 // The device is reset because it changes back to direct mode, without generating any events.
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6839
6840 // Start a new gesture.
6841 processDown(mapper, 100, 200);
6842 processSync(mapper);
6843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6844 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6845
6846 // No more events.
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6849}
6850
Prabir Pradhan5632d622021-09-06 07:57:20 -07006851// --- TouchDisplayProjectionTest ---
6852
6853class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6854public:
6855 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6856 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6857 // rotated equivalent of the given un-rotated physical display bounds.
6858 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6859 uint32_t inverseRotationFlags;
6860 auto width = DISPLAY_WIDTH;
6861 auto height = DISPLAY_HEIGHT;
6862 switch (orientation) {
6863 case DISPLAY_ORIENTATION_90:
6864 inverseRotationFlags = ui::Transform::ROT_270;
6865 std::swap(width, height);
6866 break;
6867 case DISPLAY_ORIENTATION_180:
6868 inverseRotationFlags = ui::Transform::ROT_180;
6869 break;
6870 case DISPLAY_ORIENTATION_270:
6871 inverseRotationFlags = ui::Transform::ROT_90;
6872 std::swap(width, height);
6873 break;
6874 case DISPLAY_ORIENTATION_0:
6875 inverseRotationFlags = ui::Transform::ROT_0;
6876 break;
6877 default:
6878 FAIL() << "Invalid orientation: " << orientation;
6879 }
6880
6881 const ui::Transform rotation(inverseRotationFlags, width, height);
6882 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6883
6884 std::optional<DisplayViewport> internalViewport =
6885 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6886 DisplayViewport& v = *internalViewport;
6887 v.displayId = DISPLAY_ID;
6888 v.orientation = orientation;
6889
6890 v.logicalLeft = 0;
6891 v.logicalTop = 0;
6892 v.logicalRight = 100;
6893 v.logicalBottom = 100;
6894
6895 v.physicalLeft = rotatedPhysicalDisplay.left;
6896 v.physicalTop = rotatedPhysicalDisplay.top;
6897 v.physicalRight = rotatedPhysicalDisplay.right;
6898 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6899
6900 v.deviceWidth = width;
6901 v.deviceHeight = height;
6902
6903 v.isActive = true;
6904 v.uniqueId = UNIQUE_ID;
6905 v.type = ViewportType::INTERNAL;
6906 mFakePolicy->updateViewport(v);
6907 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6908 }
6909
6910 void assertReceivedMove(const Point& point) {
6911 NotifyMotionArgs motionArgs;
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6914 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6916 1, 0, 0, 0, 0, 0, 0, 0));
6917 }
6918};
6919
6920TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6921 addConfigurationProperty("touch.deviceType", "touchScreen");
6922 prepareDisplay(DISPLAY_ORIENTATION_0);
6923
6924 prepareButtons();
6925 prepareAxes(POSITION);
6926 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6927
6928 NotifyMotionArgs motionArgs;
6929
6930 // Configure the DisplayViewport such that the logical display maps to a subsection of
6931 // the display panel called the physical display. Here, the physical display is bounded by the
6932 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6933 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6934 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6935 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6936
6937 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6938 DISPLAY_ORIENTATION_270}) {
6939 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6940
6941 // Touches outside the physical display should be ignored, and should not generate any
6942 // events. Ensure touches at the following points that lie outside of the physical display
6943 // area do not generate any events.
6944 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6945 processDown(mapper, toRawX(point.x), toRawY(point.y));
6946 processSync(mapper);
6947 processUp(mapper);
6948 processSync(mapper);
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6950 << "Unexpected event generated for touch outside physical display at point: "
6951 << point.x << ", " << point.y;
6952 }
6953 }
6954}
6955
6956TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6957 addConfigurationProperty("touch.deviceType", "touchScreen");
6958 prepareDisplay(DISPLAY_ORIENTATION_0);
6959
6960 prepareButtons();
6961 prepareAxes(POSITION);
6962 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6963
6964 NotifyMotionArgs motionArgs;
6965
6966 // Configure the DisplayViewport such that the logical display maps to a subsection of
6967 // the display panel called the physical display. Here, the physical display is bounded by the
6968 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6969 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6970
6971 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6972 DISPLAY_ORIENTATION_270}) {
6973 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6974
6975 // Touches that start outside the physical display should be ignored until it enters the
6976 // physical display bounds, at which point it should generate a down event. Start a touch at
6977 // the point (5, 100), which is outside the physical display bounds.
6978 static const Point kOutsidePoint{5, 100};
6979 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6980 processSync(mapper);
6981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6982
6983 // Move the touch into the physical display area. This should generate a pointer down.
6984 processMove(mapper, toRawX(11), toRawY(21));
6985 processSync(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6988 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6989 ASSERT_NO_FATAL_FAILURE(
6990 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6991
6992 // Move the touch inside the physical display area. This should generate a pointer move.
6993 processMove(mapper, toRawX(69), toRawY(159));
6994 processSync(mapper);
6995 assertReceivedMove({69, 159});
6996
6997 // Move outside the physical display area. Since the pointer is already down, this should
6998 // now continue generating events.
6999 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7000 processSync(mapper);
7001 assertReceivedMove(kOutsidePoint);
7002
7003 // Release. This should generate a pointer up.
7004 processUp(mapper);
7005 processSync(mapper);
7006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7007 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7009 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7010
7011 // Ensure no more events were generated.
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7014 }
7015}
7016
Michael Wrightd02c5b62014-02-10 15:10:22 -08007017// --- MultiTouchInputMapperTest ---
7018
7019class MultiTouchInputMapperTest : public TouchInputMapperTest {
7020protected:
7021 void prepareAxes(int axes);
7022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007023 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7024 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7025 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7026 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7027 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7028 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7029 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7030 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7031 void processId(MultiTouchInputMapper& mapper, int32_t id);
7032 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7033 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7034 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7035 void processMTSync(MultiTouchInputMapper& mapper);
7036 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007037};
7038
7039void MultiTouchInputMapperTest::prepareAxes(int axes) {
7040 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007041 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7042 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007043 }
7044 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007045 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7046 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007047 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007048 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7049 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050 }
7051 }
7052 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007053 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7054 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007056 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007057 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007058 }
7059 }
7060 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007061 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7062 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007063 }
7064 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007065 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7066 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 }
7068 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007069 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7070 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071 }
7072 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007073 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7074 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075 }
7076 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007077 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7078 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007079 }
7080 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007081 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082 }
7083}
7084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007085void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7086 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007089}
7090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007091void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7092 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094}
7095
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007096void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7097 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099}
7100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007101void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103}
7104
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007105void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007107}
7108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007109void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7110 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112}
7113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007114void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116}
7117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007118void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120}
7121
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007122void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124}
7125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007126void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128}
7129
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007130void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132}
7133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007134void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7135 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137}
7138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007139void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141}
7142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007143void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145}
7146
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007148 addConfigurationProperty("touch.deviceType", "touchScreen");
7149 prepareDisplay(DISPLAY_ORIENTATION_0);
7150 prepareAxes(POSITION);
7151 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007152 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007153
arthurhungdcef2dc2020-08-11 14:47:50 +08007154 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155
7156 NotifyMotionArgs motionArgs;
7157
7158 // Two fingers down at once.
7159 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7160 processPosition(mapper, x1, y1);
7161 processMTSync(mapper);
7162 processPosition(mapper, x2, y2);
7163 processMTSync(mapper);
7164 processSync(mapper);
7165
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7167 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7168 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7169 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7170 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7171 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7172 ASSERT_EQ(0, motionArgs.flags);
7173 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7174 ASSERT_EQ(0, motionArgs.buttonState);
7175 ASSERT_EQ(0, motionArgs.edgeFlags);
7176 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7177 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7180 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7181 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7182 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7183 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7184
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7186 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7187 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7188 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7189 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007190 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007191 ASSERT_EQ(0, motionArgs.flags);
7192 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7193 ASSERT_EQ(0, motionArgs.buttonState);
7194 ASSERT_EQ(0, motionArgs.edgeFlags);
7195 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7196 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7198 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7201 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7203 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7204 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7205 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7207
7208 // Move.
7209 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7210 processPosition(mapper, x1, y1);
7211 processMTSync(mapper);
7212 processPosition(mapper, x2, y2);
7213 processMTSync(mapper);
7214 processSync(mapper);
7215
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7218 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7219 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7220 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7222 ASSERT_EQ(0, motionArgs.flags);
7223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7224 ASSERT_EQ(0, motionArgs.buttonState);
7225 ASSERT_EQ(0, motionArgs.edgeFlags);
7226 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7229 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7232 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7234 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7235 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7236 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7238
7239 // First finger up.
7240 x2 += 15; y2 -= 20;
7241 processPosition(mapper, x2, y2);
7242 processMTSync(mapper);
7243 processSync(mapper);
7244
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7247 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7248 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7249 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007250 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007251 ASSERT_EQ(0, motionArgs.flags);
7252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7253 ASSERT_EQ(0, motionArgs.buttonState);
7254 ASSERT_EQ(0, motionArgs.edgeFlags);
7255 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7258 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7261 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7263 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7264 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7265 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7266 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7267
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7269 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7270 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7274 ASSERT_EQ(0, motionArgs.flags);
7275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7276 ASSERT_EQ(0, motionArgs.buttonState);
7277 ASSERT_EQ(0, motionArgs.edgeFlags);
7278 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7279 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7282 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7283 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7284 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7286
7287 // Move.
7288 x2 += 20; y2 -= 25;
7289 processPosition(mapper, x2, y2);
7290 processMTSync(mapper);
7291 processSync(mapper);
7292
7293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7295 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7296 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7297 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7299 ASSERT_EQ(0, motionArgs.flags);
7300 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7301 ASSERT_EQ(0, motionArgs.buttonState);
7302 ASSERT_EQ(0, motionArgs.edgeFlags);
7303 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7304 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7307 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7308 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7309 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7311
7312 // New finger down.
7313 int32_t x3 = 700, y3 = 300;
7314 processPosition(mapper, x2, y2);
7315 processMTSync(mapper);
7316 processPosition(mapper, x3, y3);
7317 processMTSync(mapper);
7318 processSync(mapper);
7319
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007325 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007326 ASSERT_EQ(0, motionArgs.flags);
7327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7328 ASSERT_EQ(0, motionArgs.buttonState);
7329 ASSERT_EQ(0, motionArgs.edgeFlags);
7330 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7333 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7336 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7338 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7339 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7340 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7342
7343 // Second finger up.
7344 x3 += 30; y3 -= 20;
7345 processPosition(mapper, x3, y3);
7346 processMTSync(mapper);
7347 processSync(mapper);
7348
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007354 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007355 ASSERT_EQ(0, motionArgs.flags);
7356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7357 ASSERT_EQ(0, motionArgs.buttonState);
7358 ASSERT_EQ(0, motionArgs.edgeFlags);
7359 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7362 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7365 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7367 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7368 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7369 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7370 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7371
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7374 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7375 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7376 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7378 ASSERT_EQ(0, motionArgs.flags);
7379 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7380 ASSERT_EQ(0, motionArgs.buttonState);
7381 ASSERT_EQ(0, motionArgs.edgeFlags);
7382 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7383 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7386 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7387 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7388 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7390
7391 // Last finger up.
7392 processMTSync(mapper);
7393 processSync(mapper);
7394
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7397 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7398 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7399 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7400 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7401 ASSERT_EQ(0, motionArgs.flags);
7402 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7403 ASSERT_EQ(0, motionArgs.buttonState);
7404 ASSERT_EQ(0, motionArgs.edgeFlags);
7405 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7406 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7409 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7410 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7411 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7412 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7413
7414 // Should not have sent any more keys or motions.
7415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7417}
7418
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007419TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7420 addConfigurationProperty("touch.deviceType", "touchScreen");
7421 prepareDisplay(DISPLAY_ORIENTATION_0);
7422
7423 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7424 /*fuzz*/ 0, /*resolution*/ 10);
7425 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7426 /*fuzz*/ 0, /*resolution*/ 11);
7427 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7428 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7429 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7430 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7431 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7432 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7433 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7434 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7435
7436 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7437
7438 // X and Y axes
7439 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7440 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7441 // Touch major and minor
7442 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7443 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7444 // Tool major and minor
7445 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7446 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7447}
7448
7449TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7450 addConfigurationProperty("touch.deviceType", "touchScreen");
7451 prepareDisplay(DISPLAY_ORIENTATION_0);
7452
7453 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7454 /*fuzz*/ 0, /*resolution*/ 10);
7455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7456 /*fuzz*/ 0, /*resolution*/ 11);
7457
7458 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7459
7460 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7461
7462 // Touch major and minor
7463 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7464 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7465 // Tool major and minor
7466 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7467 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7468}
7469
Michael Wrightd02c5b62014-02-10 15:10:22 -08007470TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007471 addConfigurationProperty("touch.deviceType", "touchScreen");
7472 prepareDisplay(DISPLAY_ORIENTATION_0);
7473 prepareAxes(POSITION | ID);
7474 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007475 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007476
arthurhungdcef2dc2020-08-11 14:47:50 +08007477 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007478
7479 NotifyMotionArgs motionArgs;
7480
7481 // Two fingers down at once.
7482 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7483 processPosition(mapper, x1, y1);
7484 processId(mapper, 1);
7485 processMTSync(mapper);
7486 processPosition(mapper, x2, y2);
7487 processId(mapper, 2);
7488 processMTSync(mapper);
7489 processSync(mapper);
7490
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7492 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7493 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7497 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7498
7499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007500 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007501 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7502 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7504 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7507 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7509 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7510
7511 // Move.
7512 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7513 processPosition(mapper, x1, y1);
7514 processId(mapper, 1);
7515 processMTSync(mapper);
7516 processPosition(mapper, x2, y2);
7517 processId(mapper, 2);
7518 processMTSync(mapper);
7519 processSync(mapper);
7520
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7523 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7524 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7526 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7529 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7531 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7532
7533 // First finger up.
7534 x2 += 15; y2 -= 20;
7535 processPosition(mapper, x2, y2);
7536 processId(mapper, 2);
7537 processMTSync(mapper);
7538 processSync(mapper);
7539
7540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007541 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007542 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7543 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7545 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7548 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7550 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7551
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7554 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7555 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7558 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7559
7560 // Move.
7561 x2 += 20; y2 -= 25;
7562 processPosition(mapper, x2, y2);
7563 processId(mapper, 2);
7564 processMTSync(mapper);
7565 processSync(mapper);
7566
7567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7569 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7570 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7573 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7574
7575 // New finger down.
7576 int32_t x3 = 700, y3 = 300;
7577 processPosition(mapper, x2, y2);
7578 processId(mapper, 2);
7579 processMTSync(mapper);
7580 processPosition(mapper, x3, y3);
7581 processId(mapper, 3);
7582 processMTSync(mapper);
7583 processSync(mapper);
7584
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007586 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7588 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7590 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7593 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7595 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7596
7597 // Second finger up.
7598 x3 += 30; y3 -= 20;
7599 processPosition(mapper, x3, y3);
7600 processId(mapper, 3);
7601 processMTSync(mapper);
7602 processSync(mapper);
7603
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007605 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007606 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7607 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7609 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7612 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7614 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7615
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7618 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7619 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7622 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7623
7624 // Last finger up.
7625 processMTSync(mapper);
7626 processSync(mapper);
7627
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7629 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7630 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7631 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7634 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7635
7636 // Should not have sent any more keys or motions.
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7639}
7640
7641TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007642 addConfigurationProperty("touch.deviceType", "touchScreen");
7643 prepareDisplay(DISPLAY_ORIENTATION_0);
7644 prepareAxes(POSITION | ID | SLOT);
7645 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007647
arthurhungdcef2dc2020-08-11 14:47:50 +08007648 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007649
7650 NotifyMotionArgs motionArgs;
7651
7652 // Two fingers down at once.
7653 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7654 processPosition(mapper, x1, y1);
7655 processId(mapper, 1);
7656 processSlot(mapper, 1);
7657 processPosition(mapper, x2, y2);
7658 processId(mapper, 2);
7659 processSync(mapper);
7660
7661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7663 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7664 ASSERT_EQ(0, 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(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7668
7669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007670 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007671 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7672 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7674 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7677 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7679 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7680
7681 // Move.
7682 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7683 processSlot(mapper, 0);
7684 processPosition(mapper, x1, y1);
7685 processSlot(mapper, 1);
7686 processPosition(mapper, x2, y2);
7687 processSync(mapper);
7688
7689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7691 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7692 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7694 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7697 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7699 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7700
7701 // First finger up.
7702 x2 += 15; y2 -= 20;
7703 processSlot(mapper, 0);
7704 processId(mapper, -1);
7705 processSlot(mapper, 1);
7706 processPosition(mapper, x2, y2);
7707 processSync(mapper);
7708
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007710 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7712 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7714 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7717 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7719 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7720
7721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7723 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7724 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7727 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7728
7729 // Move.
7730 x2 += 20; y2 -= 25;
7731 processPosition(mapper, x2, y2);
7732 processSync(mapper);
7733
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7736 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7737 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7740 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7741
7742 // New finger down.
7743 int32_t x3 = 700, y3 = 300;
7744 processPosition(mapper, x2, y2);
7745 processSlot(mapper, 0);
7746 processId(mapper, 3);
7747 processPosition(mapper, x3, y3);
7748 processSync(mapper);
7749
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007751 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007752 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7755 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7758 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7760 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7761
7762 // Second finger up.
7763 x3 += 30; y3 -= 20;
7764 processSlot(mapper, 1);
7765 processId(mapper, -1);
7766 processSlot(mapper, 0);
7767 processPosition(mapper, x3, y3);
7768 processSync(mapper);
7769
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007771 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7775 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7778 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7780 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7781
7782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7784 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7785 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7788 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7789
7790 // Last finger up.
7791 processId(mapper, -1);
7792 processSync(mapper);
7793
7794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7795 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7796 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7797 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7800 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7801
7802 // Should not have sent any more keys or motions.
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7805}
7806
7807TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007808 addConfigurationProperty("touch.deviceType", "touchScreen");
7809 prepareDisplay(DISPLAY_ORIENTATION_0);
7810 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007811 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007812
7813 // These calculations are based on the input device calibration documentation.
7814 int32_t rawX = 100;
7815 int32_t rawY = 200;
7816 int32_t rawTouchMajor = 7;
7817 int32_t rawTouchMinor = 6;
7818 int32_t rawToolMajor = 9;
7819 int32_t rawToolMinor = 8;
7820 int32_t rawPressure = 11;
7821 int32_t rawDistance = 0;
7822 int32_t rawOrientation = 3;
7823 int32_t id = 5;
7824
7825 float x = toDisplayX(rawX);
7826 float y = toDisplayY(rawY);
7827 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7828 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7829 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7830 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7831 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7832 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7833 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7834 float distance = float(rawDistance);
7835
7836 processPosition(mapper, rawX, rawY);
7837 processTouchMajor(mapper, rawTouchMajor);
7838 processTouchMinor(mapper, rawTouchMinor);
7839 processToolMajor(mapper, rawToolMajor);
7840 processToolMinor(mapper, rawToolMinor);
7841 processPressure(mapper, rawPressure);
7842 processOrientation(mapper, rawOrientation);
7843 processDistance(mapper, rawDistance);
7844 processId(mapper, id);
7845 processMTSync(mapper);
7846 processSync(mapper);
7847
7848 NotifyMotionArgs args;
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7850 ASSERT_EQ(0, args.pointerProperties[0].id);
7851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7852 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7853 orientation, distance));
7854}
7855
7856TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007857 addConfigurationProperty("touch.deviceType", "touchScreen");
7858 prepareDisplay(DISPLAY_ORIENTATION_0);
7859 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7860 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007861 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862
7863 // These calculations are based on the input device calibration documentation.
7864 int32_t rawX = 100;
7865 int32_t rawY = 200;
7866 int32_t rawTouchMajor = 140;
7867 int32_t rawTouchMinor = 120;
7868 int32_t rawToolMajor = 180;
7869 int32_t rawToolMinor = 160;
7870
7871 float x = toDisplayX(rawX);
7872 float y = toDisplayY(rawY);
7873 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7874 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7875 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7876 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7877 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7878
7879 processPosition(mapper, rawX, rawY);
7880 processTouchMajor(mapper, rawTouchMajor);
7881 processTouchMinor(mapper, rawTouchMinor);
7882 processToolMajor(mapper, rawToolMajor);
7883 processToolMinor(mapper, rawToolMinor);
7884 processMTSync(mapper);
7885 processSync(mapper);
7886
7887 NotifyMotionArgs args;
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7890 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7891}
7892
7893TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007894 addConfigurationProperty("touch.deviceType", "touchScreen");
7895 prepareDisplay(DISPLAY_ORIENTATION_0);
7896 prepareAxes(POSITION | TOUCH | TOOL);
7897 addConfigurationProperty("touch.size.calibration", "diameter");
7898 addConfigurationProperty("touch.size.scale", "10");
7899 addConfigurationProperty("touch.size.bias", "160");
7900 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007901 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902
7903 // These calculations are based on the input device calibration documentation.
7904 // Note: We only provide a single common touch/tool value because the device is assumed
7905 // not to emit separate values for each pointer (isSummed = 1).
7906 int32_t rawX = 100;
7907 int32_t rawY = 200;
7908 int32_t rawX2 = 150;
7909 int32_t rawY2 = 250;
7910 int32_t rawTouchMajor = 5;
7911 int32_t rawToolMajor = 8;
7912
7913 float x = toDisplayX(rawX);
7914 float y = toDisplayY(rawY);
7915 float x2 = toDisplayX(rawX2);
7916 float y2 = toDisplayY(rawY2);
7917 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7918 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7919 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7920
7921 processPosition(mapper, rawX, rawY);
7922 processTouchMajor(mapper, rawTouchMajor);
7923 processToolMajor(mapper, rawToolMajor);
7924 processMTSync(mapper);
7925 processPosition(mapper, rawX2, rawY2);
7926 processTouchMajor(mapper, rawTouchMajor);
7927 processToolMajor(mapper, rawToolMajor);
7928 processMTSync(mapper);
7929 processSync(mapper);
7930
7931 NotifyMotionArgs args;
7932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7933 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7934
7935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007936 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937 ASSERT_EQ(size_t(2), args.pointerCount);
7938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7939 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7941 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7942}
7943
7944TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007945 addConfigurationProperty("touch.deviceType", "touchScreen");
7946 prepareDisplay(DISPLAY_ORIENTATION_0);
7947 prepareAxes(POSITION | TOUCH | TOOL);
7948 addConfigurationProperty("touch.size.calibration", "area");
7949 addConfigurationProperty("touch.size.scale", "43");
7950 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007951 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952
7953 // These calculations are based on the input device calibration documentation.
7954 int32_t rawX = 100;
7955 int32_t rawY = 200;
7956 int32_t rawTouchMajor = 5;
7957 int32_t rawToolMajor = 8;
7958
7959 float x = toDisplayX(rawX);
7960 float y = toDisplayY(rawY);
7961 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7962 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7963 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7964
7965 processPosition(mapper, rawX, rawY);
7966 processTouchMajor(mapper, rawTouchMajor);
7967 processToolMajor(mapper, rawToolMajor);
7968 processMTSync(mapper);
7969 processSync(mapper);
7970
7971 NotifyMotionArgs args;
7972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7974 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7975}
7976
7977TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007978 addConfigurationProperty("touch.deviceType", "touchScreen");
7979 prepareDisplay(DISPLAY_ORIENTATION_0);
7980 prepareAxes(POSITION | PRESSURE);
7981 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7982 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007983 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984
Michael Wrightaa449c92017-12-13 21:21:43 +00007985 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007986 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007987 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7988 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7989 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7990
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 // These calculations are based on the input device calibration documentation.
7992 int32_t rawX = 100;
7993 int32_t rawY = 200;
7994 int32_t rawPressure = 60;
7995
7996 float x = toDisplayX(rawX);
7997 float y = toDisplayY(rawY);
7998 float pressure = float(rawPressure) * 0.01f;
7999
8000 processPosition(mapper, rawX, rawY);
8001 processPressure(mapper, rawPressure);
8002 processMTSync(mapper);
8003 processSync(mapper);
8004
8005 NotifyMotionArgs args;
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8008 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8009}
8010
8011TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012 addConfigurationProperty("touch.deviceType", "touchScreen");
8013 prepareDisplay(DISPLAY_ORIENTATION_0);
8014 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008015 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016
8017 NotifyMotionArgs motionArgs;
8018 NotifyKeyArgs keyArgs;
8019
8020 processId(mapper, 1);
8021 processPosition(mapper, 100, 200);
8022 processSync(mapper);
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8024 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8025 ASSERT_EQ(0, motionArgs.buttonState);
8026
8027 // press BTN_LEFT, release BTN_LEFT
8028 processKey(mapper, BTN_LEFT, 1);
8029 processSync(mapper);
8030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8032 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8033
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8036 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8037
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 processKey(mapper, BTN_LEFT, 0);
8039 processSync(mapper);
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008041 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008043
8044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008046 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047
8048 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8049 processKey(mapper, BTN_RIGHT, 1);
8050 processKey(mapper, BTN_MIDDLE, 1);
8051 processSync(mapper);
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8054 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8055 motionArgs.buttonState);
8056
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8059 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8060
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8062 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8063 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8064 motionArgs.buttonState);
8065
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 processKey(mapper, BTN_RIGHT, 0);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008071
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008074 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075
8076 processKey(mapper, BTN_MIDDLE, 0);
8077 processSync(mapper);
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008079 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008081
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008084 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085
8086 // press BTN_BACK, release BTN_BACK
8087 processKey(mapper, BTN_BACK, 1);
8088 processSync(mapper);
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8090 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8091 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008092
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008095 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8096
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8099 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100
8101 processKey(mapper, BTN_BACK, 0);
8102 processSync(mapper);
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008105 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008106
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008109 ASSERT_EQ(0, motionArgs.buttonState);
8110
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8112 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8113 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8114
8115 // press BTN_SIDE, release BTN_SIDE
8116 processKey(mapper, BTN_SIDE, 1);
8117 processSync(mapper);
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8119 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8120 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008121
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008124 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8125
8126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8128 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129
8130 processKey(mapper, BTN_SIDE, 0);
8131 processSync(mapper);
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008135
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008138 ASSERT_EQ(0, motionArgs.buttonState);
8139
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8141 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8142 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8143
8144 // press BTN_FORWARD, release BTN_FORWARD
8145 processKey(mapper, BTN_FORWARD, 1);
8146 processSync(mapper);
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8148 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8149 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008150
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008153 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8154
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8157 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158
8159 processKey(mapper, BTN_FORWARD, 0);
8160 processSync(mapper);
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008164
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008167 ASSERT_EQ(0, motionArgs.buttonState);
8168
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8170 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8171 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8172
8173 // press BTN_EXTRA, release BTN_EXTRA
8174 processKey(mapper, BTN_EXTRA, 1);
8175 processSync(mapper);
8176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8177 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8178 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008179
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008182 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8183
8184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8186 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187
8188 processKey(mapper, BTN_EXTRA, 0);
8189 processSync(mapper);
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008193
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008196 ASSERT_EQ(0, motionArgs.buttonState);
8197
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8199 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8200 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8201
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8203
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 // press BTN_STYLUS, release BTN_STYLUS
8205 processKey(mapper, BTN_STYLUS, 1);
8206 processSync(mapper);
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8210
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214
8215 processKey(mapper, BTN_STYLUS, 0);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224
8225 // press BTN_STYLUS2, release BTN_STYLUS2
8226 processKey(mapper, BTN_STYLUS2, 1);
8227 processSync(mapper);
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008230 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8231
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8234 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008235
8236 processKey(mapper, BTN_STYLUS2, 0);
8237 processSync(mapper);
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008241
8242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008244 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245
8246 // release touch
8247 processId(mapper, -1);
8248 processSync(mapper);
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8250 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8251 ASSERT_EQ(0, motionArgs.buttonState);
8252}
8253
8254TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008255 addConfigurationProperty("touch.deviceType", "touchScreen");
8256 prepareDisplay(DISPLAY_ORIENTATION_0);
8257 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008258 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259
8260 NotifyMotionArgs motionArgs;
8261
8262 // default tool type is finger
8263 processId(mapper, 1);
8264 processPosition(mapper, 100, 200);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8267 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8269
8270 // eraser
8271 processKey(mapper, BTN_TOOL_RUBBER, 1);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8276
8277 // stylus
8278 processKey(mapper, BTN_TOOL_RUBBER, 0);
8279 processKey(mapper, BTN_TOOL_PEN, 1);
8280 processSync(mapper);
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8284
8285 // brush
8286 processKey(mapper, BTN_TOOL_PEN, 0);
8287 processKey(mapper, BTN_TOOL_BRUSH, 1);
8288 processSync(mapper);
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8292
8293 // pencil
8294 processKey(mapper, BTN_TOOL_BRUSH, 0);
8295 processKey(mapper, BTN_TOOL_PENCIL, 1);
8296 processSync(mapper);
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8300
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008301 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008302 processKey(mapper, BTN_TOOL_PENCIL, 0);
8303 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8304 processSync(mapper);
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8308
8309 // mouse
8310 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8311 processKey(mapper, BTN_TOOL_MOUSE, 1);
8312 processSync(mapper);
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8316
8317 // lens
8318 processKey(mapper, BTN_TOOL_MOUSE, 0);
8319 processKey(mapper, BTN_TOOL_LENS, 1);
8320 processSync(mapper);
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8324
8325 // double-tap
8326 processKey(mapper, BTN_TOOL_LENS, 0);
8327 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8332
8333 // triple-tap
8334 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8335 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8336 processSync(mapper);
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8340
8341 // quad-tap
8342 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8343 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8348
8349 // finger
8350 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8351 processKey(mapper, BTN_TOOL_FINGER, 1);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8356
8357 // stylus trumps finger
8358 processKey(mapper, BTN_TOOL_PEN, 1);
8359 processSync(mapper);
8360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8363
8364 // eraser trumps stylus
8365 processKey(mapper, BTN_TOOL_RUBBER, 1);
8366 processSync(mapper);
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8370
8371 // mouse trumps eraser
8372 processKey(mapper, BTN_TOOL_MOUSE, 1);
8373 processSync(mapper);
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8377
8378 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8379 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8380 processSync(mapper);
8381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8384
8385 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8386 processToolType(mapper, MT_TOOL_PEN);
8387 processSync(mapper);
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8391
8392 // back to default tool type
8393 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8394 processKey(mapper, BTN_TOOL_MOUSE, 0);
8395 processKey(mapper, BTN_TOOL_RUBBER, 0);
8396 processKey(mapper, BTN_TOOL_PEN, 0);
8397 processKey(mapper, BTN_TOOL_FINGER, 0);
8398 processSync(mapper);
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8402}
8403
8404TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008405 addConfigurationProperty("touch.deviceType", "touchScreen");
8406 prepareDisplay(DISPLAY_ORIENTATION_0);
8407 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008408 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008409 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008410
8411 NotifyMotionArgs motionArgs;
8412
8413 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8414 processId(mapper, 1);
8415 processPosition(mapper, 100, 200);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8420 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8421
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8425 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8426
8427 // move a little
8428 processPosition(mapper, 150, 250);
8429 processSync(mapper);
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8433 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8434
8435 // down when BTN_TOUCH is pressed, pressure defaults to 1
8436 processKey(mapper, BTN_TOUCH, 1);
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8441 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8442
8443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8444 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8446 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8447
8448 // up when BTN_TOUCH is released, hover restored
8449 processKey(mapper, BTN_TOUCH, 0);
8450 processSync(mapper);
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8454 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8455
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8457 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8459 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8460
8461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8462 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8464 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8465
8466 // exit hover when pointer goes away
8467 processId(mapper, -1);
8468 processSync(mapper);
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8472 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8473}
8474
8475TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476 addConfigurationProperty("touch.deviceType", "touchScreen");
8477 prepareDisplay(DISPLAY_ORIENTATION_0);
8478 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008479 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008480
8481 NotifyMotionArgs motionArgs;
8482
8483 // initially hovering because pressure is 0
8484 processId(mapper, 1);
8485 processPosition(mapper, 100, 200);
8486 processPressure(mapper, 0);
8487 processSync(mapper);
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8491 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8492
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8496 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8497
8498 // move a little
8499 processPosition(mapper, 150, 250);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8504 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8505
8506 // down when pressure becomes non-zero
8507 processPressure(mapper, RAW_PRESSURE_MAX);
8508 processSync(mapper);
8509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8512 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8513
8514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8515 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8517 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8518
8519 // up when pressure becomes 0, hover restored
8520 processPressure(mapper, 0);
8521 processSync(mapper);
8522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8523 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8525 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8526
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8528 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8530 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8531
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8536
8537 // exit hover when pointer goes away
8538 processId(mapper, -1);
8539 processSync(mapper);
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8543 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8544}
8545
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008546/**
8547 * Set the input device port <--> display port associations, and check that the
8548 * events are routed to the display that matches the display port.
8549 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8550 */
8551TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008552 const std::string usb2 = "USB2";
8553 const uint8_t hdmi1 = 0;
8554 const uint8_t hdmi2 = 1;
8555 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008556 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008557
8558 addConfigurationProperty("touch.deviceType", "touchScreen");
8559 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008560 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008561
8562 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8563 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8564
8565 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8566 // for this input device is specified, and the matching viewport is not present,
8567 // the input device should be disabled (at the mapper level).
8568
8569 // Add viewport for display 2 on hdmi2
8570 prepareSecondaryDisplay(type, hdmi2);
8571 // Send a touch event
8572 processPosition(mapper, 100, 100);
8573 processSync(mapper);
8574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8575
8576 // Add viewport for display 1 on hdmi1
8577 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8578 // Send a touch event again
8579 processPosition(mapper, 100, 100);
8580 processSync(mapper);
8581
8582 NotifyMotionArgs args;
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8584 ASSERT_EQ(DISPLAY_ID, args.displayId);
8585}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008586
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008587TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8588 addConfigurationProperty("touch.deviceType", "touchScreen");
8589 prepareAxes(POSITION);
8590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8591
8592 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8593
8594 prepareDisplay(DISPLAY_ORIENTATION_0);
8595 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8596
8597 // Send a touch event
8598 processPosition(mapper, 100, 100);
8599 processSync(mapper);
8600
8601 NotifyMotionArgs args;
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8603 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8604}
8605
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008606TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008607 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008608 std::shared_ptr<FakePointerController> fakePointerController =
8609 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008610 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008611 fakePointerController->setPosition(100, 200);
8612 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008613 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008614
Garfield Tan888a6a42020-01-09 11:39:16 -08008615 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008616 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008617
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008618 prepareDisplay(DISPLAY_ORIENTATION_0);
8619 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008620 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008621
8622 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008623 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008624
8625 NotifyMotionArgs motionArgs;
8626 processPosition(mapper, 100, 100);
8627 processSync(mapper);
8628
8629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8630 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8631 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8632}
8633
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008634/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008635 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8636 */
8637TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8638 addConfigurationProperty("touch.deviceType", "touchScreen");
8639 prepareAxes(POSITION);
8640 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8641
8642 prepareDisplay(DISPLAY_ORIENTATION_0);
8643 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8644 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8645 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8646 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8647
8648 NotifyMotionArgs args;
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8650 ASSERT_EQ(26, args.readTime);
8651
8652 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8653 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8654 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8655
8656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8657 ASSERT_EQ(33, args.readTime);
8658}
8659
8660/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008661 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8662 * events should not be delivered to the listener.
8663 */
8664TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8665 addConfigurationProperty("touch.deviceType", "touchScreen");
8666 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8667 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8668 ViewportType::INTERNAL);
8669 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8670 prepareAxes(POSITION);
8671 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8672
8673 NotifyMotionArgs motionArgs;
8674 processPosition(mapper, 100, 100);
8675 processSync(mapper);
8676
8677 mFakeListener->assertNotifyMotionWasNotCalled();
8678}
8679
Garfield Tanc734e4f2021-01-15 20:01:39 -08008680TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8681 addConfigurationProperty("touch.deviceType", "touchScreen");
8682 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8683 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8684 ViewportType::INTERNAL);
8685 std::optional<DisplayViewport> optionalDisplayViewport =
8686 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8687 ASSERT_TRUE(optionalDisplayViewport.has_value());
8688 DisplayViewport displayViewport = *optionalDisplayViewport;
8689
8690 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8691 prepareAxes(POSITION);
8692 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8693
8694 // Finger down
8695 int32_t x = 100, y = 100;
8696 processPosition(mapper, x, y);
8697 processSync(mapper);
8698
8699 NotifyMotionArgs motionArgs;
8700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8701 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8702
8703 // Deactivate display viewport
8704 displayViewport.isActive = false;
8705 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8706 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8707
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008708 // The ongoing touch should be canceled immediately
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8710 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8711
8712 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008713 x += 10, y += 10;
8714 processPosition(mapper, x, y);
8715 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008717
8718 // Reactivate display viewport
8719 displayViewport.isActive = true;
8720 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8721 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8722
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008723 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008724 x += 10, y += 10;
8725 processPosition(mapper, x, y);
8726 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8728 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008729}
8730
Arthur Hung7c645402019-01-25 17:45:42 +08008731TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8732 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008733 prepareAxes(POSITION | ID | SLOT);
8734 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008735 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008736
8737 // Create the second touch screen device, and enable multi fingers.
8738 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008739 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008740 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008741 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008742 std::shared_ptr<InputDevice> device2 =
8743 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008744 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008745
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008746 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8747 0 /*flat*/, 0 /*fuzz*/);
8748 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8749 0 /*flat*/, 0 /*fuzz*/);
8750 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8751 0 /*flat*/, 0 /*fuzz*/);
8752 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8753 0 /*flat*/, 0 /*fuzz*/);
8754 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8755 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8756 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008757
8758 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008759 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008760 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8761 device2->reset(ARBITRARY_TIME);
8762
8763 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008764 std::shared_ptr<FakePointerController> fakePointerController =
8765 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008766 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008767
8768 // Setup policy for associated displays and show touches.
8769 const uint8_t hdmi1 = 0;
8770 const uint8_t hdmi2 = 1;
8771 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8772 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8773 mFakePolicy->setShowTouches(true);
8774
8775 // Create displays.
8776 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008777 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008778
8779 // Default device will reconfigure above, need additional reconfiguration for another device.
8780 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008781 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8782 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008783
8784 // Two fingers down at default display.
8785 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8786 processPosition(mapper, x1, y1);
8787 processId(mapper, 1);
8788 processSlot(mapper, 1);
8789 processPosition(mapper, x2, y2);
8790 processId(mapper, 2);
8791 processSync(mapper);
8792
8793 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8794 fakePointerController->getSpots().find(DISPLAY_ID);
8795 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8796 ASSERT_EQ(size_t(2), iter->second.size());
8797
8798 // Two fingers down at second display.
8799 processPosition(mapper2, x1, y1);
8800 processId(mapper2, 1);
8801 processSlot(mapper2, 1);
8802 processPosition(mapper2, x2, y2);
8803 processId(mapper2, 2);
8804 processSync(mapper2);
8805
8806 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8807 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8808 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008809
8810 // Disable the show touches configuration and ensure the spots are cleared.
8811 mFakePolicy->setShowTouches(false);
8812 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8813 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8814
8815 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008816}
8817
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008818TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008819 prepareAxes(POSITION);
8820 addConfigurationProperty("touch.deviceType", "touchScreen");
8821 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008822 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008823
8824 NotifyMotionArgs motionArgs;
8825 // Unrotated video frame
8826 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8827 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008828 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008829 processPosition(mapper, 100, 200);
8830 processSync(mapper);
8831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8832 ASSERT_EQ(frames, motionArgs.videoFrames);
8833
8834 // Subsequent touch events should not have any videoframes
8835 // This is implemented separately in FakeEventHub,
8836 // but that should match the behaviour of TouchVideoDevice.
8837 processPosition(mapper, 200, 200);
8838 processSync(mapper);
8839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8840 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8841}
8842
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008843TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008844 prepareAxes(POSITION);
8845 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008846 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008847 // Unrotated video frame
8848 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8849 NotifyMotionArgs motionArgs;
8850
8851 // Test all 4 orientations
8852 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008853 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8854 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8855 clearViewports();
8856 prepareDisplay(orientation);
8857 std::vector<TouchVideoFrame> frames{frame};
8858 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8859 processPosition(mapper, 100, 200);
8860 processSync(mapper);
8861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8862 ASSERT_EQ(frames, motionArgs.videoFrames);
8863 }
8864}
8865
8866TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8867 prepareAxes(POSITION);
8868 addConfigurationProperty("touch.deviceType", "touchScreen");
8869 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8870 // orientation-aware are affected by display rotation.
8871 addConfigurationProperty("touch.orientationAware", "0");
8872 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8873 // Unrotated video frame
8874 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8875 NotifyMotionArgs motionArgs;
8876
8877 // Test all 4 orientations
8878 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008879 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8880 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8881 clearViewports();
8882 prepareDisplay(orientation);
8883 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008884 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008885 processPosition(mapper, 100, 200);
8886 processSync(mapper);
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008888 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8889 // compared to the display. This is so that when the window transform (which contains the
8890 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8891 // window's coordinate space.
8892 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008893 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008894
8895 // Release finger.
8896 processSync(mapper);
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008898 }
8899}
8900
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008901TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008902 prepareAxes(POSITION);
8903 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008904 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008905 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8906 // so mix these.
8907 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8908 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8909 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8910 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8911 NotifyMotionArgs motionArgs;
8912
8913 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008914 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008915 processPosition(mapper, 100, 200);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008918 ASSERT_EQ(frames, motionArgs.videoFrames);
8919}
8920
8921TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8922 prepareAxes(POSITION);
8923 addConfigurationProperty("touch.deviceType", "touchScreen");
8924 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8925 // orientation-aware are affected by display rotation.
8926 addConfigurationProperty("touch.orientationAware", "0");
8927 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8928 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8929 // so mix these.
8930 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8931 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8932 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8933 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8934 NotifyMotionArgs motionArgs;
8935
8936 prepareDisplay(DISPLAY_ORIENTATION_90);
8937 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8938 processPosition(mapper, 100, 200);
8939 processSync(mapper);
8940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8941 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8942 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8943 // compared to the display. This is so that when the window transform (which contains the
8944 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8945 // window's coordinate space.
8946 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8947 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008948 ASSERT_EQ(frames, motionArgs.videoFrames);
8949}
8950
Arthur Hung9da14732019-09-02 16:16:58 +08008951/**
8952 * If we had defined port associations, but the viewport is not ready, the touch device would be
8953 * expected to be disabled, and it should be enabled after the viewport has found.
8954 */
8955TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008956 constexpr uint8_t hdmi2 = 1;
8957 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008958 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008959
8960 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8961
8962 addConfigurationProperty("touch.deviceType", "touchScreen");
8963 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008964 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008965
8966 ASSERT_EQ(mDevice->isEnabled(), false);
8967
8968 // Add display on hdmi2, the device should be enabled and can receive touch event.
8969 prepareSecondaryDisplay(type, hdmi2);
8970 ASSERT_EQ(mDevice->isEnabled(), true);
8971
8972 // Send a touch event.
8973 processPosition(mapper, 100, 100);
8974 processSync(mapper);
8975
8976 NotifyMotionArgs args;
8977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8978 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8979}
8980
Arthur Hung421eb1c2020-01-16 00:09:42 +08008981TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008982 addConfigurationProperty("touch.deviceType", "touchScreen");
8983 prepareDisplay(DISPLAY_ORIENTATION_0);
8984 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008985 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008986
8987 NotifyMotionArgs motionArgs;
8988
8989 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8990 // finger down
8991 processId(mapper, 1);
8992 processPosition(mapper, x1, y1);
8993 processSync(mapper);
8994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8995 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8997
8998 // finger move
8999 processId(mapper, 1);
9000 processPosition(mapper, x2, y2);
9001 processSync(mapper);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9005
9006 // finger up.
9007 processId(mapper, -1);
9008 processSync(mapper);
9009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9010 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9012
9013 // new finger down
9014 processId(mapper, 1);
9015 processPosition(mapper, x3, y3);
9016 processSync(mapper);
9017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9018 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9020}
9021
9022/**
arthurhungcc7f9802020-04-30 17:55:40 +08009023 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9024 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009025 */
arthurhungcc7f9802020-04-30 17:55:40 +08009026TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009027 addConfigurationProperty("touch.deviceType", "touchScreen");
9028 prepareDisplay(DISPLAY_ORIENTATION_0);
9029 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009030 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009031
9032 NotifyMotionArgs motionArgs;
9033
9034 // default tool type is finger
9035 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009036 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009037 processPosition(mapper, x1, y1);
9038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9040 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9042
9043 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9044 processToolType(mapper, MT_TOOL_PALM);
9045 processSync(mapper);
9046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9047 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9048
9049 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009050 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009051 processPosition(mapper, x2, y2);
9052 processSync(mapper);
9053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9054
9055 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009056 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9059
9060 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009061 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009062 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009063 processPosition(mapper, x3, y3);
9064 processSync(mapper);
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9066 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9068}
9069
arthurhungbf89a482020-04-17 17:37:55 +08009070/**
arthurhungcc7f9802020-04-30 17:55:40 +08009071 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9072 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009073 */
arthurhungcc7f9802020-04-30 17:55:40 +08009074TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009075 addConfigurationProperty("touch.deviceType", "touchScreen");
9076 prepareDisplay(DISPLAY_ORIENTATION_0);
9077 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9078 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9079
9080 NotifyMotionArgs motionArgs;
9081
9082 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009083 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9084 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009085 processPosition(mapper, x1, y1);
9086 processSync(mapper);
9087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9088 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9090
9091 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009092 processSlot(mapper, SECOND_SLOT);
9093 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009094 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009095 processSync(mapper);
9096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009097 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9099
9100 // If the tool type of the first finger changes to MT_TOOL_PALM,
9101 // we expect to receive ACTION_POINTER_UP with cancel flag.
9102 processSlot(mapper, FIRST_SLOT);
9103 processId(mapper, FIRST_TRACKING_ID);
9104 processToolType(mapper, MT_TOOL_PALM);
9105 processSync(mapper);
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009107 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009108 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9109
9110 // The following MOVE events of second finger should be processed.
9111 processSlot(mapper, SECOND_SLOT);
9112 processId(mapper, SECOND_TRACKING_ID);
9113 processPosition(mapper, x2 + 1, y2 + 1);
9114 processSync(mapper);
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9117 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9118
9119 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9120 // it. Second finger receive move.
9121 processSlot(mapper, FIRST_SLOT);
9122 processId(mapper, INVALID_TRACKING_ID);
9123 processSync(mapper);
9124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9126 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9127
9128 // Second finger keeps moving.
9129 processSlot(mapper, SECOND_SLOT);
9130 processId(mapper, SECOND_TRACKING_ID);
9131 processPosition(mapper, x2 + 2, y2 + 2);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9135 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9136
9137 // Second finger up.
9138 processId(mapper, INVALID_TRACKING_ID);
9139 processSync(mapper);
9140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9141 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9142 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9143}
9144
9145/**
9146 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9147 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9148 */
9149TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9150 addConfigurationProperty("touch.deviceType", "touchScreen");
9151 prepareDisplay(DISPLAY_ORIENTATION_0);
9152 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9153 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9154
9155 NotifyMotionArgs motionArgs;
9156
9157 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9158 // First finger down.
9159 processId(mapper, FIRST_TRACKING_ID);
9160 processPosition(mapper, x1, y1);
9161 processSync(mapper);
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9163 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9165
9166 // Second finger down.
9167 processSlot(mapper, SECOND_SLOT);
9168 processId(mapper, SECOND_TRACKING_ID);
9169 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009170 processSync(mapper);
9171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009172 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9174
arthurhungcc7f9802020-04-30 17:55:40 +08009175 // If the tool type of the first finger changes to MT_TOOL_PALM,
9176 // we expect to receive ACTION_POINTER_UP with cancel flag.
9177 processSlot(mapper, FIRST_SLOT);
9178 processId(mapper, FIRST_TRACKING_ID);
9179 processToolType(mapper, MT_TOOL_PALM);
9180 processSync(mapper);
9181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009182 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009183 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9184
9185 // Second finger keeps moving.
9186 processSlot(mapper, SECOND_SLOT);
9187 processId(mapper, SECOND_TRACKING_ID);
9188 processPosition(mapper, x2 + 1, y2 + 1);
9189 processSync(mapper);
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9192
9193 // second finger becomes palm, receive cancel due to only 1 finger is active.
9194 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009195 processToolType(mapper, MT_TOOL_PALM);
9196 processSync(mapper);
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9198 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9199
arthurhungcc7f9802020-04-30 17:55:40 +08009200 // third finger down.
9201 processSlot(mapper, THIRD_SLOT);
9202 processId(mapper, THIRD_TRACKING_ID);
9203 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009204 processPosition(mapper, x3, y3);
9205 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009209 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9210
9211 // third finger move
9212 processId(mapper, THIRD_TRACKING_ID);
9213 processPosition(mapper, x3 + 1, y3 + 1);
9214 processSync(mapper);
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9217
9218 // first finger up, third finger receive move.
9219 processSlot(mapper, FIRST_SLOT);
9220 processId(mapper, INVALID_TRACKING_ID);
9221 processSync(mapper);
9222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9224 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9225
9226 // second finger up, third finger receive move.
9227 processSlot(mapper, SECOND_SLOT);
9228 processId(mapper, INVALID_TRACKING_ID);
9229 processSync(mapper);
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9232 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9233
9234 // third finger up.
9235 processSlot(mapper, THIRD_SLOT);
9236 processId(mapper, INVALID_TRACKING_ID);
9237 processSync(mapper);
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9239 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9240 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9241}
9242
9243/**
9244 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9245 * and the active finger could still be allowed to receive the events
9246 */
9247TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9248 addConfigurationProperty("touch.deviceType", "touchScreen");
9249 prepareDisplay(DISPLAY_ORIENTATION_0);
9250 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9251 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9252
9253 NotifyMotionArgs motionArgs;
9254
9255 // default tool type is finger
9256 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9257 processId(mapper, FIRST_TRACKING_ID);
9258 processPosition(mapper, x1, y1);
9259 processSync(mapper);
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9263
9264 // Second finger down.
9265 processSlot(mapper, SECOND_SLOT);
9266 processId(mapper, SECOND_TRACKING_ID);
9267 processPosition(mapper, x2, y2);
9268 processSync(mapper);
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009270 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9272
9273 // If the tool type of the second finger changes to MT_TOOL_PALM,
9274 // we expect to receive ACTION_POINTER_UP with cancel flag.
9275 processId(mapper, SECOND_TRACKING_ID);
9276 processToolType(mapper, MT_TOOL_PALM);
9277 processSync(mapper);
9278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009279 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009280 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9281
9282 // The following MOVE event should be processed.
9283 processSlot(mapper, FIRST_SLOT);
9284 processId(mapper, FIRST_TRACKING_ID);
9285 processPosition(mapper, x1 + 1, y1 + 1);
9286 processSync(mapper);
9287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9289 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9290
9291 // second finger up.
9292 processSlot(mapper, SECOND_SLOT);
9293 processId(mapper, INVALID_TRACKING_ID);
9294 processSync(mapper);
9295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9297
9298 // first finger keep moving
9299 processSlot(mapper, FIRST_SLOT);
9300 processId(mapper, FIRST_TRACKING_ID);
9301 processPosition(mapper, x1 + 2, y1 + 2);
9302 processSync(mapper);
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9305
9306 // first finger up.
9307 processId(mapper, INVALID_TRACKING_ID);
9308 processSync(mapper);
9309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9310 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9311 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009312}
9313
Arthur Hung9ad18942021-06-19 02:04:46 +00009314/**
9315 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9316 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9317 * cause slot be valid again.
9318 */
9319TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9320 addConfigurationProperty("touch.deviceType", "touchScreen");
9321 prepareDisplay(DISPLAY_ORIENTATION_0);
9322 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9323 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9324
9325 NotifyMotionArgs motionArgs;
9326
9327 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9328 // First finger down.
9329 processId(mapper, FIRST_TRACKING_ID);
9330 processPosition(mapper, x1, y1);
9331 processPressure(mapper, RAW_PRESSURE_MAX);
9332 processSync(mapper);
9333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9334 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9335 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9336
9337 // First finger move.
9338 processId(mapper, FIRST_TRACKING_ID);
9339 processPosition(mapper, x1 + 1, y1 + 1);
9340 processPressure(mapper, RAW_PRESSURE_MAX);
9341 processSync(mapper);
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9344 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9345
9346 // Second finger down.
9347 processSlot(mapper, SECOND_SLOT);
9348 processId(mapper, SECOND_TRACKING_ID);
9349 processPosition(mapper, x2, y2);
9350 processPressure(mapper, RAW_PRESSURE_MAX);
9351 processSync(mapper);
9352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009353 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009354 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9355
9356 // second finger up with some unexpected data.
9357 processSlot(mapper, SECOND_SLOT);
9358 processId(mapper, INVALID_TRACKING_ID);
9359 processPosition(mapper, x2, y2);
9360 processSync(mapper);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009362 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009363 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9364
9365 // first finger up with some unexpected data.
9366 processSlot(mapper, FIRST_SLOT);
9367 processId(mapper, INVALID_TRACKING_ID);
9368 processPosition(mapper, x2, y2);
9369 processPressure(mapper, RAW_PRESSURE_MAX);
9370 processSync(mapper);
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9372 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9373 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9374}
9375
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009376// --- MultiTouchInputMapperTest_ExternalDevice ---
9377
9378class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9379protected:
Chris Yea52ade12020-08-27 16:49:20 -07009380 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009381};
9382
9383/**
9384 * Expect fallback to internal viewport if device is external and external viewport is not present.
9385 */
9386TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9387 prepareAxes(POSITION);
9388 addConfigurationProperty("touch.deviceType", "touchScreen");
9389 prepareDisplay(DISPLAY_ORIENTATION_0);
9390 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9391
9392 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9393
9394 NotifyMotionArgs motionArgs;
9395
9396 // Expect the event to be sent to the internal viewport,
9397 // because an external viewport is not present.
9398 processPosition(mapper, 100, 100);
9399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9402
9403 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009404 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009405 processPosition(mapper, 100, 100);
9406 processSync(mapper);
9407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9408 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9409}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009410
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009411TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9412 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9413 std::shared_ptr<FakePointerController> fakePointerController =
9414 std::make_shared<FakePointerController>();
9415 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9416 fakePointerController->setPosition(0, 0);
9417 fakePointerController->setButtonState(0);
9418
9419 // prepare device and capture
9420 prepareDisplay(DISPLAY_ORIENTATION_0);
9421 prepareAxes(POSITION | ID | SLOT);
9422 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9423 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9424 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009425 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009426 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9427
9428 // captured touchpad should be a touchpad source
9429 NotifyDeviceResetArgs resetArgs;
9430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9431 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9432
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009433 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009434
9435 const InputDeviceInfo::MotionRange* relRangeX =
9436 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9437 ASSERT_NE(relRangeX, nullptr);
9438 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9439 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9440 const InputDeviceInfo::MotionRange* relRangeY =
9441 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9442 ASSERT_NE(relRangeY, nullptr);
9443 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9444 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9445
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009446 // run captured pointer tests - note that this is unscaled, so input listener events should be
9447 // identical to what the hardware sends (accounting for any
9448 // calibration).
9449 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009450 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009451 processId(mapper, 1);
9452 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9453 processKey(mapper, BTN_TOUCH, 1);
9454 processSync(mapper);
9455
9456 // expect coord[0] to contain initial location of touch 0
9457 NotifyMotionArgs args;
9458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9459 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9460 ASSERT_EQ(1U, args.pointerCount);
9461 ASSERT_EQ(0, args.pointerProperties[0].id);
9462 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9463 ASSERT_NO_FATAL_FAILURE(
9464 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9465
9466 // FINGER 1 DOWN
9467 processSlot(mapper, 1);
9468 processId(mapper, 2);
9469 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9470 processSync(mapper);
9471
9472 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009474 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009475 ASSERT_EQ(2U, args.pointerCount);
9476 ASSERT_EQ(0, args.pointerProperties[0].id);
9477 ASSERT_EQ(1, args.pointerProperties[1].id);
9478 ASSERT_NO_FATAL_FAILURE(
9479 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9480 ASSERT_NO_FATAL_FAILURE(
9481 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9482
9483 // FINGER 1 MOVE
9484 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9485 processSync(mapper);
9486
9487 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9488 // from move
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9491 ASSERT_NO_FATAL_FAILURE(
9492 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9493 ASSERT_NO_FATAL_FAILURE(
9494 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9495
9496 // FINGER 0 MOVE
9497 processSlot(mapper, 0);
9498 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9499 processSync(mapper);
9500
9501 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9504 ASSERT_NO_FATAL_FAILURE(
9505 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9506 ASSERT_NO_FATAL_FAILURE(
9507 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9508
9509 // BUTTON DOWN
9510 processKey(mapper, BTN_LEFT, 1);
9511 processSync(mapper);
9512
9513 // touchinputmapper design sends a move before button press
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9518
9519 // BUTTON UP
9520 processKey(mapper, BTN_LEFT, 0);
9521 processSync(mapper);
9522
9523 // touchinputmapper design sends a move after button release
9524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9525 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9528
9529 // FINGER 0 UP
9530 processId(mapper, -1);
9531 processSync(mapper);
9532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9533 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9534
9535 // FINGER 1 MOVE
9536 processSlot(mapper, 1);
9537 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9538 processSync(mapper);
9539
9540 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9543 ASSERT_EQ(1U, args.pointerCount);
9544 ASSERT_EQ(1, args.pointerProperties[0].id);
9545 ASSERT_NO_FATAL_FAILURE(
9546 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9547
9548 // FINGER 1 UP
9549 processId(mapper, -1);
9550 processKey(mapper, BTN_TOUCH, 0);
9551 processSync(mapper);
9552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9553 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9554
9555 // non captured touchpad should be a mouse source
9556 mFakePolicy->setPointerCapture(false);
9557 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9559 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9560}
9561
9562TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9563 std::shared_ptr<FakePointerController> fakePointerController =
9564 std::make_shared<FakePointerController>();
9565 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9566 fakePointerController->setPosition(0, 0);
9567 fakePointerController->setButtonState(0);
9568
9569 // prepare device and capture
9570 prepareDisplay(DISPLAY_ORIENTATION_0);
9571 prepareAxes(POSITION | ID | SLOT);
9572 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9573 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009574 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009575 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9576 // run uncaptured pointer tests - pushes out generic events
9577 // FINGER 0 DOWN
9578 processId(mapper, 3);
9579 processPosition(mapper, 100, 100);
9580 processKey(mapper, BTN_TOUCH, 1);
9581 processSync(mapper);
9582
9583 // start at (100,100), cursor should be at (0,0) * scale
9584 NotifyMotionArgs args;
9585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9587 ASSERT_NO_FATAL_FAILURE(
9588 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9589
9590 // FINGER 0 MOVE
9591 processPosition(mapper, 200, 200);
9592 processSync(mapper);
9593
9594 // compute scaling to help with touch position checking
9595 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9596 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9597 float scale =
9598 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9599
9600 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9604 0, 0, 0, 0, 0, 0, 0));
9605}
9606
9607TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9608 std::shared_ptr<FakePointerController> fakePointerController =
9609 std::make_shared<FakePointerController>();
9610
9611 prepareDisplay(DISPLAY_ORIENTATION_0);
9612 prepareAxes(POSITION | ID | SLOT);
9613 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009614 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009615 mFakePolicy->setPointerCapture(false);
9616 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9617
9618 // uncaptured touchpad should be a pointer device
9619 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9620
9621 // captured touchpad should be a touchpad device
9622 mFakePolicy->setPointerCapture(true);
9623 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9624 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9625}
9626
HQ Liue6983c72022-04-19 22:14:56 +00009627class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9628protected:
9629 float mPointerMovementScale;
9630 float mPointerXZoomScale;
9631 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9632 addConfigurationProperty("touch.deviceType", "pointer");
9633 std::shared_ptr<FakePointerController> fakePointerController =
9634 std::make_shared<FakePointerController>();
9635 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9636 fakePointerController->setPosition(0, 0);
9637 fakePointerController->setButtonState(0);
9638 prepareDisplay(DISPLAY_ORIENTATION_0);
9639
9640 prepareAxes(POSITION);
9641 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9642 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9643 // needs to be disabled, and the pointer gesture needs to be enabled.
9644 mFakePolicy->setPointerCapture(false);
9645 mFakePolicy->setPointerGestureEnabled(true);
9646 mFakePolicy->setPointerController(fakePointerController);
9647
9648 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9649 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9650 mPointerMovementScale =
9651 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9652 mPointerXZoomScale =
9653 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9654 }
9655
9656 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9657 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9658 /*flat*/ 0,
9659 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9660 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9661 /*flat*/ 0,
9662 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9663 }
9664};
9665
9666/**
9667 * Two fingers down on a pointer mode touch pad. The width
9668 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9669 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9670 * be greater than the both value to be freeform gesture, so that after two
9671 * fingers start to move downwards, the gesture should be swipe.
9672 */
9673TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9674 // The min freeform gesture width is 25units/mm x 30mm = 750
9675 // which is greater than fraction of the diagnal length of the touchpad (349).
9676 // Thus, MaxSwipWidth is 750.
9677 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9678 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9679 NotifyMotionArgs motionArgs;
9680
9681 // Two fingers down at once.
9682 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9683 // Pointer's initial position is used the [0,0] coordinate.
9684 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9685
9686 processId(mapper, FIRST_TRACKING_ID);
9687 processPosition(mapper, x1, y1);
9688 processMTSync(mapper);
9689 processId(mapper, SECOND_TRACKING_ID);
9690 processPosition(mapper, x2, y2);
9691 processMTSync(mapper);
9692 processSync(mapper);
9693
9694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9695 ASSERT_EQ(1U, motionArgs.pointerCount);
9696 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9698 ASSERT_NO_FATAL_FAILURE(
9699 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9700
9701 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9702 // that there should be 1 pointer.
9703 int32_t movingDistance = 200;
9704 y1 += movingDistance;
9705 y2 += movingDistance;
9706
9707 processId(mapper, FIRST_TRACKING_ID);
9708 processPosition(mapper, x1, y1);
9709 processMTSync(mapper);
9710 processId(mapper, SECOND_TRACKING_ID);
9711 processPosition(mapper, x2, y2);
9712 processMTSync(mapper);
9713 processSync(mapper);
9714
9715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9716 ASSERT_EQ(1U, motionArgs.pointerCount);
9717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9720 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9721 0, 0, 0, 0));
9722}
9723
9724/**
9725 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9726 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9727 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9728 * value to be freeform gesture, so that after two fingers start to move downwards,
9729 * the gesture should be swipe.
9730 */
9731TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9732 // The min freeform gesture width is 5units/mm x 30mm = 150
9733 // which is greater than fraction of the diagnal length of the touchpad (349).
9734 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9735 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9736 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9737 NotifyMotionArgs motionArgs;
9738
9739 // Two fingers down at once.
9740 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9741 // Pointer's initial position is used the [0,0] coordinate.
9742 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9743
9744 processId(mapper, FIRST_TRACKING_ID);
9745 processPosition(mapper, x1, y1);
9746 processMTSync(mapper);
9747 processId(mapper, SECOND_TRACKING_ID);
9748 processPosition(mapper, x2, y2);
9749 processMTSync(mapper);
9750 processSync(mapper);
9751
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9753 ASSERT_EQ(1U, motionArgs.pointerCount);
9754 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9756 ASSERT_NO_FATAL_FAILURE(
9757 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9758
9759 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9760 // and there should be 1 pointer.
9761 int32_t movingDistance = 200;
9762 y1 += movingDistance;
9763 y2 += movingDistance;
9764
9765 processId(mapper, FIRST_TRACKING_ID);
9766 processPosition(mapper, x1, y1);
9767 processMTSync(mapper);
9768 processId(mapper, SECOND_TRACKING_ID);
9769 processPosition(mapper, x2, y2);
9770 processMTSync(mapper);
9771 processSync(mapper);
9772
9773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9774 ASSERT_EQ(1U, motionArgs.pointerCount);
9775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9777 // New coordinate is the scaled relative coordinate from the initial coordinate.
9778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9779 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9780 0, 0, 0, 0));
9781}
9782
9783/**
9784 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9785 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9786 * freeform gestures after two fingers start to move downwards.
9787 */
9788TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9789 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9790 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9791
9792 NotifyMotionArgs motionArgs;
9793
9794 // Two fingers down at once. Wider than the max swipe width.
9795 // The gesture is expected to be PRESS, then transformed to FREEFORM
9796 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9797
9798 processId(mapper, FIRST_TRACKING_ID);
9799 processPosition(mapper, x1, y1);
9800 processMTSync(mapper);
9801 processId(mapper, SECOND_TRACKING_ID);
9802 processPosition(mapper, x2, y2);
9803 processMTSync(mapper);
9804 processSync(mapper);
9805
9806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9807 ASSERT_EQ(1U, motionArgs.pointerCount);
9808 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9810 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9811 ASSERT_NO_FATAL_FAILURE(
9812 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9813
9814 int32_t movingDistance = 200;
9815
9816 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9817 // then two down events for two pointers.
9818 y1 += movingDistance;
9819 y2 += movingDistance;
9820
9821 processId(mapper, FIRST_TRACKING_ID);
9822 processPosition(mapper, x1, y1);
9823 processMTSync(mapper);
9824 processId(mapper, SECOND_TRACKING_ID);
9825 processPosition(mapper, x2, y2);
9826 processMTSync(mapper);
9827 processSync(mapper);
9828
9829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9830 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9831 ASSERT_EQ(1U, motionArgs.pointerCount);
9832 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9835 ASSERT_EQ(1U, motionArgs.pointerCount);
9836 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9839 ASSERT_EQ(2U, motionArgs.pointerCount);
9840 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9842 // Two pointers' scaled relative coordinates from their initial centroid.
9843 // Initial y coordinates are 0 as y1 and y2 have the same value.
9844 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9845 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9846 // When pointers move, the new coordinates equal to the initial coordinates plus
9847 // scaled moving distance.
9848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9849 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9850 0, 0, 0, 0));
9851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9852 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9853 0, 0, 0, 0));
9854
9855 // Move two fingers down again, expect one MOVE motion event.
9856 y1 += movingDistance;
9857 y2 += movingDistance;
9858
9859 processId(mapper, FIRST_TRACKING_ID);
9860 processPosition(mapper, x1, y1);
9861 processMTSync(mapper);
9862 processId(mapper, SECOND_TRACKING_ID);
9863 processPosition(mapper, x2, y2);
9864 processMTSync(mapper);
9865 processSync(mapper);
9866
9867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9868 ASSERT_EQ(2U, motionArgs.pointerCount);
9869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9870 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9872 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9873 0, 0, 0, 0, 0));
9874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9875 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9876 0, 0, 0, 0, 0));
9877}
9878
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009879// --- JoystickInputMapperTest ---
9880
9881class JoystickInputMapperTest : public InputMapperTest {
9882protected:
9883 static const int32_t RAW_X_MIN;
9884 static const int32_t RAW_X_MAX;
9885 static const int32_t RAW_Y_MIN;
9886 static const int32_t RAW_Y_MAX;
9887
9888 void SetUp() override {
9889 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9890 }
9891 void prepareAxes() {
9892 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9893 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9894 }
9895
9896 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9898 }
9899
9900 void processSync(JoystickInputMapper& mapper) {
9901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9902 }
9903
9904 void prepareVirtualDisplay(int32_t orientation) {
9905 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9906 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9907 NO_PORT, ViewportType::VIRTUAL);
9908 }
9909};
9910
9911const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9912const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9913const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9914const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9915
9916TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9917 prepareAxes();
9918 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9919
9920 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9921
9922 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9923
9924 // Send an axis event
9925 processAxis(mapper, ABS_X, 100);
9926 processSync(mapper);
9927
9928 NotifyMotionArgs args;
9929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9930 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9931
9932 // Send another axis event
9933 processAxis(mapper, ABS_Y, 100);
9934 processSync(mapper);
9935
9936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9937 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9938}
9939
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009940// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009941
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009942class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009943protected:
9944 static const char* DEVICE_NAME;
9945 static const char* DEVICE_LOCATION;
9946 static const int32_t DEVICE_ID;
9947 static const int32_t DEVICE_GENERATION;
9948 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009949 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009950 static const int32_t EVENTHUB_ID;
9951
9952 std::shared_ptr<FakeEventHub> mFakeEventHub;
9953 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009954 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009955 std::unique_ptr<InstrumentedInputReader> mReader;
9956 std::shared_ptr<InputDevice> mDevice;
9957
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009958 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009959 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009960 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009961 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009962 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009963 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009964 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9965 }
9966
9967 void SetUp() override { SetUp(DEVICE_CLASSES); }
9968
9969 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009970 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009971 mFakePolicy.clear();
9972 }
9973
9974 void configureDevice(uint32_t changes) {
9975 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9976 mReader->requestRefreshConfiguration(changes);
9977 mReader->loopOnce();
9978 }
9979 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9980 }
9981
9982 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9983 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009984 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009985 InputDeviceIdentifier identifier;
9986 identifier.name = name;
9987 identifier.location = location;
9988 std::shared_ptr<InputDevice> device =
9989 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9990 identifier);
9991 mReader->pushNextDevice(device);
9992 mFakeEventHub->addDevice(eventHubId, name, classes);
9993 mReader->loopOnce();
9994 return device;
9995 }
9996
9997 template <class T, typename... Args>
9998 T& addControllerAndConfigure(Args... args) {
9999 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10000
10001 return controller;
10002 }
10003};
10004
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010005const char* PeripheralControllerTest::DEVICE_NAME = "device";
10006const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10007const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10008const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10009const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010010const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10011 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010012const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010013
10014// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010015class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010016protected:
10017 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010018 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010019 }
10020};
10021
10022TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010023 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010024
10025 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10026 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10027}
10028
10029TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010030 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010031
10032 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10033 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10034}
10035
10036// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010037class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010038protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010039 void SetUp() override {
10040 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10041 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010042};
10043
Chris Ye85758332021-05-16 23:05:17 -070010044TEST_F(LightControllerTest, MonoLight) {
10045 RawLightInfo infoMono = {.id = 1,
10046 .name = "Mono",
10047 .maxBrightness = 255,
10048 .flags = InputLightClass::BRIGHTNESS,
10049 .path = ""};
10050 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010051
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010052 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010053 InputDeviceInfo info;
10054 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010055 std::vector<InputDeviceLightInfo> lights = info.getLights();
10056 ASSERT_EQ(1U, lights.size());
10057 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010058
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010059 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10060 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010061}
10062
10063TEST_F(LightControllerTest, RGBLight) {
10064 RawLightInfo infoRed = {.id = 1,
10065 .name = "red",
10066 .maxBrightness = 255,
10067 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10068 .path = ""};
10069 RawLightInfo infoGreen = {.id = 2,
10070 .name = "green",
10071 .maxBrightness = 255,
10072 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10073 .path = ""};
10074 RawLightInfo infoBlue = {.id = 3,
10075 .name = "blue",
10076 .maxBrightness = 255,
10077 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10078 .path = ""};
10079 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10080 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10081 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10082
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010083 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010084 InputDeviceInfo info;
10085 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010086 std::vector<InputDeviceLightInfo> lights = info.getLights();
10087 ASSERT_EQ(1U, lights.size());
10088 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010089
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010090 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10091 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010092}
10093
10094TEST_F(LightControllerTest, MultiColorRGBLight) {
10095 RawLightInfo infoColor = {.id = 1,
10096 .name = "red",
10097 .maxBrightness = 255,
10098 .flags = InputLightClass::BRIGHTNESS |
10099 InputLightClass::MULTI_INTENSITY |
10100 InputLightClass::MULTI_INDEX,
10101 .path = ""};
10102
10103 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10104
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010105 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010106 InputDeviceInfo info;
10107 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010108 std::vector<InputDeviceLightInfo> lights = info.getLights();
10109 ASSERT_EQ(1U, lights.size());
10110 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010111
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010112 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10113 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010114}
10115
10116TEST_F(LightControllerTest, PlayerIdLight) {
10117 RawLightInfo info1 = {.id = 1,
10118 .name = "player1",
10119 .maxBrightness = 255,
10120 .flags = InputLightClass::BRIGHTNESS,
10121 .path = ""};
10122 RawLightInfo info2 = {.id = 2,
10123 .name = "player2",
10124 .maxBrightness = 255,
10125 .flags = InputLightClass::BRIGHTNESS,
10126 .path = ""};
10127 RawLightInfo info3 = {.id = 3,
10128 .name = "player3",
10129 .maxBrightness = 255,
10130 .flags = InputLightClass::BRIGHTNESS,
10131 .path = ""};
10132 RawLightInfo info4 = {.id = 4,
10133 .name = "player4",
10134 .maxBrightness = 255,
10135 .flags = InputLightClass::BRIGHTNESS,
10136 .path = ""};
10137 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10138 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10139 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10140 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10141
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010142 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010143 InputDeviceInfo info;
10144 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010145 std::vector<InputDeviceLightInfo> lights = info.getLights();
10146 ASSERT_EQ(1U, lights.size());
10147 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010148
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010149 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10150 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10151 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010152}
10153
Michael Wrightd02c5b62014-02-10 15:10:22 -080010154} // namespace android