blob: b36b49812dea98294c96de426ba687287b0324e2 [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>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050040#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000042#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000043#include "input/DisplayViewport.h"
44#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010045
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000046using android::hardware::input::InputDeviceCountryCode;
47
Michael Wrightd02c5b62014-02-10 15:10:22 -080048namespace android {
49
Dominik Laskowski2f01d772022-03-23 16:01:29 -070050using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000051using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070052using std::chrono_literals::operator""ms;
53
54// Timeout for waiting for an expected event
55static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
56
Michael Wrightd02c5b62014-02-10 15:10:22 -080057// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000058static constexpr nsecs_t ARBITRARY_TIME = 1234;
59static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080060
61// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080081static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080082static constexpr int32_t BATTERY_STATUS = 4;
83static constexpr int32_t BATTERY_CAPACITY = 66;
Prabir Pradhane287ecd2022-09-07 21:18:05 +000084static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
Chris Ye3fdbfef2021-01-06 18:45:18 -080085static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
86static constexpr int32_t LIGHT_COLOR = 0x7F448866;
87static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080088
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080089static constexpr int32_t ACTION_POINTER_0_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_0_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93static constexpr int32_t ACTION_POINTER_1_DOWN =
94 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
95static constexpr int32_t ACTION_POINTER_1_UP =
96 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
97
Michael Wrightd02c5b62014-02-10 15:10:22 -080098// Error tolerance for floating point assertions.
99static const float EPSILON = 0.001f;
100
101template<typename T>
102static inline T min(T a, T b) {
103 return a < b ? a : b;
104}
105
106static inline float avg(float x, float y) {
107 return (x + y) / 2;
108}
109
Chris Ye3fdbfef2021-01-06 18:45:18 -0800110// Mapping for light color name and the light color
111const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
112 {"green", LightColor::GREEN},
113 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700115static int32_t getInverseRotation(int32_t orientation) {
116 switch (orientation) {
117 case DISPLAY_ORIENTATION_90:
118 return DISPLAY_ORIENTATION_270;
119 case DISPLAY_ORIENTATION_270:
120 return DISPLAY_ORIENTATION_90;
121 default:
122 return orientation;
123 }
124}
125
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800126static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
127 InputDeviceInfo info;
128 mapper.populateDeviceInfo(&info);
129
130 const InputDeviceInfo::MotionRange* motionRange =
131 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
132 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
133}
134
135static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
136 InputDeviceInfo info;
137 mapper.populateDeviceInfo(&info);
138
139 const InputDeviceInfo::MotionRange* motionRange =
140 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
141 ASSERT_EQ(nullptr, motionRange);
142}
143
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144// --- FakePointerController ---
145
146class FakePointerController : public PointerControllerInterface {
147 bool mHaveBounds;
148 float mMinX, mMinY, mMaxX, mMaxY;
149 float mX, mY;
150 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800151 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153public:
154 FakePointerController() :
155 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800156 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157 }
158
Michael Wright17db18e2020-06-26 20:51:44 +0100159 virtual ~FakePointerController() {}
160
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 void setBounds(float minX, float minY, float maxX, float maxY) {
162 mHaveBounds = true;
163 mMinX = minX;
164 mMinY = minY;
165 mMaxX = maxX;
166 mMaxY = maxY;
167 }
168
Chris Yea52ade12020-08-27 16:49:20 -0700169 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 mX = x;
171 mY = y;
172 }
173
Chris Yea52ade12020-08-27 16:49:20 -0700174 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 *outX = mX;
180 *outY = mY;
181 }
182
Chris Yea52ade12020-08-27 16:49:20 -0700183 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800184
Chris Yea52ade12020-08-27 16:49:20 -0700185 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800186 mDisplayId = viewport.displayId;
187 }
188
Arthur Hung7c645402019-01-25 17:45:42 +0800189 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
190 return mSpotsByDisplay;
191 }
192
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193private:
Chris Yea52ade12020-08-27 16:49:20 -0700194 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 *outMinX = mMinX;
196 *outMinY = mMinY;
197 *outMaxX = mMaxX;
198 *outMaxY = mMaxY;
199 return mHaveBounds;
200 }
201
Chris Yea52ade12020-08-27 16:49:20 -0700202 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 mX += deltaX;
204 if (mX < mMinX) mX = mMinX;
205 if (mX > mMaxX) mX = mMaxX;
206 mY += deltaY;
207 if (mY < mMinY) mY = mMinY;
208 if (mY > mMaxY) mY = mMaxY;
209 }
210
Chris Yea52ade12020-08-27 16:49:20 -0700211 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212
Chris Yea52ade12020-08-27 16:49:20 -0700213 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214
Chris Yea52ade12020-08-27 16:49:20 -0700215 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
Chris Yea52ade12020-08-27 16:49:20 -0700217 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
218 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800219 std::vector<int32_t> newSpots;
220 // Add spots for fingers that are down.
221 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
222 uint32_t id = idBits.clearFirstMarkedBit();
223 newSpots.push_back(id);
224 }
225
226 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227 }
228
Prabir Pradhan197e0862022-07-01 14:28:00 +0000229 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800230
231 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232};
233
234
235// --- FakeInputReaderPolicy ---
236
237class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700238 std::mutex mLock;
239 std::condition_variable mDevicesChangedCondition;
240
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000242 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700243 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
244 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100245 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700246 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247
248protected:
Chris Yea52ade12020-08-27 16:49:20 -0700249 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250
251public:
252 FakeInputReaderPolicy() {
253 }
254
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 waitForInputDevices([](bool devicesChanged) {
257 if (!devicesChanged) {
258 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
259 }
260 });
261 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800263 void assertInputDevicesNotChanged() {
264 waitForInputDevices([](bool devicesChanged) {
265 if (devicesChanged) {
266 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
267 }
268 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700269 }
270
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700271 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100272 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100273 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700274 }
275
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700276 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
277 return mConfig.getDisplayViewportByUniqueId(uniqueId);
278 }
279 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
280 return mConfig.getDisplayViewportByType(type);
281 }
282
283 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
284 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 }
286
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 void addDisplayViewport(DisplayViewport viewport) {
288 mViewports.push_back(std::move(viewport));
289 mConfig.setDisplayViewports(mViewports);
290 }
291
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700292 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000293 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700294 std::optional<uint8_t> physicalPort, ViewportType type) {
295 const bool isRotated =
296 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
297 DisplayViewport v;
298 v.displayId = displayId;
299 v.orientation = orientation;
300 v.logicalLeft = 0;
301 v.logicalTop = 0;
302 v.logicalRight = isRotated ? height : width;
303 v.logicalBottom = isRotated ? width : height;
304 v.physicalLeft = 0;
305 v.physicalTop = 0;
306 v.physicalRight = isRotated ? height : width;
307 v.physicalBottom = isRotated ? width : height;
308 v.deviceWidth = isRotated ? height : width;
309 v.deviceHeight = isRotated ? width : height;
310 v.isActive = isActive;
311 v.uniqueId = uniqueId;
312 v.physicalPort = physicalPort;
313 v.type = type;
314
315 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 }
317
Arthur Hung6cd19a42019-08-30 19:04:12 +0800318 bool updateViewport(const DisplayViewport& viewport) {
319 size_t count = mViewports.size();
320 for (size_t i = 0; i < count; i++) {
321 const DisplayViewport& currentViewport = mViewports[i];
322 if (currentViewport.displayId == viewport.displayId) {
323 mViewports[i] = viewport;
324 mConfig.setDisplayViewports(mViewports);
325 return true;
326 }
327 }
328 // no viewport found.
329 return false;
330 }
331
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100332 void addExcludedDeviceName(const std::string& deviceName) {
333 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700336 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
337 mConfig.portAssociations.insert({inputPort, displayPort});
338 }
339
Christine Franks1ba71cc2021-04-07 14:37:42 -0700340 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
341 const std::string& displayUniqueId) {
342 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
343 }
344
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000345 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700346
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000347 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700348
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000349 void setPointerController(std::shared_ptr<FakePointerController> controller) {
350 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 }
352
353 const InputReaderConfiguration* getReaderConfiguration() const {
354 return &mConfig;
355 }
356
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800357 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 return mInputDevices;
359 }
360
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100361 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700362 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700363 return transform;
364 }
365
366 void setTouchAffineTransformation(const TouchAffineTransformation t) {
367 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800368 }
369
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000370 PointerCaptureRequest setPointerCapture(bool enabled) {
371 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
372 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800373 }
374
Arthur Hung7c645402019-01-25 17:45:42 +0800375 void setShowTouches(bool enabled) {
376 mConfig.showTouches = enabled;
377 }
378
Garfield Tan888a6a42020-01-09 11:39:16 -0800379 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
380 mConfig.defaultPointerDisplayId = pointerDisplayId;
381 }
382
HQ Liue6983c72022-04-19 22:14:56 +0000383 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
384
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800385 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
386
HQ Liue6983c72022-04-19 22:14:56 +0000387 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
388
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000389 void setVelocityControlParams(const VelocityControlParameters& params) {
390 mConfig.pointerVelocityControlParameters = params;
391 mConfig.wheelVelocityControlParameters = params;
392 }
393
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000395 uint32_t mNextPointerCaptureSequenceNumber = 0;
396
Chris Yea52ade12020-08-27 16:49:20 -0700397 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800398 *outConfig = mConfig;
399 }
400
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000401 std::shared_ptr<PointerControllerInterface> obtainPointerController(
402 int32_t /*deviceId*/) override {
403 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 }
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700407 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700409 mInputDevicesChanged = true;
410 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 }
412
Chris Yea52ade12020-08-27 16:49:20 -0700413 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
414 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700415 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416 }
417
Chris Yea52ade12020-08-27 16:49:20 -0700418 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800419
420 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
421 std::unique_lock<std::mutex> lock(mLock);
422 base::ScopedLockAssertion assumeLocked(mLock);
423
424 const bool devicesChanged =
425 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
426 return mInputDevicesChanged;
427 });
428 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
429 mInputDevicesChanged = false;
430 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431};
432
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433// --- FakeEventHub ---
434
435class FakeEventHub : public EventHubInterface {
436 struct KeyInfo {
437 int32_t keyCode;
438 uint32_t flags;
439 };
440
Chris Yef59a2f42020-10-16 12:55:26 -0700441 struct SensorInfo {
442 InputDeviceSensorType sensorType;
443 int32_t sensorDataIndex;
444 };
445
Michael Wrightd02c5b62014-02-10 15:10:22 -0800446 struct Device {
447 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700448 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449 PropertyMap configuration;
450 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
451 KeyedVector<int, bool> relativeAxes;
452 KeyedVector<int32_t, int32_t> keyCodeStates;
453 KeyedVector<int32_t, int32_t> scanCodeStates;
454 KeyedVector<int32_t, int32_t> switchStates;
455 KeyedVector<int32_t, int32_t> absoluteAxisValue;
456 KeyedVector<int32_t, KeyInfo> keysByScanCode;
457 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
458 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100459 // fake mapping which would normally come from keyCharacterMap
460 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700461 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
462 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800463 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700464 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000465 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700466
467 status_t enable() {
468 enabled = true;
469 return OK;
470 }
471
472 status_t disable() {
473 enabled = false;
474 return OK;
475 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700477 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 };
479
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700480 std::mutex mLock;
481 std::condition_variable mEventsCondition;
482
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100484 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000485 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600486 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000487 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800488 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
489 // Simulates a device light brightness, from light id to light brightness.
490 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
491 // Simulates a device light intensities, from light id to light intensities map.
492 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
493 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700495public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 virtual ~FakeEventHub() {
497 for (size_t i = 0; i < mDevices.size(); i++) {
498 delete mDevices.valueAt(i);
499 }
500 }
501
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 FakeEventHub() { }
503
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700504 void addDevice(int32_t deviceId, const std::string& name,
505 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 Device* device = new Device(classes);
507 device->identifier.name = name;
508 mDevices.add(deviceId, device);
509
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000510 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511 }
512
513 void removeDevice(int32_t deviceId) {
514 delete mDevices.valueFor(deviceId);
515 mDevices.removeItem(deviceId);
516
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000517 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518 }
519
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000520 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700521 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700522 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700523 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
524 return false;
525 }
526 return device->enabled;
527 }
528
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000529 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700530 status_t result;
531 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700532 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700533 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
534 return BAD_VALUE;
535 }
536 if (device->enabled) {
537 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
538 return OK;
539 }
540 result = device->enable();
541 return result;
542 }
543
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000544 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700545 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700546 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700547 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
548 return BAD_VALUE;
549 }
550 if (!device->enabled) {
551 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
552 return OK;
553 }
554 return device->disable();
555 }
556
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000558 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 }
560
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700561 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 Device* device = getDevice(deviceId);
563 device->configuration.addProperty(key, value);
564 }
565
566 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
567 Device* device = getDevice(deviceId);
568 device->configuration.addAll(configuration);
569 }
570
571 void addAbsoluteAxis(int32_t deviceId, int axis,
572 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
573 Device* device = getDevice(deviceId);
574
575 RawAbsoluteAxisInfo info;
576 info.valid = true;
577 info.minValue = minValue;
578 info.maxValue = maxValue;
579 info.flat = flat;
580 info.fuzz = fuzz;
581 info.resolution = resolution;
582 device->absoluteAxes.add(axis, info);
583 }
584
585 void addRelativeAxis(int32_t deviceId, int32_t axis) {
586 Device* device = getDevice(deviceId);
587 device->relativeAxes.add(axis, true);
588 }
589
590 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
591 Device* device = getDevice(deviceId);
592 device->keyCodeStates.replaceValueFor(keyCode, state);
593 }
594
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000595 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
596 Device* device = getDevice(deviceId);
597 device->countryCode = countryCode;
598 }
599
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
601 Device* device = getDevice(deviceId);
602 device->scanCodeStates.replaceValueFor(scanCode, state);
603 }
604
605 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
606 Device* device = getDevice(deviceId);
607 device->switchStates.replaceValueFor(switchCode, state);
608 }
609
610 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
611 Device* device = getDevice(deviceId);
612 device->absoluteAxisValue.replaceValueFor(axis, value);
613 }
614
615 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
616 int32_t keyCode, uint32_t flags) {
617 Device* device = getDevice(deviceId);
618 KeyInfo info;
619 info.keyCode = keyCode;
620 info.flags = flags;
621 if (scanCode) {
622 device->keysByScanCode.add(scanCode, info);
623 }
624 if (usageCode) {
625 device->keysByUsageCode.add(usageCode, info);
626 }
627 }
628
Philip Junker4af3b3d2021-12-14 10:36:55 +0100629 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
630 Device* device = getDevice(deviceId);
631 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
632 }
633
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 void addLed(int32_t deviceId, int32_t led, bool initialState) {
635 Device* device = getDevice(deviceId);
636 device->leds.add(led, initialState);
637 }
638
Chris Yef59a2f42020-10-16 12:55:26 -0700639 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
640 int32_t sensorDataIndex) {
641 Device* device = getDevice(deviceId);
642 SensorInfo info;
643 info.sensorType = sensorType;
644 info.sensorDataIndex = sensorDataIndex;
645 device->sensorsByAbsCode.emplace(absCode, info);
646 }
647
648 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
649 Device* device = getDevice(deviceId);
650 typename BitArray<MSC_MAX>::Buffer buffer;
651 buffer[mscEvent / 32] = 1 << mscEvent % 32;
652 device->mscBitmask.loadFromBuffer(buffer);
653 }
654
Chris Ye3fdbfef2021-01-06 18:45:18 -0800655 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
656 mRawLightInfos.emplace(rawId, std::move(info));
657 }
658
659 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
660 mLightBrightness.emplace(rawId, brightness);
661 }
662
663 void fakeLightIntensities(int32_t rawId,
664 const std::unordered_map<LightColor, int32_t> intensities) {
665 mLightIntensities.emplace(rawId, std::move(intensities));
666 }
667
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 bool getLedState(int32_t deviceId, int32_t led) {
669 Device* device = getDevice(deviceId);
670 return device->leds.valueFor(led);
671 }
672
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100673 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674 return mExcludedDevices;
675 }
676
677 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
678 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800679 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 }
681
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000682 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
683 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700684 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 RawEvent event;
686 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000687 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 event.deviceId = deviceId;
689 event.type = type;
690 event.code = code;
691 event.value = value;
692 mEvents.push_back(event);
693
694 if (type == EV_ABS) {
695 setAbsoluteAxisValue(deviceId, code, value);
696 }
697 }
698
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600699 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
700 std::vector<TouchVideoFrame>> videoFrames) {
701 mVideoFrames = std::move(videoFrames);
702 }
703
Michael Wrightd02c5b62014-02-10 15:10:22 -0800704 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700705 std::unique_lock<std::mutex> lock(mLock);
706 base::ScopedLockAssertion assumeLocked(mLock);
707 const bool queueIsEmpty =
708 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
709 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
710 if (!queueIsEmpty) {
711 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
712 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713 }
714
715private:
716 Device* getDevice(int32_t deviceId) const {
717 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100718 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 }
720
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700721 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700723 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 }
725
Chris Yea52ade12020-08-27 16:49:20 -0700726 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 Device* device = getDevice(deviceId);
728 return device ? device->identifier : InputDeviceIdentifier();
729 }
730
Chris Yea52ade12020-08-27 16:49:20 -0700731 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732
Chris Yea52ade12020-08-27 16:49:20 -0700733 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734 Device* device = getDevice(deviceId);
735 if (device) {
736 *outConfiguration = device->configuration;
737 }
738 }
739
Chris Yea52ade12020-08-27 16:49:20 -0700740 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
741 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800743 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744 ssize_t index = device->absoluteAxes.indexOfKey(axis);
745 if (index >= 0) {
746 *outAxisInfo = device->absoluteAxes.valueAt(index);
747 return OK;
748 }
749 }
750 outAxisInfo->clear();
751 return -1;
752 }
753
Chris Yea52ade12020-08-27 16:49:20 -0700754 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 Device* device = getDevice(deviceId);
756 if (device) {
757 return device->relativeAxes.indexOfKey(axis) >= 0;
758 }
759 return false;
760 }
761
Chris Yea52ade12020-08-27 16:49:20 -0700762 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763
Chris Yef59a2f42020-10-16 12:55:26 -0700764 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
765 Device* device = getDevice(deviceId);
766 if (device) {
767 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
768 }
769 return false;
770 }
771
Chris Yea52ade12020-08-27 16:49:20 -0700772 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
773 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 Device* device = getDevice(deviceId);
775 if (device) {
776 const KeyInfo* key = getKey(device, scanCode, usageCode);
777 if (key) {
778 if (outKeycode) {
779 *outKeycode = key->keyCode;
780 }
781 if (outFlags) {
782 *outFlags = key->flags;
783 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700784 if (outMetaState) {
785 *outMetaState = metaState;
786 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787 return OK;
788 }
789 }
790 return NAME_NOT_FOUND;
791 }
792
793 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
794 if (usageCode) {
795 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
796 if (index >= 0) {
797 return &device->keysByUsageCode.valueAt(index);
798 }
799 }
800 if (scanCode) {
801 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
802 if (index >= 0) {
803 return &device->keysByScanCode.valueAt(index);
804 }
805 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700806 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 }
808
Chris Yea52ade12020-08-27 16:49:20 -0700809 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000811 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
812 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700813 Device* device = getDevice(deviceId);
814 if (!device) {
815 return Errorf("Sensor device not found.");
816 }
817 auto it = device->sensorsByAbsCode.find(absCode);
818 if (it == device->sensorsByAbsCode.end()) {
819 return Errorf("Sensor map not found.");
820 }
821 const SensorInfo& info = it->second;
822 return std::make_pair(info.sensorType, info.sensorDataIndex);
823 }
824
Chris Yea52ade12020-08-27 16:49:20 -0700825 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 mExcludedDevices = devices;
827 }
828
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700829 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000830 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700832 std::vector<RawEvent> buffer;
833 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000834
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700835 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700836 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837 }
838
Chris Yea52ade12020-08-27 16:49:20 -0700839 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600840 auto it = mVideoFrames.find(deviceId);
841 if (it != mVideoFrames.end()) {
842 std::vector<TouchVideoFrame> frames = std::move(it->second);
843 mVideoFrames.erase(deviceId);
844 return frames;
845 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800846 return {};
847 }
848
Chris Yea52ade12020-08-27 16:49:20 -0700849 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850 Device* device = getDevice(deviceId);
851 if (device) {
852 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
853 if (index >= 0) {
854 return device->scanCodeStates.valueAt(index);
855 }
856 }
857 return AKEY_STATE_UNKNOWN;
858 }
859
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000860 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
861 Device* device = getDevice(deviceId);
862 if (device) {
863 return device->countryCode;
864 }
865 return InputDeviceCountryCode::INVALID;
866 }
867
Chris Yea52ade12020-08-27 16:49:20 -0700868 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 Device* device = getDevice(deviceId);
870 if (device) {
871 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
872 if (index >= 0) {
873 return device->keyCodeStates.valueAt(index);
874 }
875 }
876 return AKEY_STATE_UNKNOWN;
877 }
878
Chris Yea52ade12020-08-27 16:49:20 -0700879 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880 Device* device = getDevice(deviceId);
881 if (device) {
882 ssize_t index = device->switchStates.indexOfKey(sw);
883 if (index >= 0) {
884 return device->switchStates.valueAt(index);
885 }
886 }
887 return AKEY_STATE_UNKNOWN;
888 }
889
Chris Yea52ade12020-08-27 16:49:20 -0700890 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
891 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 Device* device = getDevice(deviceId);
893 if (device) {
894 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
895 if (index >= 0) {
896 *outValue = device->absoluteAxisValue.valueAt(index);
897 return OK;
898 }
899 }
900 *outValue = 0;
901 return -1;
902 }
903
Philip Junker4af3b3d2021-12-14 10:36:55 +0100904 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
905 Device* device = getDevice(deviceId);
906 if (!device) {
907 return AKEYCODE_UNKNOWN;
908 }
909 auto it = device->keyCodeMapping.find(locationKeyCode);
910 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
911 }
912
Chris Yea52ade12020-08-27 16:49:20 -0700913 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700914 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700915 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 bool result = false;
917 Device* device = getDevice(deviceId);
918 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700919 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700920 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
922 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
923 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924 }
925 }
926 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
927 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
928 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 }
930 }
931 }
932 }
933 return result;
934 }
935
Chris Yea52ade12020-08-27 16:49:20 -0700936 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 Device* device = getDevice(deviceId);
938 if (device) {
939 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
940 return index >= 0;
941 }
942 return false;
943 }
944
Arthur Hungcb40a002021-08-03 14:31:01 +0000945 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
946 Device* device = getDevice(deviceId);
947 if (!device) {
948 return false;
949 }
950 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
951 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
952 return true;
953 }
954 }
955 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
956 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
957 return true;
958 }
959 }
960 return false;
961 }
962
Chris Yea52ade12020-08-27 16:49:20 -0700963 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 Device* device = getDevice(deviceId);
965 return device && device->leds.indexOfKey(led) >= 0;
966 }
967
Chris Yea52ade12020-08-27 16:49:20 -0700968 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 Device* device = getDevice(deviceId);
970 if (device) {
971 ssize_t index = device->leds.indexOfKey(led);
972 if (index >= 0) {
973 device->leds.replaceValueAt(led, on);
974 } else {
975 ADD_FAILURE()
976 << "Attempted to set the state of an LED that the EventHub declared "
977 "was not present. led=" << led;
978 }
979 }
980 }
981
Chris Yea52ade12020-08-27 16:49:20 -0700982 void getVirtualKeyDefinitions(
983 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 outVirtualKeys.clear();
985
986 Device* device = getDevice(deviceId);
987 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800988 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 }
990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700993 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994 }
995
Chris Yea52ade12020-08-27 16:49:20 -0700996 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 return false;
998 }
999
Chris Yea52ade12020-08-27 16:49:20 -07001000 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001
Chris Yea52ade12020-08-27 16:49:20 -07001002 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001004 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001005
Chris Yee2b1e5c2021-03-10 22:45:12 -08001006 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1007 return BATTERY_CAPACITY;
1008 }
Kim Low03ea0352020-11-06 12:45:07 -08001009
Chris Yee2b1e5c2021-03-10 22:45:12 -08001010 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1011 return BATTERY_STATUS;
1012 }
1013
Andy Chenf9f1a022022-08-29 20:07:10 -04001014 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1015 return {DEFAULT_BATTERY};
1016 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001017
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001018 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1019 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001020 if (batteryId != DEFAULT_BATTERY) return {};
1021 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1022 .name = "default battery",
1023 .flags = InputBatteryClass::CAPACITY,
1024 .path = BATTERY_DEVPATH};
1025 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001026 }
Kim Low03ea0352020-11-06 12:45:07 -08001027
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001028 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001029 std::vector<int32_t> ids;
1030 for (const auto& [rawId, info] : mRawLightInfos) {
1031 ids.push_back(rawId);
1032 }
1033 return ids;
1034 }
1035
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001036 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001037 auto it = mRawLightInfos.find(lightId);
1038 if (it == mRawLightInfos.end()) {
1039 return std::nullopt;
1040 }
1041 return it->second;
1042 }
1043
1044 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1045 mLightBrightness.emplace(lightId, brightness);
1046 }
1047
1048 void setLightIntensities(int32_t deviceId, int32_t lightId,
1049 std::unordered_map<LightColor, int32_t> intensities) override {
1050 mLightIntensities.emplace(lightId, intensities);
1051 };
1052
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001053 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001054 auto lightIt = mLightBrightness.find(lightId);
1055 if (lightIt == mLightBrightness.end()) {
1056 return std::nullopt;
1057 }
1058 return lightIt->second;
1059 }
1060
1061 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001062 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001063 auto lightIt = mLightIntensities.find(lightId);
1064 if (lightIt == mLightIntensities.end()) {
1065 return std::nullopt;
1066 }
1067 return lightIt->second;
1068 };
1069
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001070 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001072 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073
Chris Yea52ade12020-08-27 16:49:20 -07001074 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075
Chris Yea52ade12020-08-27 16:49:20 -07001076 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077};
1078
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079// --- FakeInputMapper ---
1080
1081class FakeInputMapper : public InputMapper {
1082 uint32_t mSources;
1083 int32_t mKeyboardType;
1084 int32_t mMetaState;
1085 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1086 KeyedVector<int32_t, int32_t> mScanCodeStates;
1087 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001088 // fake mapping which would normally come from keyCharacterMap
1089 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001090 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001092 std::mutex mLock;
1093 std::condition_variable mStateChangedCondition;
1094 bool mConfigureWasCalled GUARDED_BY(mLock);
1095 bool mResetWasCalled GUARDED_BY(mLock);
1096 bool mProcessWasCalled GUARDED_BY(mLock);
1097 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098
Arthur Hungc23540e2018-11-29 20:42:11 +08001099 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001101 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1102 : InputMapper(deviceContext),
1103 mSources(sources),
1104 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001106 mConfigureWasCalled(false),
1107 mResetWasCalled(false),
1108 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
Chris Yea52ade12020-08-27 16:49:20 -07001110 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111
1112 void setKeyboardType(int32_t keyboardType) {
1113 mKeyboardType = keyboardType;
1114 }
1115
1116 void setMetaState(int32_t metaState) {
1117 mMetaState = metaState;
1118 }
1119
1120 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121 std::unique_lock<std::mutex> lock(mLock);
1122 base::ScopedLockAssertion assumeLocked(mLock);
1123 const bool configureCalled =
1124 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1125 return mConfigureWasCalled;
1126 });
1127 if (!configureCalled) {
1128 FAIL() << "Expected configure() to have been called.";
1129 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 mConfigureWasCalled = false;
1131 }
1132
1133 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001134 std::unique_lock<std::mutex> lock(mLock);
1135 base::ScopedLockAssertion assumeLocked(mLock);
1136 const bool resetCalled =
1137 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1138 return mResetWasCalled;
1139 });
1140 if (!resetCalled) {
1141 FAIL() << "Expected reset() to have been called.";
1142 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143 mResetWasCalled = false;
1144 }
1145
Yi Kong9b14ac62018-07-17 13:48:38 -07001146 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001147 std::unique_lock<std::mutex> lock(mLock);
1148 base::ScopedLockAssertion assumeLocked(mLock);
1149 const bool processCalled =
1150 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1151 return mProcessWasCalled;
1152 });
1153 if (!processCalled) {
1154 FAIL() << "Expected process() to have been called.";
1155 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 if (outLastEvent) {
1157 *outLastEvent = mLastEvent;
1158 }
1159 mProcessWasCalled = false;
1160 }
1161
1162 void setKeyCodeState(int32_t keyCode, int32_t state) {
1163 mKeyCodeStates.replaceValueFor(keyCode, state);
1164 }
1165
1166 void setScanCodeState(int32_t scanCode, int32_t state) {
1167 mScanCodeStates.replaceValueFor(scanCode, state);
1168 }
1169
1170 void setSwitchState(int32_t switchCode, int32_t state) {
1171 mSwitchStates.replaceValueFor(switchCode, state);
1172 }
1173
1174 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001175 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176 }
1177
Philip Junker4af3b3d2021-12-14 10:36:55 +01001178 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1179 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1180 }
1181
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001183 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184
Chris Yea52ade12020-08-27 16:49:20 -07001185 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 InputMapper::populateDeviceInfo(deviceInfo);
1187
1188 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1189 deviceInfo->setKeyboardType(mKeyboardType);
1190 }
1191 }
1192
Chris Yea52ade12020-08-27 16:49:20 -07001193 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001194 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001196
1197 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001198 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001199 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1200 mViewport = config->getDisplayViewportByPort(*displayPort);
1201 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001202
1203 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 }
1205
Chris Yea52ade12020-08-27 16:49:20 -07001206 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001207 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001209 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210 }
1211
Chris Yea52ade12020-08-27 16:49:20 -07001212 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001213 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 mLastEvent = *rawEvent;
1215 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001216 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217 }
1218
Chris Yea52ade12020-08-27 16:49:20 -07001219 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001220 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1221 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1222 }
1223
Philip Junker4af3b3d2021-12-14 10:36:55 +01001224 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1225 auto it = mKeyCodeMapping.find(locationKeyCode);
1226 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1227 }
1228
Chris Yea52ade12020-08-27 16:49:20 -07001229 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001230 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1231 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1232 }
1233
Chris Yea52ade12020-08-27 16:49:20 -07001234 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1236 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1237 }
1238
Chris Yea52ade12020-08-27 16:49:20 -07001239 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001240 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001241 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001242 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1244 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1245 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246 }
1247 }
1248 }
Chris Yea52ade12020-08-27 16:49:20 -07001249 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 return result;
1251 }
1252
1253 virtual int32_t getMetaState() {
1254 return mMetaState;
1255 }
1256
1257 virtual void fadePointer() {
1258 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001259
1260 virtual std::optional<int32_t> getAssociatedDisplay() {
1261 if (mViewport) {
1262 return std::make_optional(mViewport->displayId);
1263 }
1264 return std::nullopt;
1265 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001266};
1267
1268
1269// --- InstrumentedInputReader ---
1270
1271class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001272 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273
1274public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001275 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1276 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001277 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001278 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001280 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001282 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001284 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001285 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001286 InputDeviceIdentifier identifier;
1287 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001288 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001289 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001290 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001291 }
1292
Prabir Pradhan28efc192019-11-05 01:10:04 +00001293 // Make the protected loopOnce method accessible to tests.
1294 using InputReader::loopOnce;
1295
Michael Wrightd02c5b62014-02-10 15:10:22 -08001296protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001297 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1298 const InputDeviceIdentifier& identifier)
1299 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001300 if (!mNextDevices.empty()) {
1301 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1302 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001303 return device;
1304 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001305 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306 }
1307
arthurhungdcef2dc2020-08-11 14:47:50 +08001308 // --- FakeInputReaderContext ---
1309 class FakeInputReaderContext : public ContextImpl {
1310 int32_t mGlobalMetaState;
1311 bool mUpdateGlobalMetaStateWasCalled;
1312 int32_t mGeneration;
1313
1314 public:
1315 FakeInputReaderContext(InputReader* reader)
1316 : ContextImpl(reader),
1317 mGlobalMetaState(0),
1318 mUpdateGlobalMetaStateWasCalled(false),
1319 mGeneration(1) {}
1320
1321 virtual ~FakeInputReaderContext() {}
1322
1323 void assertUpdateGlobalMetaStateWasCalled() {
1324 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1325 << "Expected updateGlobalMetaState() to have been called.";
1326 mUpdateGlobalMetaStateWasCalled = false;
1327 }
1328
1329 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1330
1331 uint32_t getGeneration() { return mGeneration; }
1332
1333 void updateGlobalMetaState() override {
1334 mUpdateGlobalMetaStateWasCalled = true;
1335 ContextImpl::updateGlobalMetaState();
1336 }
1337
1338 int32_t getGlobalMetaState() override {
1339 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1340 }
1341
1342 int32_t bumpGeneration() override {
1343 mGeneration = ContextImpl::bumpGeneration();
1344 return mGeneration;
1345 }
1346 } mFakeContext;
1347
Michael Wrightd02c5b62014-02-10 15:10:22 -08001348 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001349
1350public:
1351 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352};
1353
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354// --- InputReaderPolicyTest ---
1355class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001356protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357 sp<FakeInputReaderPolicy> mFakePolicy;
1358
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001359 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001360 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361};
1362
1363/**
1364 * Check that empty set of viewports is an acceptable configuration.
1365 * Also try to get internal viewport two different ways - by type and by uniqueId.
1366 *
1367 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1368 * Such configuration is not currently allowed.
1369 */
1370TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001371 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372
1373 // We didn't add any viewports yet, so there shouldn't be any.
1374 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001375 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001376 ASSERT_FALSE(internalViewport);
1377
1378 // Add an internal viewport, then clear it
1379 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001380 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001381 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001382
1383 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001384 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001385 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001386 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387
1388 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001391 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001392
1393 mFakePolicy->clearViewports();
1394 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001395 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001397 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001398 ASSERT_FALSE(internalViewport);
1399}
1400
1401TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1402 const std::string internalUniqueId = "local:0";
1403 const std::string externalUniqueId = "local:1";
1404 const std::string virtualUniqueId1 = "virtual:2";
1405 const std::string virtualUniqueId2 = "virtual:3";
1406 constexpr int32_t virtualDisplayId1 = 2;
1407 constexpr int32_t virtualDisplayId2 = 3;
1408
1409 // Add an internal viewport
1410 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001411 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1412 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 // Add an external viewport
1414 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1416 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 // Add an virtual viewport
1418 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001419 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1420 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 // Add another virtual viewport
1422 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001423 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1424 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425
1426 // Check matching by type for internal
1427 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001428 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001429 ASSERT_TRUE(internalViewport);
1430 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1431
1432 // Check matching by type for external
1433 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001434 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 ASSERT_TRUE(externalViewport);
1436 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1437
1438 // Check matching by uniqueId for virtual viewport #1
1439 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1444 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1445
1446 // Check matching by uniqueId for virtual viewport #2
1447 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001448 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001449 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001450 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1452 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1453}
1454
1455
1456/**
1457 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1458 * that lookup works by checking display id.
1459 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1460 */
1461TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1462 const std::string uniqueId1 = "uniqueId1";
1463 const std::string uniqueId2 = "uniqueId2";
1464 constexpr int32_t displayId1 = 2;
1465 constexpr int32_t displayId2 = 3;
1466
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001467 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1468 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001469 for (const ViewportType& type : types) {
1470 mFakePolicy->clearViewports();
1471 // Add a viewport
1472 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001473 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1474 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 // Add another viewport
1476 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001477 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1478 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001479
1480 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001481 std::optional<DisplayViewport> viewport1 =
1482 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001483 ASSERT_TRUE(viewport1);
1484 ASSERT_EQ(displayId1, viewport1->displayId);
1485 ASSERT_EQ(type, viewport1->type);
1486
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001487 std::optional<DisplayViewport> viewport2 =
1488 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001489 ASSERT_TRUE(viewport2);
1490 ASSERT_EQ(displayId2, viewport2->displayId);
1491 ASSERT_EQ(type, viewport2->type);
1492
1493 // When there are multiple viewports of the same kind, and uniqueId is not specified
1494 // in the call to getDisplayViewport, then that situation is not supported.
1495 // The viewports can be stored in any order, so we cannot rely on the order, since that
1496 // is just implementation detail.
1497 // However, we can check that it still returns *a* viewport, we just cannot assert
1498 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001499 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001500 ASSERT_TRUE(someViewport);
1501 }
1502}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001504/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001505 * When we have multiple internal displays make sure we always return the default display when
1506 * querying by type.
1507 */
1508TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1509 const std::string uniqueId1 = "uniqueId1";
1510 const std::string uniqueId2 = "uniqueId2";
1511 constexpr int32_t nonDefaultDisplayId = 2;
1512 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1513 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1514
1515 // Add the default display first and ensure it gets returned.
1516 mFakePolicy->clearViewports();
1517 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001518 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001519 ViewportType::INTERNAL);
1520 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001521 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001522 ViewportType::INTERNAL);
1523
1524 std::optional<DisplayViewport> viewport =
1525 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1526 ASSERT_TRUE(viewport);
1527 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1528 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1529
1530 // Add the default display second to make sure order doesn't matter.
1531 mFakePolicy->clearViewports();
1532 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001533 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001534 ViewportType::INTERNAL);
1535 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001536 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001537 ViewportType::INTERNAL);
1538
1539 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1540 ASSERT_TRUE(viewport);
1541 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1542 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1543}
1544
1545/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001546 * Check getDisplayViewportByPort
1547 */
1548TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001549 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001550 const std::string uniqueId1 = "uniqueId1";
1551 const std::string uniqueId2 = "uniqueId2";
1552 constexpr int32_t displayId1 = 1;
1553 constexpr int32_t displayId2 = 2;
1554 const uint8_t hdmi1 = 0;
1555 const uint8_t hdmi2 = 1;
1556 const uint8_t hdmi3 = 2;
1557
1558 mFakePolicy->clearViewports();
1559 // Add a viewport that's associated with some display port that's not of interest.
1560 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001561 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1562 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001563 // Add another viewport, connected to HDMI1 port
1564 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001565 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1566 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001567
1568 // Check that correct display viewport was returned by comparing the display ports.
1569 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1570 ASSERT_TRUE(hdmi1Viewport);
1571 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1572 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1573
1574 // Check that we can still get the same viewport using the uniqueId
1575 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1576 ASSERT_TRUE(hdmi1Viewport);
1577 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1578 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1579 ASSERT_EQ(type, hdmi1Viewport->type);
1580
1581 // Check that we cannot find a port with "HDMI2", because we never added one
1582 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1583 ASSERT_FALSE(hdmi2Viewport);
1584}
1585
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586// --- InputReaderTest ---
1587
1588class InputReaderTest : public testing::Test {
1589protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001590 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001592 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001593 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594
Chris Yea52ade12020-08-27 16:49:20 -07001595 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001596 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001597 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001598 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001599
Prabir Pradhan28efc192019-11-05 01:10:04 +00001600 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001601 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001602 }
1603
Chris Yea52ade12020-08-27 16:49:20 -07001604 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001605 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 }
1608
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001609 void addDevice(int32_t eventHubId, const std::string& name,
1610 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001611 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001612
1613 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001614 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615 }
1616 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001617 mReader->loopOnce();
1618 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001619 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1620 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001621 }
1622
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001623 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001624 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001625 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001626 }
1627
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001628 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001629 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001630 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001631 }
1632
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001634 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001635 ftl::Flags<InputDeviceClass> classes,
1636 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001637 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001638 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1639 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001640 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642 return mapper;
1643 }
1644};
1645
Chris Ye98d3f532020-10-01 21:48:59 -07001646TEST_F(InputReaderTest, PolicyGetInputDevices) {
1647 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001648 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001649 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650
1651 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001652 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001653 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001654 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001655 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001656 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1657 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001658 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001659}
1660
Chris Yee7310032020-09-22 15:36:28 -07001661TEST_F(InputReaderTest, GetMergedInputDevices) {
1662 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1663 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1664 // Add two subdevices to device
1665 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1666 // Must add at least one mapper or the device will be ignored!
1667 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1668 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1669
1670 // Push same device instance for next device to be added, so they'll have same identifier.
1671 mReader->pushNextDevice(device);
1672 mReader->pushNextDevice(device);
1673 ASSERT_NO_FATAL_FAILURE(
1674 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1675 ASSERT_NO_FATAL_FAILURE(
1676 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1677
1678 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001679 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001680}
1681
Chris Yee14523a2020-12-19 13:46:00 -08001682TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1683 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1684 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1685 // Add two subdevices to device
1686 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1687 // Must add at least one mapper or the device will be ignored!
1688 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1689 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1690
1691 // Push same device instance for next device to be added, so they'll have same identifier.
1692 mReader->pushNextDevice(device);
1693 mReader->pushNextDevice(device);
1694 // Sensor device is initially disabled
1695 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1696 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1697 nullptr));
1698 // Device is disabled because the only sub device is a sensor device and disabled initially.
1699 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1700 ASSERT_FALSE(device->isEnabled());
1701 ASSERT_NO_FATAL_FAILURE(
1702 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1703 // The merged device is enabled if any sub device is enabled
1704 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1705 ASSERT_TRUE(device->isEnabled());
1706}
1707
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001708TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001710 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001711 constexpr int32_t eventHubId = 1;
1712 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001713 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001714 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001715 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001716 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717
Yi Kong9b14ac62018-07-17 13:48:38 -07001718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001719
1720 NotifyDeviceResetArgs resetArgs;
1721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001722 ASSERT_EQ(deviceId, resetArgs.deviceId);
1723
1724 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001725 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001726 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001727
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001729 ASSERT_EQ(deviceId, resetArgs.deviceId);
1730 ASSERT_EQ(device->isEnabled(), false);
1731
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001732 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001733 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001736 ASSERT_EQ(device->isEnabled(), false);
1737
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001738 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001739 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001741 ASSERT_EQ(deviceId, resetArgs.deviceId);
1742 ASSERT_EQ(device->isEnabled(), true);
1743}
1744
Michael Wrightd02c5b62014-02-10 15:10:22 -08001745TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001746 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001747 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001748 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001749 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001750 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001751 AINPUT_SOURCE_KEYBOARD, nullptr);
1752 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001753
1754 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1755 AINPUT_SOURCE_ANY, AKEYCODE_A))
1756 << "Should return unknown when the device id is >= 0 but unknown.";
1757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001758 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1759 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1760 << "Should return unknown when the device id is valid but the sources are not "
1761 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001762
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001763 ASSERT_EQ(AKEY_STATE_DOWN,
1764 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1765 AKEYCODE_A))
1766 << "Should return value provided by mapper when device id is valid and the device "
1767 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768
1769 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1770 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1771 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1772
1773 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1774 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1775 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1776}
1777
Philip Junker4af3b3d2021-12-14 10:36:55 +01001778TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1779 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1780 constexpr int32_t eventHubId = 1;
1781 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1782 InputDeviceClass::KEYBOARD,
1783 AINPUT_SOURCE_KEYBOARD, nullptr);
1784 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1785
1786 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1787 << "Should return unknown when the device with the specified id is not found.";
1788
1789 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1790 << "Should return correct mapping when device id is valid and mapping exists.";
1791
1792 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1793 << "Should return the location key code when device id is valid and there's no "
1794 "mapping.";
1795}
1796
1797TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1799 constexpr int32_t eventHubId = 1;
1800 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1801 InputDeviceClass::JOYSTICK,
1802 AINPUT_SOURCE_GAMEPAD, nullptr);
1803 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1804
1805 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1806 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1807}
1808
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001811 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001813 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001815 AINPUT_SOURCE_KEYBOARD, nullptr);
1816 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001817
1818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1819 AINPUT_SOURCE_ANY, KEY_A))
1820 << "Should return unknown when the device id is >= 0 but unknown.";
1821
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001822 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1823 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1824 << "Should return unknown when the device id is valid but the sources are not "
1825 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 ASSERT_EQ(AKEY_STATE_DOWN,
1828 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1829 KEY_A))
1830 << "Should return value provided by mapper when device id is valid and the device "
1831 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832
1833 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1834 AINPUT_SOURCE_TRACKBALL, KEY_A))
1835 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1836
1837 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1838 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1839 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1840}
1841
1842TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001843 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001844 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001845 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001846 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001848 AINPUT_SOURCE_KEYBOARD, nullptr);
1849 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850
1851 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1852 AINPUT_SOURCE_ANY, SW_LID))
1853 << "Should return unknown when the device id is >= 0 but unknown.";
1854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001855 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1856 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1857 << "Should return unknown when the device id is valid but the sources are not "
1858 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001860 ASSERT_EQ(AKEY_STATE_DOWN,
1861 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1862 SW_LID))
1863 << "Should return value provided by mapper when device id is valid and the device "
1864 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001865
1866 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1867 AINPUT_SOURCE_TRACKBALL, SW_LID))
1868 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1869
1870 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1871 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1872 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1873}
1874
1875TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001876 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001877 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001878 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001879 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001880 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001881 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001883 mapper.addSupportedKeyCode(AKEYCODE_A);
1884 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001885
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001886 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887 uint8_t flags[4] = { 0, 0, 0, 1 };
1888
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001889 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890 << "Should return false when device id is >= 0 but unknown.";
1891 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1892
1893 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001894 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001895 << "Should return false when device id is valid but the sources are not supported by "
1896 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001897 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1898
1899 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001900 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001901 keyCodes, flags))
1902 << "Should return value provided by mapper when device id is valid and the device "
1903 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001904 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1905
1906 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001907 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1908 << "Should return false when the device id is < 0 but the sources are not supported by "
1909 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001910 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1911
1912 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001913 ASSERT_TRUE(
1914 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1915 << "Should return value provided by mapper when device id is < 0 and one of the "
1916 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1918}
1919
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001920TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001921 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001922 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923
1924 NotifyConfigurationChangedArgs args;
1925
1926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1927 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1928}
1929
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001930TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001932 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001933 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001935 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001936 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001937 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001938 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001939
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001940 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001941 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001942 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1943
1944 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001945 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001946 ASSERT_EQ(when, event.when);
1947 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001948 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001949 ASSERT_EQ(EV_KEY, event.type);
1950 ASSERT_EQ(KEY_A, event.code);
1951 ASSERT_EQ(1, event.value);
1952}
1953
Garfield Tan1c7bc862020-01-28 13:24:04 -08001954TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001955 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001956 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001957 constexpr int32_t eventHubId = 1;
1958 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001959 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001960 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001961 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001962 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001963
1964 NotifyDeviceResetArgs resetArgs;
1965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001966 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001968 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001969 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001971 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001972 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001974 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001975 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001977 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001978 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001980 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001981 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001983 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001984 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001985}
1986
Garfield Tan1c7bc862020-01-28 13:24:04 -08001987TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1988 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001989 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001990 constexpr int32_t eventHubId = 1;
1991 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1992 // Must add at least one mapper or the device will be ignored!
1993 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001994 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001995 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1996
1997 NotifyDeviceResetArgs resetArgs;
1998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1999 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2000}
2001
Arthur Hungc23540e2018-11-29 20:42:11 +08002002TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002003 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002004 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002005 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002006 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002007 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2008 FakeInputMapper& mapper =
2009 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002010 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002011
2012 const uint8_t hdmi1 = 1;
2013
2014 // Associated touch screen with second display.
2015 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2016
2017 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002018 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002019 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002020 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002021 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002022 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002023 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002024 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002025 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002026 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002027
2028 // Add the device, and make sure all of the callbacks are triggered.
2029 // The device is added after the input port associations are processed since
2030 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002031 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002034 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002035
Arthur Hung2c9a3342019-07-23 14:18:59 +08002036 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002037 ASSERT_EQ(deviceId, device->getId());
2038 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2039 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002040
2041 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002042 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002043 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002044 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002045}
2046
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002047TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2048 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002049 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002050 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2051 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2052 // Must add at least one mapper or the device will be ignored!
2053 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2054 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2055 mReader->pushNextDevice(device);
2056 mReader->pushNextDevice(device);
2057 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2058 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2059
2060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2061
2062 NotifyDeviceResetArgs resetArgs;
2063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2064 ASSERT_EQ(deviceId, resetArgs.deviceId);
2065 ASSERT_TRUE(device->isEnabled());
2066 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2067 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2068
2069 disableDevice(deviceId);
2070 mReader->loopOnce();
2071
2072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2073 ASSERT_EQ(deviceId, resetArgs.deviceId);
2074 ASSERT_FALSE(device->isEnabled());
2075 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2076 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2077
2078 enableDevice(deviceId);
2079 mReader->loopOnce();
2080
2081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2082 ASSERT_EQ(deviceId, resetArgs.deviceId);
2083 ASSERT_TRUE(device->isEnabled());
2084 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2085 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2086}
2087
2088TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2089 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002090 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002091 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2092 // Add two subdevices to device
2093 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2094 FakeInputMapper& mapperDevice1 =
2095 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2096 FakeInputMapper& mapperDevice2 =
2097 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2098 mReader->pushNextDevice(device);
2099 mReader->pushNextDevice(device);
2100 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2101 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2102
2103 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2104 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2105
2106 ASSERT_EQ(AKEY_STATE_DOWN,
2107 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2108 ASSERT_EQ(AKEY_STATE_DOWN,
2109 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2110 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2111 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2112}
2113
Prabir Pradhan7e186182020-11-10 13:56:45 -08002114TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2115 NotifyPointerCaptureChangedArgs args;
2116
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002117 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002118 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2119 mReader->loopOnce();
2120 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002121 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2122 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002123
2124 mFakePolicy->setPointerCapture(false);
2125 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2126 mReader->loopOnce();
2127 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002128 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002129
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002130 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002131 // does not change.
2132 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2133 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002134 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002135}
2136
Chris Ye87143712020-11-10 05:05:58 +00002137class FakeVibratorInputMapper : public FakeInputMapper {
2138public:
2139 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2140 : FakeInputMapper(deviceContext, sources) {}
2141
2142 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2143};
2144
2145TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2146 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002147 ftl::Flags<InputDeviceClass> deviceClass =
2148 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002149 constexpr int32_t eventHubId = 1;
2150 const char* DEVICE_LOCATION = "BLUETOOTH";
2151 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2152 FakeVibratorInputMapper& mapper =
2153 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2154 mReader->pushNextDevice(device);
2155
2156 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2157 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2158
2159 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2160 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2161}
2162
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002163// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002164
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002165class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002166public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002167 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002168
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002169 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002170
Andy Chenf9f1a022022-08-29 20:07:10 -04002171 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2172
Chris Yee2b1e5c2021-03-10 22:45:12 -08002173 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2174
2175 void dump(std::string& dump) override {}
2176
2177 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2178 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002179 }
2180
Chris Yee2b1e5c2021-03-10 22:45:12 -08002181 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2182 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002183 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002184
2185 bool setLightColor(int32_t lightId, int32_t color) override {
2186 getDeviceContext().setLightBrightness(lightId, color >> 24);
2187 return true;
2188 }
2189
2190 std::optional<int32_t> getLightColor(int32_t lightId) override {
2191 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2192 if (!result.has_value()) {
2193 return std::nullopt;
2194 }
2195 return result.value() << 24;
2196 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002197
2198 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2199
2200 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2201
2202private:
2203 InputDeviceContext& mDeviceContext;
2204 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2205 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002206 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002207};
2208
Chris Yee2b1e5c2021-03-10 22:45:12 -08002209TEST_F(InputReaderTest, BatteryGetCapacity) {
2210 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002211 ftl::Flags<InputDeviceClass> deviceClass =
2212 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002213 constexpr int32_t eventHubId = 1;
2214 const char* DEVICE_LOCATION = "BLUETOOTH";
2215 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002216 FakePeripheralController& controller =
2217 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002218 mReader->pushNextDevice(device);
2219
2220 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2221
2222 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2223 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2224}
2225
2226TEST_F(InputReaderTest, BatteryGetStatus) {
2227 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002228 ftl::Flags<InputDeviceClass> deviceClass =
2229 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002230 constexpr int32_t eventHubId = 1;
2231 const char* DEVICE_LOCATION = "BLUETOOTH";
2232 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002233 FakePeripheralController& controller =
2234 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002235 mReader->pushNextDevice(device);
2236
2237 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2238
2239 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2240 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2241}
2242
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002243TEST_F(InputReaderTest, BatteryGetDevicePath) {
2244 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2245 ftl::Flags<InputDeviceClass> deviceClass =
2246 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2247 constexpr int32_t eventHubId = 1;
2248 const char* DEVICE_LOCATION = "BLUETOOTH";
2249 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2250 device->addController<FakePeripheralController>(eventHubId);
2251 mReader->pushNextDevice(device);
2252
2253 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2254
2255 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2256}
2257
Chris Ye3fdbfef2021-01-06 18:45:18 -08002258TEST_F(InputReaderTest, LightGetColor) {
2259 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002260 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002261 constexpr int32_t eventHubId = 1;
2262 const char* DEVICE_LOCATION = "BLUETOOTH";
2263 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002264 FakePeripheralController& controller =
2265 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002266 mReader->pushNextDevice(device);
2267 RawLightInfo info = {.id = 1,
2268 .name = "Mono",
2269 .maxBrightness = 255,
2270 .flags = InputLightClass::BRIGHTNESS,
2271 .path = ""};
2272 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2273 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2274
2275 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002276
Chris Yee2b1e5c2021-03-10 22:45:12 -08002277 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2278 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002279 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2280 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2281}
2282
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002283// --- InputReaderIntegrationTest ---
2284
2285// These tests create and interact with the InputReader only through its interface.
2286// The InputReader is started during SetUp(), which starts its processing in its own
2287// thread. The tests use linux uinput to emulate input devices.
2288// NOTE: Interacting with the physical device while these tests are running may cause
2289// the tests to fail.
2290class InputReaderIntegrationTest : public testing::Test {
2291protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002292 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002293 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002294 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002295
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002296 std::shared_ptr<FakePointerController> mFakePointerController;
2297
Chris Yea52ade12020-08-27 16:49:20 -07002298 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002299 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002300 mFakePointerController = std::make_shared<FakePointerController>();
2301 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002302 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2303 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002304
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002305 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2306 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002307 ASSERT_EQ(mReader->start(), OK);
2308
2309 // Since this test is run on a real device, all the input devices connected
2310 // to the test device will show up in mReader. We wait for those input devices to
2311 // show up before beginning the tests.
2312 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2313 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2314 }
2315
Chris Yea52ade12020-08-27 16:49:20 -07002316 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002317 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002318 mReader.reset();
2319 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002320 mFakePolicy.clear();
2321 }
2322};
2323
2324TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2325 // An invalid input device that is only used for this test.
2326 class InvalidUinputDevice : public UinputDevice {
2327 public:
2328 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2329
2330 private:
2331 void configureDevice(int fd, uinput_user_dev* device) override {}
2332 };
2333
2334 const size_t numDevices = mFakePolicy->getInputDevices().size();
2335
2336 // UinputDevice does not set any event or key bits, so InputReader should not
2337 // consider it as a valid device.
2338 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2339 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2340 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2341 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2342
2343 invalidDevice.reset();
2344 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2345 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2346 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2347}
2348
2349TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2350 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2351
2352 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2353 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2355 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2356
2357 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002358 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002359 const auto& it =
2360 std::find_if(inputDevices.begin(), inputDevices.end(),
2361 [&keyboard](const InputDeviceInfo& info) {
2362 return info.getIdentifier().name == keyboard->getName();
2363 });
2364
2365 ASSERT_NE(it, inputDevices.end());
2366 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2367 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2368 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002369
2370 keyboard.reset();
2371 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2373 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2374}
2375
2376TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2377 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2378 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2379
2380 NotifyConfigurationChangedArgs configChangedArgs;
2381 ASSERT_NO_FATAL_FAILURE(
2382 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002383 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002384 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2385
2386 NotifyKeyArgs keyArgs;
2387 keyboard->pressAndReleaseHomeKey();
2388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2389 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002390 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002391 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002392 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002393 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002394 prevTimestamp = keyArgs.eventTime;
2395
2396 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2397 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002398 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002399 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002400 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002401}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002403/**
2404 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2405 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2406 * are passed to the listener.
2407 */
2408static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2409TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2410 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2411 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2412 NotifyKeyArgs keyArgs;
2413
2414 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2416 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2417 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2418
2419 controller->pressAndReleaseKey(BTN_GEAR_UP);
2420 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2422 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2423}
2424
Arthur Hungaab25622020-01-16 11:22:11 +08002425// --- TouchProcessTest ---
2426class TouchIntegrationTest : public InputReaderIntegrationTest {
2427protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002428 const std::string UNIQUE_ID = "local:0";
2429
Chris Yea52ade12020-08-27 16:49:20 -07002430 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002431 InputReaderIntegrationTest::SetUp();
2432 // At least add an internal display.
2433 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2434 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002435 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002436
2437 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2438 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2439 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2440 }
2441
2442 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2443 int32_t orientation, const std::string& uniqueId,
2444 std::optional<uint8_t> physicalPort,
2445 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002446 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2447 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002448 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2449 }
2450
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002451 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2452 NotifyMotionArgs args;
2453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2454 EXPECT_EQ(action, args.action);
2455 ASSERT_EQ(points.size(), args.pointerCount);
2456 for (size_t i = 0; i < args.pointerCount; i++) {
2457 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2458 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2459 }
2460 }
2461
Arthur Hungaab25622020-01-16 11:22:11 +08002462 std::unique_ptr<UinputTouchScreen> mDevice;
2463};
2464
2465TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2466 NotifyMotionArgs args;
2467 const Point centerPoint = mDevice->getCenterPoint();
2468
2469 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002470 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002471 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002472 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2474 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2475
2476 // ACTION_MOVE
2477 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002478 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2481
2482 // ACTION_UP
2483 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002484 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002485 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2487}
2488
2489TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2490 NotifyMotionArgs args;
2491 const Point centerPoint = mDevice->getCenterPoint();
2492
2493 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002494 mDevice->sendSlot(FIRST_SLOT);
2495 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002496 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002497 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2499 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2500
2501 // ACTION_POINTER_DOWN (Second slot)
2502 const Point secondPoint = centerPoint + Point(100, 100);
2503 mDevice->sendSlot(SECOND_SLOT);
2504 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002505 mDevice->sendDown(secondPoint);
2506 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002507 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002508 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002509
2510 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002511 mDevice->sendMove(secondPoint + Point(1, 1));
2512 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002513 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2515
2516 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002517 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002518 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002519 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002520 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002521
2522 // ACTION_UP
2523 mDevice->sendSlot(FIRST_SLOT);
2524 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002525 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2527 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2528}
2529
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002530/**
2531 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2532 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2533 * data?
2534 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2535 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2536 * for Pointer 0 only is generated after.
2537 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2538 * events, we will not miss any information.
2539 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2540 * event generated afterwards that contains the newest movement of pointer 0.
2541 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2542 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2543 * losing information about non-palm pointers.
2544 */
2545TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2546 NotifyMotionArgs args;
2547 const Point centerPoint = mDevice->getCenterPoint();
2548
2549 // ACTION_DOWN
2550 mDevice->sendSlot(FIRST_SLOT);
2551 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2552 mDevice->sendDown(centerPoint);
2553 mDevice->sendSync();
2554 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2555
2556 // ACTION_POINTER_DOWN (Second slot)
2557 const Point secondPoint = centerPoint + Point(100, 100);
2558 mDevice->sendSlot(SECOND_SLOT);
2559 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2560 mDevice->sendDown(secondPoint);
2561 mDevice->sendSync();
2562 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2563
2564 // ACTION_MOVE (First slot)
2565 mDevice->sendSlot(FIRST_SLOT);
2566 mDevice->sendMove(centerPoint + Point(5, 5));
2567 // ACTION_POINTER_UP (Second slot)
2568 mDevice->sendSlot(SECOND_SLOT);
2569 mDevice->sendPointerUp();
2570 // Send a single sync for the above 2 pointer updates
2571 mDevice->sendSync();
2572
2573 // First, we should get POINTER_UP for the second pointer
2574 assertReceivedMotion(ACTION_POINTER_1_UP,
2575 {/*first pointer */ centerPoint + Point(5, 5),
2576 /*second pointer*/ secondPoint});
2577
2578 // Next, the MOVE event for the first pointer
2579 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2580}
2581
2582/**
2583 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2584 * move, and then it will go up, all in the same frame.
2585 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2586 * gets sent to the listener.
2587 */
2588TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2589 NotifyMotionArgs args;
2590 const Point centerPoint = mDevice->getCenterPoint();
2591
2592 // ACTION_DOWN
2593 mDevice->sendSlot(FIRST_SLOT);
2594 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2595 mDevice->sendDown(centerPoint);
2596 mDevice->sendSync();
2597 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2598
2599 // ACTION_POINTER_DOWN (Second slot)
2600 const Point secondPoint = centerPoint + Point(100, 100);
2601 mDevice->sendSlot(SECOND_SLOT);
2602 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2603 mDevice->sendDown(secondPoint);
2604 mDevice->sendSync();
2605 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2606
2607 // ACTION_MOVE (First slot)
2608 mDevice->sendSlot(FIRST_SLOT);
2609 mDevice->sendMove(centerPoint + Point(5, 5));
2610 // ACTION_POINTER_UP (Second slot)
2611 mDevice->sendSlot(SECOND_SLOT);
2612 mDevice->sendMove(secondPoint + Point(6, 6));
2613 mDevice->sendPointerUp();
2614 // Send a single sync for the above 2 pointer updates
2615 mDevice->sendSync();
2616
2617 // First, we should get POINTER_UP for the second pointer
2618 // The movement of the second pointer during the liftoff frame is ignored.
2619 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2620 assertReceivedMotion(ACTION_POINTER_1_UP,
2621 {/*first pointer */ centerPoint + Point(5, 5),
2622 /*second pointer*/ secondPoint});
2623
2624 // Next, the MOVE event for the first pointer
2625 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2626}
2627
Arthur Hungaab25622020-01-16 11:22:11 +08002628TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2629 NotifyMotionArgs args;
2630 const Point centerPoint = mDevice->getCenterPoint();
2631
2632 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002633 mDevice->sendSlot(FIRST_SLOT);
2634 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002635 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002636 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002637 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2638 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2639
arthurhungcc7f9802020-04-30 17:55:40 +08002640 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002641 const Point secondPoint = centerPoint + Point(100, 100);
2642 mDevice->sendSlot(SECOND_SLOT);
2643 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2644 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002645 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002646 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002647 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002648
arthurhungcc7f9802020-04-30 17:55:40 +08002649 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002650 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002651 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002652 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2654
arthurhungcc7f9802020-04-30 17:55:40 +08002655 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2656 // a palm event.
2657 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002658 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002659 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002660 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002661 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002662 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002663
arthurhungcc7f9802020-04-30 17:55:40 +08002664 // Send up to second slot, expect first slot send moving.
2665 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002666 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002667 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002669
arthurhungcc7f9802020-04-30 17:55:40 +08002670 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002671 mDevice->sendSlot(FIRST_SLOT);
2672 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002673 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002674
arthurhungcc7f9802020-04-30 17:55:40 +08002675 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002677}
2678
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680class InputDeviceTest : public testing::Test {
2681protected:
2682 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002683 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684 static const int32_t DEVICE_ID;
2685 static const int32_t DEVICE_GENERATION;
2686 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002687 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002688 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002690 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002692 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002693 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002694 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695
Chris Yea52ade12020-08-27 16:49:20 -07002696 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002697 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002698 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002699 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002700 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002701 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702 InputDeviceIdentifier identifier;
2703 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002704 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002705 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002706 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002707 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002708 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002709 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 }
2711
Chris Yea52ade12020-08-27 16:49:20 -07002712 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002713 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002714 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 }
2716};
2717
2718const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002719const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002720const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2722const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002723const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002724 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002725const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726
2727TEST_F(InputDeviceTest, ImmutableProperties) {
2728 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002729 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002730 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731}
2732
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002733TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2734 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2735
2736 // Configuration
2737 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2738 InputReaderConfiguration config;
2739 mDevice->configure(ARBITRARY_TIME, &config, 0);
2740
2741 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2742}
2743
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002744TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2745 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002746}
2747
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2749 // Configuration.
2750 InputReaderConfiguration config;
2751 mDevice->configure(ARBITRARY_TIME, &config, 0);
2752
2753 // Reset.
2754 mDevice->reset(ARBITRARY_TIME);
2755
2756 NotifyDeviceResetArgs resetArgs;
2757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2758 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2759 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2760
2761 // Metadata.
2762 ASSERT_TRUE(mDevice->isIgnored());
2763 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2764
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002765 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002767 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2769 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2770
2771 // State queries.
2772 ASSERT_EQ(0, mDevice->getMetaState());
2773
2774 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2775 << "Ignored device should return unknown key code state.";
2776 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2777 << "Ignored device should return unknown scan code state.";
2778 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2779 << "Ignored device should return unknown switch state.";
2780
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002781 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002783 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 << "Ignored device should never mark any key codes.";
2785 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2786 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2787}
2788
2789TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2790 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002791 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002793 FakeInputMapper& mapper1 =
2794 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002795 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2796 mapper1.setMetaState(AMETA_ALT_ON);
2797 mapper1.addSupportedKeyCode(AKEYCODE_A);
2798 mapper1.addSupportedKeyCode(AKEYCODE_B);
2799 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2800 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2801 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2802 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2803 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002805 FakeInputMapper& mapper2 =
2806 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002807 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808
2809 InputReaderConfiguration config;
2810 mDevice->configure(ARBITRARY_TIME, &config, 0);
2811
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002812 std::string propertyValue;
2813 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002815 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002817 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2818 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819
2820 // Reset
2821 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002822 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2823 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824
2825 NotifyDeviceResetArgs resetArgs;
2826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2827 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2828 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2829
2830 // Metadata.
2831 ASSERT_FALSE(mDevice->isIgnored());
2832 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2833
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002834 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002836 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2838 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2839
2840 // State queries.
2841 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2842 << "Should query mappers and combine meta states.";
2843
2844 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2845 << "Should return unknown key code state when source not supported.";
2846 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2847 << "Should return unknown scan code state when source not supported.";
2848 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2849 << "Should return unknown switch state when source not supported.";
2850
2851 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2852 << "Should query mapper when source is supported.";
2853 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2854 << "Should query mapper when source is supported.";
2855 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2856 << "Should query mapper when source is supported.";
2857
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002858 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002860 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861 << "Should do nothing when source is unsupported.";
2862 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2863 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2864 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2865 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2866
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002867 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002868 << "Should query mapper when source is supported.";
2869 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2870 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2871 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2872 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2873
2874 // Event handling.
2875 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002876 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877 mDevice->process(&event, 1);
2878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002879 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2880 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881}
2882
Arthur Hung2c9a3342019-07-23 14:18:59 +08002883// A single input device is associated with a specific display. Check that:
2884// 1. Device is disabled if the viewport corresponding to the associated display is not found
2885// 2. Device is disabled when setEnabled API is called
2886TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002887 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002888
2889 // First Configuration.
2890 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2891
2892 // Device should be enabled by default.
2893 ASSERT_TRUE(mDevice->isEnabled());
2894
2895 // Prepare associated info.
2896 constexpr uint8_t hdmi = 1;
2897 const std::string UNIQUE_ID = "local:1";
2898
2899 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2900 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2901 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2902 // Device should be disabled because it is associated with a specific display via
2903 // input port <-> display port association, but the corresponding display is not found
2904 ASSERT_FALSE(mDevice->isEnabled());
2905
2906 // Prepare displays.
2907 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002908 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2909 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002910 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2911 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2912 ASSERT_TRUE(mDevice->isEnabled());
2913
2914 // Device should be disabled after set disable.
2915 mFakePolicy->addDisabledDevice(mDevice->getId());
2916 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2917 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2918 ASSERT_FALSE(mDevice->isEnabled());
2919
2920 // Device should still be disabled even found the associated display.
2921 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2922 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2923 ASSERT_FALSE(mDevice->isEnabled());
2924}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002925
Christine Franks1ba71cc2021-04-07 14:37:42 -07002926TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2927 // Device should be enabled by default.
2928 mFakePolicy->clearViewports();
2929 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2930 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2931 ASSERT_TRUE(mDevice->isEnabled());
2932
2933 // Device should be disabled because it is associated with a specific display, but the
2934 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002935 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002936 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2937 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2938 ASSERT_FALSE(mDevice->isEnabled());
2939
2940 // Device should be enabled when a display is found.
2941 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2942 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2943 NO_PORT, ViewportType::INTERNAL);
2944 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2945 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2946 ASSERT_TRUE(mDevice->isEnabled());
2947
2948 // Device should be disabled after set disable.
2949 mFakePolicy->addDisabledDevice(mDevice->getId());
2950 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2951 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2952 ASSERT_FALSE(mDevice->isEnabled());
2953
2954 // Device should still be disabled even found the associated display.
2955 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2956 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2957 ASSERT_FALSE(mDevice->isEnabled());
2958}
2959
Christine Franks2a2293c2022-01-18 11:51:16 -08002960TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2961 mFakePolicy->clearViewports();
2962 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2963 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2964
Christine Franks2a2293c2022-01-18 11:51:16 -08002965 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) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004279 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4280 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4281 }
4282
4283 void prepareSecondaryDisplay() {
4284 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4285 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4286 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004287 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004288
4289 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4290 float pressure) {
4291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4292 0.0f, 0.0f, 0.0f, EPSILON));
4293 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294};
4295
4296const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4297
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004298void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4299 int32_t originalY, int32_t rotatedX,
4300 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 NotifyMotionArgs args;
4302
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004308 ASSERT_NO_FATAL_FAILURE(
4309 assertCursorPointerCoords(args.pointerCoords[0],
4310 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4311 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312}
4313
4314TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004316 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004318 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319}
4320
4321TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004323 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004325 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326}
4327
4328TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004330 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331
4332 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004333 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334
4335 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004336 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4337 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4339 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4340
4341 // When the bounds are set, then there should be a valid motion range.
4342 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4343
4344 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004345 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346
4347 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4348 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4349 1, 800 - 1, 0.0f, 0.0f));
4350 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4351 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4352 2, 480 - 1, 0.0f, 0.0f));
4353 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4354 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4355 0.0f, 1.0f, 0.0f, 0.0f));
4356}
4357
4358TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004360 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361
4362 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004363 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364
4365 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4366 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4367 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4368 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4369 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4370 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4371 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4372 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4373 0.0f, 1.0f, 0.0f, 0.0f));
4374}
4375
4376TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004378 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379
arthurhungdcef2dc2020-08-11 14:47:50 +08004380 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381
4382 NotifyMotionArgs args;
4383
4384 // Button press.
4385 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4389 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4390 ASSERT_EQ(DEVICE_ID, args.deviceId);
4391 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4392 ASSERT_EQ(uint32_t(0), args.policyFlags);
4393 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4394 ASSERT_EQ(0, args.flags);
4395 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4396 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4397 ASSERT_EQ(0, args.edgeFlags);
4398 ASSERT_EQ(uint32_t(1), args.pointerCount);
4399 ASSERT_EQ(0, args.pointerProperties[0].id);
4400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004401 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4403 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4404 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4405
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4407 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4408 ASSERT_EQ(DEVICE_ID, args.deviceId);
4409 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4410 ASSERT_EQ(uint32_t(0), args.policyFlags);
4411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4412 ASSERT_EQ(0, args.flags);
4413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4414 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4415 ASSERT_EQ(0, args.edgeFlags);
4416 ASSERT_EQ(uint32_t(1), args.pointerCount);
4417 ASSERT_EQ(0, args.pointerProperties[0].id);
4418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004419 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4421 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4422 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4423
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4426 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4428 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4429 ASSERT_EQ(DEVICE_ID, args.deviceId);
4430 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4431 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4433 ASSERT_EQ(0, args.flags);
4434 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4435 ASSERT_EQ(0, args.buttonState);
4436 ASSERT_EQ(0, args.edgeFlags);
4437 ASSERT_EQ(uint32_t(1), args.pointerCount);
4438 ASSERT_EQ(0, args.pointerProperties[0].id);
4439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004440 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4442 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4443 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4444
4445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4446 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4447 ASSERT_EQ(DEVICE_ID, args.deviceId);
4448 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4449 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4451 ASSERT_EQ(0, args.flags);
4452 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4453 ASSERT_EQ(0, args.buttonState);
4454 ASSERT_EQ(0, args.edgeFlags);
4455 ASSERT_EQ(uint32_t(1), args.pointerCount);
4456 ASSERT_EQ(0, args.pointerProperties[0].id);
4457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004458 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4460 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4461 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4462}
4463
4464TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004466 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467
4468 NotifyMotionArgs args;
4469
4470 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004475 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4476 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4477 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478
4479 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004484 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4485 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486}
4487
4488TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004490 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491
4492 NotifyMotionArgs args;
4493
4494 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4498 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004499 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4502 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004503 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004504
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004510 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004511
4512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004514 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515}
4516
4517TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004519 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520
4521 NotifyMotionArgs args;
4522
4523 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004530 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4531 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4532 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4535 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004536 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4537 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4538 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004539
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004546 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4547 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4548 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549
4550 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004554 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004555 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004556
4557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004559 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560}
4561
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004562TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004563 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004565 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4566 // need to be rotated.
4567 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004568 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004570 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4575 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4576 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4577 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4578 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4579}
4580
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004581TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004582 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004584 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4585 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004586 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004588 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004589 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4591 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4592 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4593 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4594 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4595 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4596 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4597 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4598
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004599 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004600 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004601 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4602 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4603 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4604 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4605 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4606 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4607 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4608 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004610 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004611 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004612 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4613 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4614 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4615 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4616 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4617 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4618 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4619 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4620
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004621 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004622 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004623 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4624 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4625 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4626 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4627 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4628 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4629 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4630 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631}
4632
4633TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004635 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636
4637 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4638 mFakePointerController->setPosition(100, 200);
4639 mFakePointerController->setButtonState(0);
4640
4641 NotifyMotionArgs motionArgs;
4642 NotifyKeyArgs keyArgs;
4643
4644 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4648 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4649 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4650 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004651 ASSERT_NO_FATAL_FAILURE(
4652 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4655 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4656 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4657 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004658 ASSERT_NO_FATAL_FAILURE(
4659 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004660
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004664 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 ASSERT_EQ(0, motionArgs.buttonState);
4666 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004667 ASSERT_NO_FATAL_FAILURE(
4668 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 ASSERT_EQ(0, motionArgs.buttonState);
4673 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004674 ASSERT_NO_FATAL_FAILURE(
4675 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004676
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679 ASSERT_EQ(0, motionArgs.buttonState);
4680 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004681 ASSERT_NO_FATAL_FAILURE(
4682 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683
4684 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4689 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4690 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4691 motionArgs.buttonState);
4692 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4693 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004694 ASSERT_NO_FATAL_FAILURE(
4695 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4698 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4699 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4700 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4701 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004702 ASSERT_NO_FATAL_FAILURE(
4703 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004704
4705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4707 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4708 motionArgs.buttonState);
4709 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4710 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004711 ASSERT_NO_FATAL_FAILURE(
4712 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004713
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4719 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004720 ASSERT_NO_FATAL_FAILURE(
4721 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004722
4723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004725 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4726 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004727 ASSERT_NO_FATAL_FAILURE(
4728 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004733 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4734 ASSERT_EQ(0, motionArgs.buttonState);
4735 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004736 ASSERT_NO_FATAL_FAILURE(
4737 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740
4741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 ASSERT_EQ(0, motionArgs.buttonState);
4743 ASSERT_EQ(0, mFakePointerController->getButtonState());
4744 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004745 ASSERT_NO_FATAL_FAILURE(
4746 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004747
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4749 ASSERT_EQ(0, motionArgs.buttonState);
4750 ASSERT_EQ(0, mFakePointerController->getButtonState());
4751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004752 ASSERT_NO_FATAL_FAILURE(
4753 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754
4755 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4759 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4760 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004761
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004763 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4765 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004766 ASSERT_NO_FATAL_FAILURE(
4767 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004768
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4771 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4772 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004773 ASSERT_NO_FATAL_FAILURE(
4774 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780 ASSERT_EQ(0, motionArgs.buttonState);
4781 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004782 ASSERT_NO_FATAL_FAILURE(
4783 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004784
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004787 ASSERT_EQ(0, motionArgs.buttonState);
4788 ASSERT_EQ(0, mFakePointerController->getButtonState());
4789
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004790 ASSERT_NO_FATAL_FAILURE(
4791 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4793 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4794 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4795
4796 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4800 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4801 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4806 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004807 ASSERT_NO_FATAL_FAILURE(
4808 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809
4810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4812 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4813 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004814 ASSERT_NO_FATAL_FAILURE(
4815 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 ASSERT_EQ(0, motionArgs.buttonState);
4822 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004823 ASSERT_NO_FATAL_FAILURE(
4824 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004825
4826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4827 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4828 ASSERT_EQ(0, motionArgs.buttonState);
4829 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004830 ASSERT_NO_FATAL_FAILURE(
4831 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004832
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4834 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4835 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4836
4837 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4841 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4842 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004843
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4847 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004848 ASSERT_NO_FATAL_FAILURE(
4849 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004850
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4853 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4854 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004855 ASSERT_NO_FATAL_FAILURE(
4856 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004861 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862 ASSERT_EQ(0, motionArgs.buttonState);
4863 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004864 ASSERT_NO_FATAL_FAILURE(
4865 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004866
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4868 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4869 ASSERT_EQ(0, motionArgs.buttonState);
4870 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004871 ASSERT_NO_FATAL_FAILURE(
4872 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004873
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4875 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4876 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4877
4878 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4882 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4883 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004884
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004886 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4888 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004889 ASSERT_NO_FATAL_FAILURE(
4890 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004891
4892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4894 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4895 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004896 ASSERT_NO_FATAL_FAILURE(
4897 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903 ASSERT_EQ(0, motionArgs.buttonState);
4904 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004905 ASSERT_NO_FATAL_FAILURE(
4906 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004907
4908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4909 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4910 ASSERT_EQ(0, motionArgs.buttonState);
4911 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004912 ASSERT_NO_FATAL_FAILURE(
4913 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004914
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4916 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4917 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4918}
4919
4920TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004922 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923
4924 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4925 mFakePointerController->setPosition(100, 200);
4926 mFakePointerController->setButtonState(0);
4927
4928 NotifyMotionArgs args;
4929
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004934 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4935 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4937 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 +01004938 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004939}
4940
4941TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004942 addConfigurationProperty("cursor.mode", "pointer");
4943 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004944 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004945
4946 NotifyDeviceResetArgs resetArgs;
4947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4948 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4949 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4950
4951 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4952 mFakePointerController->setPosition(100, 200);
4953 mFakePointerController->setButtonState(0);
4954
4955 NotifyMotionArgs args;
4956
4957 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
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_MOVE, args.action);
4964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4965 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 +01004966 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004967
4968 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4972 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4973 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4975 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4977 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4980 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4981
4982 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004983 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4984 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4986 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4989 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4991 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4992 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4994 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4995
4996 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5001 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5004 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 +01005005 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005006
5007 // Disable pointer capture and check that the device generation got bumped
5008 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005009 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005010 mFakePolicy->setPointerCapture(false);
5011 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005012 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005013
5014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005015 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5016
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5021 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5024 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 +01005025 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026}
5027
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005028/**
5029 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5030 * pointer acceleration or speed processing should not be applied.
5031 */
5032TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5033 addConfigurationProperty("cursor.mode", "pointer");
5034 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5035 100.f /*high threshold*/, 10.f /*acceleration*/);
5036 mFakePolicy->setVelocityControlParams(testParams);
5037 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5038
5039 NotifyDeviceResetArgs resetArgs;
5040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5041 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5042 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5043
5044 NotifyMotionArgs args;
5045
5046 // Move and verify scale is applied.
5047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5051 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5052 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5053 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5054 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5055 ASSERT_GT(relX, 10);
5056 ASSERT_GT(relY, 20);
5057
5058 // Enable Pointer Capture
5059 mFakePolicy->setPointerCapture(true);
5060 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5061 NotifyPointerCaptureChangedArgs captureArgs;
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5063 ASSERT_TRUE(captureArgs.request.enable);
5064
5065 // Move and verify scale is not applied.
5066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5070 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5072 ASSERT_EQ(10, args.pointerCoords[0].getX());
5073 ASSERT_EQ(20, args.pointerCoords[0].getY());
5074}
5075
Prabir Pradhan208360b2022-06-24 18:37:04 +00005076TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5077 addConfigurationProperty("cursor.mode", "pointer");
5078 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5079
5080 NotifyDeviceResetArgs resetArgs;
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5082 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5083 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5084
5085 // Ensure the display is rotated.
5086 prepareDisplay(DISPLAY_ORIENTATION_90);
5087
5088 NotifyMotionArgs args;
5089
5090 // Verify that the coordinates are rotated.
5091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5095 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5096 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5097 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5098 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5099
5100 // Enable Pointer Capture.
5101 mFakePolicy->setPointerCapture(true);
5102 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5103 NotifyPointerCaptureChangedArgs captureArgs;
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5105 ASSERT_TRUE(captureArgs.request.enable);
5106
5107 // Move and verify rotation is not applied.
5108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5112 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5114 ASSERT_EQ(10, args.pointerCoords[0].getX());
5115 ASSERT_EQ(20, args.pointerCoords[0].getY());
5116}
5117
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005118TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005119 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005120
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005121 // Set up the default display.
5122 prepareDisplay(DISPLAY_ORIENTATION_90);
5123
5124 // Set up the secondary display as the display on which the pointer should be shown.
5125 // The InputDevice is not associated with any display.
5126 prepareSecondaryDisplay();
5127 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005128 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5129
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005130 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005131 mFakePointerController->setPosition(100, 200);
5132 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005133
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005134 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005139 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5140 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5141 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005142 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005143}
5144
5145TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5146 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5147
5148 // Set up the default display.
5149 prepareDisplay(DISPLAY_ORIENTATION_90);
5150
5151 // Set up the secondary display as the display on which the pointer should be shown,
5152 // and associate the InputDevice with the secondary display.
5153 prepareSecondaryDisplay();
5154 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5155 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5156 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5157
5158 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5159 mFakePointerController->setPosition(100, 200);
5160 mFakePointerController->setButtonState(0);
5161
5162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005166 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5167 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5168 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005169 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5170}
5171
5172TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5173 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5174
5175 // Set up the default display as the display on which the pointer should be shown.
5176 prepareDisplay(DISPLAY_ORIENTATION_90);
5177 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5178
5179 // Associate the InputDevice with the secondary display.
5180 prepareSecondaryDisplay();
5181 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5182 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5183
5184 // The mapper should not generate any events because it is associated with a display that is
5185 // different from the pointer display.
5186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005190}
5191
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192// --- TouchInputMapperTest ---
5193
5194class TouchInputMapperTest : public InputMapperTest {
5195protected:
5196 static const int32_t RAW_X_MIN;
5197 static const int32_t RAW_X_MAX;
5198 static const int32_t RAW_Y_MIN;
5199 static const int32_t RAW_Y_MAX;
5200 static const int32_t RAW_TOUCH_MIN;
5201 static const int32_t RAW_TOUCH_MAX;
5202 static const int32_t RAW_TOOL_MIN;
5203 static const int32_t RAW_TOOL_MAX;
5204 static const int32_t RAW_PRESSURE_MIN;
5205 static const int32_t RAW_PRESSURE_MAX;
5206 static const int32_t RAW_ORIENTATION_MIN;
5207 static const int32_t RAW_ORIENTATION_MAX;
5208 static const int32_t RAW_DISTANCE_MIN;
5209 static const int32_t RAW_DISTANCE_MAX;
5210 static const int32_t RAW_TILT_MIN;
5211 static const int32_t RAW_TILT_MAX;
5212 static const int32_t RAW_ID_MIN;
5213 static const int32_t RAW_ID_MAX;
5214 static const int32_t RAW_SLOT_MIN;
5215 static const int32_t RAW_SLOT_MAX;
5216 static const float X_PRECISION;
5217 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005218 static const float X_PRECISION_VIRTUAL;
5219 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220
5221 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005222 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223
5224 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5225
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005226 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005227 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005228
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229 enum Axes {
5230 POSITION = 1 << 0,
5231 TOUCH = 1 << 1,
5232 TOOL = 1 << 2,
5233 PRESSURE = 1 << 3,
5234 ORIENTATION = 1 << 4,
5235 MINOR = 1 << 5,
5236 ID = 1 << 6,
5237 DISTANCE = 1 << 7,
5238 TILT = 1 << 8,
5239 SLOT = 1 << 9,
5240 TOOL_TYPE = 1 << 10,
5241 };
5242
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005243 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5244 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005245 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005247 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248 int32_t toRawX(float displayX);
5249 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005250 int32_t toRotatedRawX(float displayX);
5251 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005252 float toCookedX(float rawX, float rawY);
5253 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005255 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005257 float toDisplayY(int32_t rawY, int32_t displayHeight);
5258
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259};
5260
5261const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5262const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5263const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5264const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5265const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5266const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5267const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5268const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005269const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5270const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005271const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5272const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5273const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5274const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5275const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5276const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5277const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5278const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5279const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5280const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5281const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5282const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005283const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5284 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5285const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5286 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005287const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5288 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005289
5290const float TouchInputMapperTest::GEOMETRIC_SCALE =
5291 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5292 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5293
5294const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5295 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5296 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5297};
5298
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005299void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005300 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5301 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005302}
5303
5304void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5305 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5306 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005307}
5308
Santos Cordonfa5cf462017-04-05 10:37:00 -07005309void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005310 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5311 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5312 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005313}
5314
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005316 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5317 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5318 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5319 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320}
5321
Jason Gerecke489fda82012-09-07 17:19:40 -07005322void TouchInputMapperTest::prepareLocationCalibration() {
5323 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5324}
5325
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326int32_t TouchInputMapperTest::toRawX(float displayX) {
5327 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5328}
5329
5330int32_t TouchInputMapperTest::toRawY(float displayY) {
5331 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5332}
5333
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005334int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5335 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5336}
5337
5338int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5339 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5340}
5341
Jason Gerecke489fda82012-09-07 17:19:40 -07005342float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5343 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5344 return rawX;
5345}
5346
5347float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5348 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5349 return rawY;
5350}
5351
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005353 return toDisplayX(rawX, DISPLAY_WIDTH);
5354}
5355
5356float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5357 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358}
5359
5360float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005361 return toDisplayY(rawY, DISPLAY_HEIGHT);
5362}
5363
5364float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5365 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366}
5367
5368
5369// --- SingleTouchInputMapperTest ---
5370
5371class SingleTouchInputMapperTest : public TouchInputMapperTest {
5372protected:
5373 void prepareButtons();
5374 void prepareAxes(int axes);
5375
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005376 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5377 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5378 void processUp(SingleTouchInputMapper& mappery);
5379 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5380 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5381 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5382 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5383 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5384 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385};
5386
5387void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005388 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389}
5390
5391void SingleTouchInputMapperTest::prepareAxes(int axes) {
5392 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005393 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5394 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395 }
5396 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005397 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5398 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399 }
5400 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005401 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5402 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403 }
5404 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005405 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5406 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407 }
5408 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005409 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5410 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 }
5412}
5413
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005414void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418}
5419
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005420void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423}
5424
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005425void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005427}
5428
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005429void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431}
5432
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005433void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5434 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436}
5437
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005438void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440}
5441
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005442void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5443 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446}
5447
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005448void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5449 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451}
5452
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005453void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455}
5456
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458 prepareButtons();
5459 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005460 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005462 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005463}
5464
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466 prepareButtons();
5467 prepareAxes(POSITION);
5468 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005469 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005471 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472}
5473
5474TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475 addConfigurationProperty("touch.deviceType", "touchScreen");
5476 prepareDisplay(DISPLAY_ORIENTATION_0);
5477 prepareButtons();
5478 prepareAxes(POSITION);
5479 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005480 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481
5482 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005483 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484
5485 // Virtual key is down.
5486 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5487 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5488 processDown(mapper, x, y);
5489 processSync(mapper);
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493
5494 // Virtual key is up.
5495 processUp(mapper);
5496 processSync(mapper);
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005499 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500}
5501
5502TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503 addConfigurationProperty("touch.deviceType", "touchScreen");
5504 prepareDisplay(DISPLAY_ORIENTATION_0);
5505 prepareButtons();
5506 prepareAxes(POSITION);
5507 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005508 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005509
5510 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005511 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512
5513 // Virtual key is down.
5514 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5515 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5516 processDown(mapper, x, y);
5517 processSync(mapper);
5518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5519
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005520 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005521
5522 // Virtual key is up.
5523 processUp(mapper);
5524 processSync(mapper);
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5526
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005527 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528}
5529
5530TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531 addConfigurationProperty("touch.deviceType", "touchScreen");
5532 prepareDisplay(DISPLAY_ORIENTATION_0);
5533 prepareButtons();
5534 prepareAxes(POSITION);
5535 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005536 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537
Michael Wrightd02c5b62014-02-10 15:10:22 -08005538 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005539 ASSERT_TRUE(
5540 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005541 ASSERT_TRUE(flags[0]);
5542 ASSERT_FALSE(flags[1]);
5543}
5544
5545TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546 addConfigurationProperty("touch.deviceType", "touchScreen");
5547 prepareDisplay(DISPLAY_ORIENTATION_0);
5548 prepareButtons();
5549 prepareAxes(POSITION);
5550 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005551 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552
arthurhungdcef2dc2020-08-11 14:47:50 +08005553 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554
5555 NotifyKeyArgs args;
5556
5557 // Press virtual key.
5558 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5559 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5560 processDown(mapper, x, y);
5561 processSync(mapper);
5562
5563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5564 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5565 ASSERT_EQ(DEVICE_ID, args.deviceId);
5566 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5567 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5568 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5569 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5570 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5571 ASSERT_EQ(KEY_HOME, args.scanCode);
5572 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5573 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5574
5575 // Release virtual key.
5576 processUp(mapper);
5577 processSync(mapper);
5578
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5580 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5581 ASSERT_EQ(DEVICE_ID, args.deviceId);
5582 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5583 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5584 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5585 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5586 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5587 ASSERT_EQ(KEY_HOME, args.scanCode);
5588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5589 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5590
5591 // Should not have sent any motions.
5592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5593}
5594
5595TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596 addConfigurationProperty("touch.deviceType", "touchScreen");
5597 prepareDisplay(DISPLAY_ORIENTATION_0);
5598 prepareButtons();
5599 prepareAxes(POSITION);
5600 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005601 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602
arthurhungdcef2dc2020-08-11 14:47:50 +08005603 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005604
5605 NotifyKeyArgs keyArgs;
5606
5607 // Press virtual key.
5608 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5609 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5610 processDown(mapper, x, y);
5611 processSync(mapper);
5612
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5614 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5615 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5616 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5617 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5618 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5619 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5620 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5621 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5622 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5623 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5624
5625 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5626 // into the display area.
5627 y -= 100;
5628 processMove(mapper, x, y);
5629 processSync(mapper);
5630
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5632 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5633 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5634 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5635 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5636 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5637 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5638 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5639 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5640 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5641 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5642 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5643
5644 NotifyMotionArgs motionArgs;
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5646 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5647 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5648 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5649 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5650 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5651 ASSERT_EQ(0, motionArgs.flags);
5652 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5653 ASSERT_EQ(0, motionArgs.buttonState);
5654 ASSERT_EQ(0, motionArgs.edgeFlags);
5655 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5656 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5659 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5660 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5661 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5662 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5663
5664 // Keep moving out of bounds. Should generate a pointer move.
5665 y -= 50;
5666 processMove(mapper, x, y);
5667 processSync(mapper);
5668
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5670 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5671 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5672 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5673 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5675 ASSERT_EQ(0, motionArgs.flags);
5676 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5677 ASSERT_EQ(0, motionArgs.buttonState);
5678 ASSERT_EQ(0, motionArgs.edgeFlags);
5679 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5680 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5681 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5683 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5684 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5685 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5686 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5687
5688 // Release out of bounds. Should generate a pointer up.
5689 processUp(mapper);
5690 processSync(mapper);
5691
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5693 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5694 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5695 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5696 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5697 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5698 ASSERT_EQ(0, motionArgs.flags);
5699 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5700 ASSERT_EQ(0, motionArgs.buttonState);
5701 ASSERT_EQ(0, motionArgs.edgeFlags);
5702 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5703 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5706 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5707 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5708 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5709 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5710
5711 // Should not have sent any more keys or motions.
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5714}
5715
5716TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717 addConfigurationProperty("touch.deviceType", "touchScreen");
5718 prepareDisplay(DISPLAY_ORIENTATION_0);
5719 prepareButtons();
5720 prepareAxes(POSITION);
5721 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005722 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005723
arthurhungdcef2dc2020-08-11 14:47:50 +08005724 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725
5726 NotifyMotionArgs motionArgs;
5727
5728 // Initially go down out of bounds.
5729 int32_t x = -10;
5730 int32_t y = -10;
5731 processDown(mapper, x, y);
5732 processSync(mapper);
5733
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5735
5736 // Move into the display area. Should generate a pointer down.
5737 x = 50;
5738 y = 75;
5739 processMove(mapper, x, y);
5740 processSync(mapper);
5741
5742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5744 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5745 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5746 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5747 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5748 ASSERT_EQ(0, motionArgs.flags);
5749 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5750 ASSERT_EQ(0, motionArgs.buttonState);
5751 ASSERT_EQ(0, motionArgs.edgeFlags);
5752 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5756 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5757 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5758 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5759 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5760
5761 // Release. Should generate a pointer up.
5762 processUp(mapper);
5763 processSync(mapper);
5764
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5766 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5767 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5768 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5769 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5770 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5771 ASSERT_EQ(0, motionArgs.flags);
5772 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5773 ASSERT_EQ(0, motionArgs.buttonState);
5774 ASSERT_EQ(0, motionArgs.edgeFlags);
5775 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5776 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5779 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5780 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5781 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5783
5784 // Should not have sent any more keys or motions.
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5787}
5788
Santos Cordonfa5cf462017-04-05 10:37:00 -07005789TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005790 addConfigurationProperty("touch.deviceType", "touchScreen");
5791 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5792
5793 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5794 prepareButtons();
5795 prepareAxes(POSITION);
5796 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005797 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005798
arthurhungdcef2dc2020-08-11 14:47:50 +08005799 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005800
5801 NotifyMotionArgs motionArgs;
5802
5803 // Down.
5804 int32_t x = 100;
5805 int32_t y = 125;
5806 processDown(mapper, x, y);
5807 processSync(mapper);
5808
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5810 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5811 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5812 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5813 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5814 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5815 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5816 ASSERT_EQ(0, motionArgs.flags);
5817 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5818 ASSERT_EQ(0, motionArgs.buttonState);
5819 ASSERT_EQ(0, motionArgs.edgeFlags);
5820 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5821 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5824 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5825 1, 0, 0, 0, 0, 0, 0, 0));
5826 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5827 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5828 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5829
5830 // Move.
5831 x += 50;
5832 y += 75;
5833 processMove(mapper, x, y);
5834 processSync(mapper);
5835
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5838 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5839 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5840 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5841 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5843 ASSERT_EQ(0, motionArgs.flags);
5844 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5845 ASSERT_EQ(0, motionArgs.buttonState);
5846 ASSERT_EQ(0, motionArgs.edgeFlags);
5847 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5848 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5851 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5852 1, 0, 0, 0, 0, 0, 0, 0));
5853 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5854 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5855 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5856
5857 // Up.
5858 processUp(mapper);
5859 processSync(mapper);
5860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5863 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5864 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5865 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5866 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5867 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5868 ASSERT_EQ(0, motionArgs.flags);
5869 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5870 ASSERT_EQ(0, motionArgs.buttonState);
5871 ASSERT_EQ(0, motionArgs.edgeFlags);
5872 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5873 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5876 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5877 1, 0, 0, 0, 0, 0, 0, 0));
5878 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5879 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5880 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5881
5882 // Should not have sent any more keys or motions.
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5885}
5886
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 addConfigurationProperty("touch.deviceType", "touchScreen");
5889 prepareDisplay(DISPLAY_ORIENTATION_0);
5890 prepareButtons();
5891 prepareAxes(POSITION);
5892 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005893 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894
arthurhungdcef2dc2020-08-11 14:47:50 +08005895 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896
5897 NotifyMotionArgs motionArgs;
5898
5899 // Down.
5900 int32_t x = 100;
5901 int32_t y = 125;
5902 processDown(mapper, x, y);
5903 processSync(mapper);
5904
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5906 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5907 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5908 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5909 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5911 ASSERT_EQ(0, motionArgs.flags);
5912 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5913 ASSERT_EQ(0, motionArgs.buttonState);
5914 ASSERT_EQ(0, motionArgs.edgeFlags);
5915 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5916 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5919 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5920 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5921 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5922 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5923
5924 // Move.
5925 x += 50;
5926 y += 75;
5927 processMove(mapper, x, y);
5928 processSync(mapper);
5929
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5932 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5933 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5934 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5936 ASSERT_EQ(0, motionArgs.flags);
5937 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5938 ASSERT_EQ(0, motionArgs.buttonState);
5939 ASSERT_EQ(0, motionArgs.edgeFlags);
5940 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5941 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5942 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5944 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5945 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5946 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5947 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5948
5949 // Up.
5950 processUp(mapper);
5951 processSync(mapper);
5952
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5954 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5955 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5956 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5957 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5959 ASSERT_EQ(0, motionArgs.flags);
5960 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5961 ASSERT_EQ(0, motionArgs.buttonState);
5962 ASSERT_EQ(0, motionArgs.edgeFlags);
5963 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5964 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5967 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5968 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5969 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5970 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5971
5972 // Should not have sent any more keys or motions.
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5975}
5976
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005977TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005978 addConfigurationProperty("touch.deviceType", "touchScreen");
5979 prepareButtons();
5980 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005981 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5982 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005983 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984
5985 NotifyMotionArgs args;
5986
5987 // Rotation 90.
5988 prepareDisplay(DISPLAY_ORIENTATION_90);
5989 processDown(mapper, toRawX(50), toRawY(75));
5990 processSync(mapper);
5991
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5993 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5994 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5995
5996 processUp(mapper);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5999}
6000
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006001TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 addConfigurationProperty("touch.deviceType", "touchScreen");
6003 prepareButtons();
6004 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006005 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6006 // orientation-aware are affected by display rotation.
6007 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006008 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009
6010 NotifyMotionArgs args;
6011
6012 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006013 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014 prepareDisplay(DISPLAY_ORIENTATION_0);
6015 processDown(mapper, toRawX(50), toRawY(75));
6016 processSync(mapper);
6017
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6019 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6020 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6021
6022 processUp(mapper);
6023 processSync(mapper);
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6025
6026 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006027 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006029 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 processSync(mapper);
6031
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6033 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6034 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6035
6036 processUp(mapper);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6039
6040 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006041 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042 prepareDisplay(DISPLAY_ORIENTATION_180);
6043 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6044 processSync(mapper);
6045
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6047 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6048 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6049
6050 processUp(mapper);
6051 processSync(mapper);
6052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6053
6054 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006055 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006057 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058 processSync(mapper);
6059
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6061 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6062 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6063
6064 processUp(mapper);
6065 processSync(mapper);
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6067}
6068
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006069TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6070 addConfigurationProperty("touch.deviceType", "touchScreen");
6071 prepareButtons();
6072 prepareAxes(POSITION);
6073 addConfigurationProperty("touch.orientationAware", "1");
6074 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6075 clearViewports();
6076 prepareDisplay(DISPLAY_ORIENTATION_0);
6077 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6078 NotifyMotionArgs args;
6079
6080 // Orientation 0.
6081 processDown(mapper, toRawX(50), toRawY(75));
6082 processSync(mapper);
6083
6084 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6085 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6086 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6087
6088 processUp(mapper);
6089 processSync(mapper);
6090 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6091}
6092
6093TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6094 addConfigurationProperty("touch.deviceType", "touchScreen");
6095 prepareButtons();
6096 prepareAxes(POSITION);
6097 addConfigurationProperty("touch.orientationAware", "1");
6098 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6099 clearViewports();
6100 prepareDisplay(DISPLAY_ORIENTATION_0);
6101 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6102 NotifyMotionArgs args;
6103
6104 // Orientation 90.
6105 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6106 processSync(mapper);
6107
6108 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6109 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6110 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6111
6112 processUp(mapper);
6113 processSync(mapper);
6114 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6115}
6116
6117TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6118 addConfigurationProperty("touch.deviceType", "touchScreen");
6119 prepareButtons();
6120 prepareAxes(POSITION);
6121 addConfigurationProperty("touch.orientationAware", "1");
6122 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6123 clearViewports();
6124 prepareDisplay(DISPLAY_ORIENTATION_0);
6125 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6126 NotifyMotionArgs args;
6127
6128 // Orientation 180.
6129 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6130 processSync(mapper);
6131
6132 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6133 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6134 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6135
6136 processUp(mapper);
6137 processSync(mapper);
6138 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6139}
6140
6141TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6142 addConfigurationProperty("touch.deviceType", "touchScreen");
6143 prepareButtons();
6144 prepareAxes(POSITION);
6145 addConfigurationProperty("touch.orientationAware", "1");
6146 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6147 clearViewports();
6148 prepareDisplay(DISPLAY_ORIENTATION_0);
6149 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6150 NotifyMotionArgs args;
6151
6152 // Orientation 270.
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
6165TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6166 addConfigurationProperty("touch.deviceType", "touchScreen");
6167 prepareButtons();
6168 prepareAxes(POSITION);
6169 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6170 // orientation-aware are affected by display rotation.
6171 addConfigurationProperty("touch.orientationAware", "0");
6172 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6173 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6174
6175 NotifyMotionArgs args;
6176
6177 // Orientation 90, Rotation 0.
6178 clearViewports();
6179 prepareDisplay(DISPLAY_ORIENTATION_0);
6180 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6181 processSync(mapper);
6182
6183 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6184 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6185 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6186
6187 processUp(mapper);
6188 processSync(mapper);
6189 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6190
6191 // Orientation 90, Rotation 90.
6192 clearViewports();
6193 prepareDisplay(DISPLAY_ORIENTATION_90);
6194 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6195 processSync(mapper);
6196
6197 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6198 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6199 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6200
6201 processUp(mapper);
6202 processSync(mapper);
6203 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6204
6205 // Orientation 90, Rotation 180.
6206 clearViewports();
6207 prepareDisplay(DISPLAY_ORIENTATION_180);
6208 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6209 processSync(mapper);
6210
6211 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6212 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6213 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6214
6215 processUp(mapper);
6216 processSync(mapper);
6217 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6218
6219 // Orientation 90, Rotation 270.
6220 clearViewports();
6221 prepareDisplay(DISPLAY_ORIENTATION_270);
6222 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6223 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6224 processSync(mapper);
6225
6226 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6227 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6228 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6229
6230 processUp(mapper);
6231 processSync(mapper);
6232 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6233}
6234
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 addConfigurationProperty("touch.deviceType", "touchScreen");
6237 prepareDisplay(DISPLAY_ORIENTATION_0);
6238 prepareButtons();
6239 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006240 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241
6242 // These calculations are based on the input device calibration documentation.
6243 int32_t rawX = 100;
6244 int32_t rawY = 200;
6245 int32_t rawPressure = 10;
6246 int32_t rawToolMajor = 12;
6247 int32_t rawDistance = 2;
6248 int32_t rawTiltX = 30;
6249 int32_t rawTiltY = 110;
6250
6251 float x = toDisplayX(rawX);
6252 float y = toDisplayY(rawY);
6253 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6254 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6255 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6256 float distance = float(rawDistance);
6257
6258 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6259 float tiltScale = M_PI / 180;
6260 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6261 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6262 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6263 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6264
6265 processDown(mapper, rawX, rawY);
6266 processPressure(mapper, rawPressure);
6267 processToolMajor(mapper, rawToolMajor);
6268 processDistance(mapper, rawDistance);
6269 processTilt(mapper, rawTiltX, rawTiltY);
6270 processSync(mapper);
6271
6272 NotifyMotionArgs args;
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6275 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6276 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6277}
6278
Jason Gerecke489fda82012-09-07 17:19:40 -07006279TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006280 addConfigurationProperty("touch.deviceType", "touchScreen");
6281 prepareDisplay(DISPLAY_ORIENTATION_0);
6282 prepareLocationCalibration();
6283 prepareButtons();
6284 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006285 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006286
6287 int32_t rawX = 100;
6288 int32_t rawY = 200;
6289
6290 float x = toDisplayX(toCookedX(rawX, rawY));
6291 float y = toDisplayY(toCookedY(rawX, rawY));
6292
6293 processDown(mapper, rawX, rawY);
6294 processSync(mapper);
6295
6296 NotifyMotionArgs args;
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6299 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6300}
6301
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 addConfigurationProperty("touch.deviceType", "touchScreen");
6304 prepareDisplay(DISPLAY_ORIENTATION_0);
6305 prepareButtons();
6306 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006307 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308
6309 NotifyMotionArgs motionArgs;
6310 NotifyKeyArgs keyArgs;
6311
6312 processDown(mapper, 100, 200);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6315 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6316 ASSERT_EQ(0, motionArgs.buttonState);
6317
6318 // press BTN_LEFT, release BTN_LEFT
6319 processKey(mapper, BTN_LEFT, 1);
6320 processSync(mapper);
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6323 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6324
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6327 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6328
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 processKey(mapper, BTN_LEFT, 0);
6330 processSync(mapper);
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006334
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6340 processKey(mapper, BTN_RIGHT, 1);
6341 processKey(mapper, BTN_MIDDLE, 1);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6345 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6346 motionArgs.buttonState);
6347
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6349 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6350 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6351
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6354 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6355 motionArgs.buttonState);
6356
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357 processKey(mapper, BTN_RIGHT, 0);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006365 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366
6367 processKey(mapper, BTN_MIDDLE, 0);
6368 processSync(mapper);
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006372
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376
6377 // press BTN_BACK, release BTN_BACK
6378 processKey(mapper, BTN_BACK, 1);
6379 processSync(mapper);
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6381 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6382 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006383
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006386 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6387
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6390 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391
6392 processKey(mapper, BTN_BACK, 0);
6393 processSync(mapper);
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400 ASSERT_EQ(0, motionArgs.buttonState);
6401
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6403 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6404 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6405
6406 // press BTN_SIDE, release BTN_SIDE
6407 processKey(mapper, BTN_SIDE, 1);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6410 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6411 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006412
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6416
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6419 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006420
6421 processKey(mapper, BTN_SIDE, 0);
6422 processSync(mapper);
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006424 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006426
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429 ASSERT_EQ(0, motionArgs.buttonState);
6430
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6432 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6433 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6434
6435 // press BTN_FORWARD, release BTN_FORWARD
6436 processKey(mapper, BTN_FORWARD, 1);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6439 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6440 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006441
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006444 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6445
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6448 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006449
6450 processKey(mapper, BTN_FORWARD, 0);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006453 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006455
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006458 ASSERT_EQ(0, motionArgs.buttonState);
6459
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6461 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6462 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6463
6464 // press BTN_EXTRA, release BTN_EXTRA
6465 processKey(mapper, BTN_EXTRA, 1);
6466 processSync(mapper);
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6468 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6469 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006470
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006473 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6474
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6477 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478
6479 processKey(mapper, BTN_EXTRA, 0);
6480 processSync(mapper);
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006482 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006484
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006487 ASSERT_EQ(0, motionArgs.buttonState);
6488
Michael Wrightd02c5b62014-02-10 15:10:22 -08006489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6490 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6491 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6492
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6494
Michael Wrightd02c5b62014-02-10 15:10:22 -08006495 // press BTN_STYLUS, release BTN_STYLUS
6496 processKey(mapper, BTN_STYLUS, 1);
6497 processSync(mapper);
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006500 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6501
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6504 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006505
6506 processKey(mapper, BTN_STYLUS, 0);
6507 processSync(mapper);
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006510 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006511
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006514 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006515
6516 // press BTN_STYLUS2, release BTN_STYLUS2
6517 processKey(mapper, BTN_STYLUS2, 1);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006521 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6522
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6525 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006526
6527 processKey(mapper, BTN_STYLUS2, 0);
6528 processSync(mapper);
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006531 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006532
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006535 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006536
6537 // release touch
6538 processUp(mapper);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6542 ASSERT_EQ(0, motionArgs.buttonState);
6543}
6544
6545TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006546 addConfigurationProperty("touch.deviceType", "touchScreen");
6547 prepareDisplay(DISPLAY_ORIENTATION_0);
6548 prepareButtons();
6549 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006550 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006551
6552 NotifyMotionArgs motionArgs;
6553
6554 // default tool type is finger
6555 processDown(mapper, 100, 200);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6560
6561 // eraser
6562 processKey(mapper, BTN_TOOL_RUBBER, 1);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6567
6568 // stylus
6569 processKey(mapper, BTN_TOOL_RUBBER, 0);
6570 processKey(mapper, BTN_TOOL_PEN, 1);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6575
6576 // brush
6577 processKey(mapper, BTN_TOOL_PEN, 0);
6578 processKey(mapper, BTN_TOOL_BRUSH, 1);
6579 processSync(mapper);
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6583
6584 // pencil
6585 processKey(mapper, BTN_TOOL_BRUSH, 0);
6586 processKey(mapper, BTN_TOOL_PENCIL, 1);
6587 processSync(mapper);
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6591
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006592 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006593 processKey(mapper, BTN_TOOL_PENCIL, 0);
6594 processKey(mapper, BTN_TOOL_AIRBRUSH, 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 // mouse
6601 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6602 processKey(mapper, BTN_TOOL_MOUSE, 1);
6603 processSync(mapper);
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6607
6608 // lens
6609 processKey(mapper, BTN_TOOL_MOUSE, 0);
6610 processKey(mapper, BTN_TOOL_LENS, 1);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6615
6616 // double-tap
6617 processKey(mapper, BTN_TOOL_LENS, 0);
6618 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
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 // triple-tap
6625 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6626 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6631
6632 // quad-tap
6633 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6634 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6635 processSync(mapper);
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6639
6640 // finger
6641 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6642 processKey(mapper, BTN_TOOL_FINGER, 1);
6643 processSync(mapper);
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6647
6648 // stylus trumps finger
6649 processKey(mapper, BTN_TOOL_PEN, 1);
6650 processSync(mapper);
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6654
6655 // eraser trumps stylus
6656 processKey(mapper, BTN_TOOL_RUBBER, 1);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6661
6662 // mouse trumps eraser
6663 processKey(mapper, BTN_TOOL_MOUSE, 1);
6664 processSync(mapper);
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6668
6669 // back to default tool type
6670 processKey(mapper, BTN_TOOL_MOUSE, 0);
6671 processKey(mapper, BTN_TOOL_RUBBER, 0);
6672 processKey(mapper, BTN_TOOL_PEN, 0);
6673 processKey(mapper, BTN_TOOL_FINGER, 0);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6678}
6679
6680TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006681 addConfigurationProperty("touch.deviceType", "touchScreen");
6682 prepareDisplay(DISPLAY_ORIENTATION_0);
6683 prepareButtons();
6684 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006685 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006686 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006687
6688 NotifyMotionArgs motionArgs;
6689
6690 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6691 processKey(mapper, BTN_TOOL_FINGER, 1);
6692 processMove(mapper, 100, 200);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6698
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6702 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6703
6704 // move a little
6705 processMove(mapper, 150, 250);
6706 processSync(mapper);
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6710 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6711
6712 // down when BTN_TOUCH is pressed, pressure defaults to 1
6713 processKey(mapper, BTN_TOUCH, 1);
6714 processSync(mapper);
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6718 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6719
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6721 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6723 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6724
6725 // up when BTN_TOUCH is released, hover restored
6726 processKey(mapper, BTN_TOUCH, 0);
6727 processSync(mapper);
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6729 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6731 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6732
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6736 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6737
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6742
6743 // exit hover when pointer goes away
6744 processKey(mapper, BTN_TOOL_FINGER, 0);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6749 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6750}
6751
6752TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006753 addConfigurationProperty("touch.deviceType", "touchScreen");
6754 prepareDisplay(DISPLAY_ORIENTATION_0);
6755 prepareButtons();
6756 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006757 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006758
6759 NotifyMotionArgs motionArgs;
6760
6761 // initially hovering because pressure is 0
6762 processDown(mapper, 100, 200);
6763 processPressure(mapper, 0);
6764 processSync(mapper);
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6768 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6769
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6771 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6773 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6774
6775 // move a little
6776 processMove(mapper, 150, 250);
6777 processSync(mapper);
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6779 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6781 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6782
6783 // down when pressure is non-zero
6784 processPressure(mapper, RAW_PRESSURE_MAX);
6785 processSync(mapper);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6789 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6790
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6794 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6795
6796 // up when pressure becomes 0, hover restored
6797 processPressure(mapper, 0);
6798 processSync(mapper);
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6802 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6803
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6805 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6807 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6808
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6810 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6812 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6813
6814 // exit hover when pointer goes away
6815 processUp(mapper);
6816 processSync(mapper);
6817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6818 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6820 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6821}
6822
lilinnan687e58f2022-07-19 16:00:50 +08006823TEST_F(SingleTouchInputMapperTest,
6824 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6825 addConfigurationProperty("touch.deviceType", "touchScreen");
6826 prepareDisplay(DISPLAY_ORIENTATION_0);
6827 prepareButtons();
6828 prepareAxes(POSITION);
6829 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6830 NotifyMotionArgs motionArgs;
6831
6832 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006833 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006834 processSync(mapper);
6835
6836 // We should receive a down event
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6838 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6839
6840 // Change display id
6841 clearViewports();
6842 prepareSecondaryDisplay(ViewportType::INTERNAL);
6843
6844 // We should receive a cancel event
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6846 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6847 // Then receive reset called
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6849}
6850
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006851TEST_F(SingleTouchInputMapperTest,
6852 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6853 addConfigurationProperty("touch.deviceType", "touchScreen");
6854 prepareDisplay(DISPLAY_ORIENTATION_0);
6855 prepareButtons();
6856 prepareAxes(POSITION);
6857 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6859 NotifyMotionArgs motionArgs;
6860
6861 // Start a new gesture.
6862 processDown(mapper, 100, 200);
6863 processSync(mapper);
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6865 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6866
6867 // Make the viewport inactive. This will put the device in disabled mode.
6868 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6869 viewport->isActive = false;
6870 mFakePolicy->updateViewport(*viewport);
6871 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6872
6873 // We should receive a cancel event for the ongoing gesture.
6874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6875 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6876 // Then we should be notified that the device was reset.
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6878
6879 // No events are generated while the viewport is inactive.
6880 processMove(mapper, 101, 201);
6881 processSync(mapper);
6882 processDown(mapper, 102, 202);
6883 processSync(mapper);
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6885
6886 // Make the viewport active again. The device should resume processing events.
6887 viewport->isActive = true;
6888 mFakePolicy->updateViewport(*viewport);
6889 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6890
6891 // The device is reset because it changes back to direct mode, without generating any events.
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6894
6895 // Start a new gesture.
6896 processDown(mapper, 100, 200);
6897 processSync(mapper);
6898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6899 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6900
6901 // No more events.
6902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6904}
6905
Prabir Pradhan5632d622021-09-06 07:57:20 -07006906// --- TouchDisplayProjectionTest ---
6907
6908class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6909public:
6910 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6911 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6912 // rotated equivalent of the given un-rotated physical display bounds.
6913 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6914 uint32_t inverseRotationFlags;
6915 auto width = DISPLAY_WIDTH;
6916 auto height = DISPLAY_HEIGHT;
6917 switch (orientation) {
6918 case DISPLAY_ORIENTATION_90:
6919 inverseRotationFlags = ui::Transform::ROT_270;
6920 std::swap(width, height);
6921 break;
6922 case DISPLAY_ORIENTATION_180:
6923 inverseRotationFlags = ui::Transform::ROT_180;
6924 break;
6925 case DISPLAY_ORIENTATION_270:
6926 inverseRotationFlags = ui::Transform::ROT_90;
6927 std::swap(width, height);
6928 break;
6929 case DISPLAY_ORIENTATION_0:
6930 inverseRotationFlags = ui::Transform::ROT_0;
6931 break;
6932 default:
6933 FAIL() << "Invalid orientation: " << orientation;
6934 }
6935
6936 const ui::Transform rotation(inverseRotationFlags, width, height);
6937 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6938
6939 std::optional<DisplayViewport> internalViewport =
6940 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6941 DisplayViewport& v = *internalViewport;
6942 v.displayId = DISPLAY_ID;
6943 v.orientation = orientation;
6944
6945 v.logicalLeft = 0;
6946 v.logicalTop = 0;
6947 v.logicalRight = 100;
6948 v.logicalBottom = 100;
6949
6950 v.physicalLeft = rotatedPhysicalDisplay.left;
6951 v.physicalTop = rotatedPhysicalDisplay.top;
6952 v.physicalRight = rotatedPhysicalDisplay.right;
6953 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6954
6955 v.deviceWidth = width;
6956 v.deviceHeight = height;
6957
6958 v.isActive = true;
6959 v.uniqueId = UNIQUE_ID;
6960 v.type = ViewportType::INTERNAL;
6961 mFakePolicy->updateViewport(v);
6962 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6963 }
6964
6965 void assertReceivedMove(const Point& point) {
6966 NotifyMotionArgs motionArgs;
6967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6969 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6971 1, 0, 0, 0, 0, 0, 0, 0));
6972 }
6973};
6974
6975TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6976 addConfigurationProperty("touch.deviceType", "touchScreen");
6977 prepareDisplay(DISPLAY_ORIENTATION_0);
6978
6979 prepareButtons();
6980 prepareAxes(POSITION);
6981 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6982
6983 NotifyMotionArgs motionArgs;
6984
6985 // Configure the DisplayViewport such that the logical display maps to a subsection of
6986 // the display panel called the physical display. Here, the physical display is bounded by the
6987 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6988 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6989 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6990 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6991
6992 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6993 DISPLAY_ORIENTATION_270}) {
6994 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6995
6996 // Touches outside the physical display should be ignored, and should not generate any
6997 // events. Ensure touches at the following points that lie outside of the physical display
6998 // area do not generate any events.
6999 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7000 processDown(mapper, toRawX(point.x), toRawY(point.y));
7001 processSync(mapper);
7002 processUp(mapper);
7003 processSync(mapper);
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7005 << "Unexpected event generated for touch outside physical display at point: "
7006 << point.x << ", " << point.y;
7007 }
7008 }
7009}
7010
7011TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7012 addConfigurationProperty("touch.deviceType", "touchScreen");
7013 prepareDisplay(DISPLAY_ORIENTATION_0);
7014
7015 prepareButtons();
7016 prepareAxes(POSITION);
7017 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7018
7019 NotifyMotionArgs motionArgs;
7020
7021 // Configure the DisplayViewport such that the logical display maps to a subsection of
7022 // the display panel called the physical display. Here, the physical display is bounded by the
7023 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7024 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7025
7026 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7027 DISPLAY_ORIENTATION_270}) {
7028 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7029
7030 // Touches that start outside the physical display should be ignored until it enters the
7031 // physical display bounds, at which point it should generate a down event. Start a touch at
7032 // the point (5, 100), which is outside the physical display bounds.
7033 static const Point kOutsidePoint{5, 100};
7034 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7035 processSync(mapper);
7036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7037
7038 // Move the touch into the physical display area. This should generate a pointer down.
7039 processMove(mapper, toRawX(11), toRawY(21));
7040 processSync(mapper);
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7042 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7043 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7044 ASSERT_NO_FATAL_FAILURE(
7045 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7046
7047 // Move the touch inside the physical display area. This should generate a pointer move.
7048 processMove(mapper, toRawX(69), toRawY(159));
7049 processSync(mapper);
7050 assertReceivedMove({69, 159});
7051
7052 // Move outside the physical display area. Since the pointer is already down, this should
7053 // now continue generating events.
7054 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7055 processSync(mapper);
7056 assertReceivedMove(kOutsidePoint);
7057
7058 // Release. This should generate a pointer up.
7059 processUp(mapper);
7060 processSync(mapper);
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7062 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7064 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7065
7066 // Ensure no more events were generated.
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7069 }
7070}
7071
Michael Wrightd02c5b62014-02-10 15:10:22 -08007072// --- MultiTouchInputMapperTest ---
7073
7074class MultiTouchInputMapperTest : public TouchInputMapperTest {
7075protected:
7076 void prepareAxes(int axes);
7077
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007078 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7079 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7080 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7081 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7082 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7083 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7084 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7085 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7086 void processId(MultiTouchInputMapper& mapper, int32_t id);
7087 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7088 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7089 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7090 void processMTSync(MultiTouchInputMapper& mapper);
7091 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007092};
7093
7094void MultiTouchInputMapperTest::prepareAxes(int axes) {
7095 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098 }
7099 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7101 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7104 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105 }
7106 }
7107 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007108 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7109 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007110 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007112 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113 }
7114 }
7115 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007116 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7117 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007118 }
7119 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007120 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7121 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007122 }
7123 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007124 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7125 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 }
7127 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007128 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7129 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007130 }
7131 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007132 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7133 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134 }
7135 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007136 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137 }
7138}
7139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007140void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7141 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144}
7145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007146void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7147 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007149}
7150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007151void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7152 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154}
7155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007156void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158}
7159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007160void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162}
7163
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007164void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7165 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167}
7168
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007169void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171}
7172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007173void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175}
7176
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007177void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179}
7180
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007181void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183}
7184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007185void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007187}
7188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007189void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7190 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007192}
7193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007194void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196}
7197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007198void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200}
7201
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203 addConfigurationProperty("touch.deviceType", "touchScreen");
7204 prepareDisplay(DISPLAY_ORIENTATION_0);
7205 prepareAxes(POSITION);
7206 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007207 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208
arthurhungdcef2dc2020-08-11 14:47:50 +08007209 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210
7211 NotifyMotionArgs motionArgs;
7212
7213 // Two fingers down at once.
7214 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7215 processPosition(mapper, x1, y1);
7216 processMTSync(mapper);
7217 processPosition(mapper, x2, y2);
7218 processMTSync(mapper);
7219 processSync(mapper);
7220
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7222 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7223 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7224 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7225 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7226 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7227 ASSERT_EQ(0, motionArgs.flags);
7228 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7229 ASSERT_EQ(0, motionArgs.buttonState);
7230 ASSERT_EQ(0, motionArgs.edgeFlags);
7231 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7232 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7235 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7236 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7237 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7238 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7239
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7241 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7242 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7243 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7244 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007245 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246 ASSERT_EQ(0, motionArgs.flags);
7247 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7248 ASSERT_EQ(0, motionArgs.buttonState);
7249 ASSERT_EQ(0, motionArgs.edgeFlags);
7250 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7251 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7253 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7256 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7258 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7259 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7260 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7261 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7262
7263 // Move.
7264 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7265 processPosition(mapper, x1, y1);
7266 processMTSync(mapper);
7267 processPosition(mapper, x2, y2);
7268 processMTSync(mapper);
7269 processSync(mapper);
7270
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7273 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7274 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7275 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7277 ASSERT_EQ(0, motionArgs.flags);
7278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7279 ASSERT_EQ(0, motionArgs.buttonState);
7280 ASSERT_EQ(0, motionArgs.edgeFlags);
7281 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7282 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7284 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7287 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7289 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7290 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7291 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7293
7294 // First finger up.
7295 x2 += 15; y2 -= 20;
7296 processPosition(mapper, x2, y2);
7297 processMTSync(mapper);
7298 processSync(mapper);
7299
7300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7301 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7302 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7303 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7304 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007305 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306 ASSERT_EQ(0, motionArgs.flags);
7307 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7308 ASSERT_EQ(0, motionArgs.buttonState);
7309 ASSERT_EQ(0, motionArgs.edgeFlags);
7310 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7311 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7313 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7316 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7318 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7319 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7320 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7322
7323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7324 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7325 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7326 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7327 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7329 ASSERT_EQ(0, motionArgs.flags);
7330 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7331 ASSERT_EQ(0, motionArgs.buttonState);
7332 ASSERT_EQ(0, motionArgs.edgeFlags);
7333 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7334 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7337 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7338 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7339 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7340 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7341
7342 // Move.
7343 x2 += 20; y2 -= 25;
7344 processPosition(mapper, x2, y2);
7345 processMTSync(mapper);
7346 processSync(mapper);
7347
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7349 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7350 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7351 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7352 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7354 ASSERT_EQ(0, motionArgs.flags);
7355 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7356 ASSERT_EQ(0, motionArgs.buttonState);
7357 ASSERT_EQ(0, motionArgs.edgeFlags);
7358 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7359 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7362 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7363 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7364 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7366
7367 // New finger down.
7368 int32_t x3 = 700, y3 = 300;
7369 processPosition(mapper, x2, y2);
7370 processMTSync(mapper);
7371 processPosition(mapper, x3, y3);
7372 processMTSync(mapper);
7373 processSync(mapper);
7374
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7377 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7378 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7379 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007380 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007381 ASSERT_EQ(0, motionArgs.flags);
7382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7383 ASSERT_EQ(0, motionArgs.buttonState);
7384 ASSERT_EQ(0, motionArgs.edgeFlags);
7385 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7386 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7388 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7391 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7393 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7394 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7395 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7397
7398 // Second finger up.
7399 x3 += 30; y3 -= 20;
7400 processPosition(mapper, x3, y3);
7401 processMTSync(mapper);
7402 processSync(mapper);
7403
7404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7406 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7407 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7408 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007409 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007410 ASSERT_EQ(0, motionArgs.flags);
7411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7412 ASSERT_EQ(0, motionArgs.buttonState);
7413 ASSERT_EQ(0, motionArgs.edgeFlags);
7414 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7415 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7417 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7420 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7422 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7423 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7424 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7425 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7426
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7428 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7429 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7430 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7431 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7433 ASSERT_EQ(0, motionArgs.flags);
7434 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7435 ASSERT_EQ(0, motionArgs.buttonState);
7436 ASSERT_EQ(0, motionArgs.edgeFlags);
7437 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7438 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7441 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7442 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7443 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7444 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7445
7446 // Last finger up.
7447 processMTSync(mapper);
7448 processSync(mapper);
7449
7450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7451 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7452 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7453 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7454 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7455 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7456 ASSERT_EQ(0, motionArgs.flags);
7457 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7458 ASSERT_EQ(0, motionArgs.buttonState);
7459 ASSERT_EQ(0, motionArgs.edgeFlags);
7460 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7464 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7465 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7466 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7468
7469 // Should not have sent any more keys or motions.
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7472}
7473
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007474TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7475 addConfigurationProperty("touch.deviceType", "touchScreen");
7476 prepareDisplay(DISPLAY_ORIENTATION_0);
7477
7478 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7479 /*fuzz*/ 0, /*resolution*/ 10);
7480 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7481 /*fuzz*/ 0, /*resolution*/ 11);
7482 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7483 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7484 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7485 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7486 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7487 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7488 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7489 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7490
7491 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7492
7493 // X and Y axes
7494 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7495 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7496 // Touch major and minor
7497 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7498 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7499 // Tool major and minor
7500 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7501 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7502}
7503
7504TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7505 addConfigurationProperty("touch.deviceType", "touchScreen");
7506 prepareDisplay(DISPLAY_ORIENTATION_0);
7507
7508 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7509 /*fuzz*/ 0, /*resolution*/ 10);
7510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7511 /*fuzz*/ 0, /*resolution*/ 11);
7512
7513 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7514
7515 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7516
7517 // Touch major and minor
7518 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7519 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7520 // Tool major and minor
7521 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7522 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7523}
7524
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007526 addConfigurationProperty("touch.deviceType", "touchScreen");
7527 prepareDisplay(DISPLAY_ORIENTATION_0);
7528 prepareAxes(POSITION | ID);
7529 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007530 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007531
arthurhungdcef2dc2020-08-11 14:47:50 +08007532 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533
7534 NotifyMotionArgs motionArgs;
7535
7536 // Two fingers down at once.
7537 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7538 processPosition(mapper, x1, y1);
7539 processId(mapper, 1);
7540 processMTSync(mapper);
7541 processPosition(mapper, x2, y2);
7542 processId(mapper, 2);
7543 processMTSync(mapper);
7544 processSync(mapper);
7545
7546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7547 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7548 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7549 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7552 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7553
7554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007555 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007556 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7557 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7559 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7562 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7564 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7565
7566 // Move.
7567 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7568 processPosition(mapper, x1, y1);
7569 processId(mapper, 1);
7570 processMTSync(mapper);
7571 processPosition(mapper, x2, y2);
7572 processId(mapper, 2);
7573 processMTSync(mapper);
7574 processSync(mapper);
7575
7576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7578 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7579 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7581 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7584 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7586 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7587
7588 // First finger up.
7589 x2 += 15; y2 -= 20;
7590 processPosition(mapper, x2, y2);
7591 processId(mapper, 2);
7592 processMTSync(mapper);
7593 processSync(mapper);
7594
7595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007596 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007597 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7598 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7600 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7603 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7605 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7606
7607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7609 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7610 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7613 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7614
7615 // Move.
7616 x2 += 20; y2 -= 25;
7617 processPosition(mapper, x2, y2);
7618 processId(mapper, 2);
7619 processMTSync(mapper);
7620 processSync(mapper);
7621
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7624 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7625 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7628 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7629
7630 // New finger down.
7631 int32_t x3 = 700, y3 = 300;
7632 processPosition(mapper, x2, y2);
7633 processId(mapper, 2);
7634 processMTSync(mapper);
7635 processPosition(mapper, x3, y3);
7636 processId(mapper, 3);
7637 processMTSync(mapper);
7638 processSync(mapper);
7639
7640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007641 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007642 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7643 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7645 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7648 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7650 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7651
7652 // Second finger up.
7653 x3 += 30; y3 -= 20;
7654 processPosition(mapper, x3, y3);
7655 processId(mapper, 3);
7656 processMTSync(mapper);
7657 processSync(mapper);
7658
7659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007660 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007661 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7664 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7667 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7669 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7670
7671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7673 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7674 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7677 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7678
7679 // Last finger up.
7680 processMTSync(mapper);
7681 processSync(mapper);
7682
7683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7684 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7685 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7686 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7689 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7690
7691 // Should not have sent any more keys or motions.
7692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7694}
7695
7696TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007697 addConfigurationProperty("touch.deviceType", "touchScreen");
7698 prepareDisplay(DISPLAY_ORIENTATION_0);
7699 prepareAxes(POSITION | ID | SLOT);
7700 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007701 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007702
arthurhungdcef2dc2020-08-11 14:47:50 +08007703 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007704
7705 NotifyMotionArgs motionArgs;
7706
7707 // Two fingers down at once.
7708 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7709 processPosition(mapper, x1, y1);
7710 processId(mapper, 1);
7711 processSlot(mapper, 1);
7712 processPosition(mapper, x2, y2);
7713 processId(mapper, 2);
7714 processSync(mapper);
7715
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7717 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7718 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7719 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7722 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7723
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007725 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007726 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7727 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7729 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7732 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7734 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7735
7736 // Move.
7737 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7738 processSlot(mapper, 0);
7739 processPosition(mapper, x1, y1);
7740 processSlot(mapper, 1);
7741 processPosition(mapper, x2, y2);
7742 processSync(mapper);
7743
7744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7746 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7747 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7749 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7752 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7754 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7755
7756 // First finger up.
7757 x2 += 15; y2 -= 20;
7758 processSlot(mapper, 0);
7759 processId(mapper, -1);
7760 processSlot(mapper, 1);
7761 processPosition(mapper, x2, y2);
7762 processSync(mapper);
7763
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007765 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007766 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7767 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7769 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7772 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7774 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7775
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7778 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7779 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7782 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7783
7784 // Move.
7785 x2 += 20; y2 -= 25;
7786 processPosition(mapper, x2, y2);
7787 processSync(mapper);
7788
7789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7791 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7792 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7795 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7796
7797 // New finger down.
7798 int32_t x3 = 700, y3 = 300;
7799 processPosition(mapper, x2, y2);
7800 processSlot(mapper, 0);
7801 processId(mapper, 3);
7802 processPosition(mapper, x3, y3);
7803 processSync(mapper);
7804
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007806 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7808 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7810 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7813 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7815 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7816
7817 // Second finger up.
7818 x3 += 30; y3 -= 20;
7819 processSlot(mapper, 1);
7820 processId(mapper, -1);
7821 processSlot(mapper, 0);
7822 processPosition(mapper, x3, y3);
7823 processSync(mapper);
7824
7825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007826 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7828 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7830 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7833 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7835 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7836
7837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7839 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7843 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7844
7845 // Last finger up.
7846 processId(mapper, -1);
7847 processSync(mapper);
7848
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7850 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7855 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7856
7857 // Should not have sent any more keys or motions.
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7860}
7861
7862TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007863 addConfigurationProperty("touch.deviceType", "touchScreen");
7864 prepareDisplay(DISPLAY_ORIENTATION_0);
7865 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007866 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007867
7868 // These calculations are based on the input device calibration documentation.
7869 int32_t rawX = 100;
7870 int32_t rawY = 200;
7871 int32_t rawTouchMajor = 7;
7872 int32_t rawTouchMinor = 6;
7873 int32_t rawToolMajor = 9;
7874 int32_t rawToolMinor = 8;
7875 int32_t rawPressure = 11;
7876 int32_t rawDistance = 0;
7877 int32_t rawOrientation = 3;
7878 int32_t id = 5;
7879
7880 float x = toDisplayX(rawX);
7881 float y = toDisplayY(rawY);
7882 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7883 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7884 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7885 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7886 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7887 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7888 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7889 float distance = float(rawDistance);
7890
7891 processPosition(mapper, rawX, rawY);
7892 processTouchMajor(mapper, rawTouchMajor);
7893 processTouchMinor(mapper, rawTouchMinor);
7894 processToolMajor(mapper, rawToolMajor);
7895 processToolMinor(mapper, rawToolMinor);
7896 processPressure(mapper, rawPressure);
7897 processOrientation(mapper, rawOrientation);
7898 processDistance(mapper, rawDistance);
7899 processId(mapper, id);
7900 processMTSync(mapper);
7901 processSync(mapper);
7902
7903 NotifyMotionArgs args;
7904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7905 ASSERT_EQ(0, args.pointerProperties[0].id);
7906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7907 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7908 orientation, distance));
7909}
7910
7911TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007912 addConfigurationProperty("touch.deviceType", "touchScreen");
7913 prepareDisplay(DISPLAY_ORIENTATION_0);
7914 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7915 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007916 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007917
7918 // These calculations are based on the input device calibration documentation.
7919 int32_t rawX = 100;
7920 int32_t rawY = 200;
7921 int32_t rawTouchMajor = 140;
7922 int32_t rawTouchMinor = 120;
7923 int32_t rawToolMajor = 180;
7924 int32_t rawToolMinor = 160;
7925
7926 float x = toDisplayX(rawX);
7927 float y = toDisplayY(rawY);
7928 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7929 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7930 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7931 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7932 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7933
7934 processPosition(mapper, rawX, rawY);
7935 processTouchMajor(mapper, rawTouchMajor);
7936 processTouchMinor(mapper, rawTouchMinor);
7937 processToolMajor(mapper, rawToolMajor);
7938 processToolMinor(mapper, rawToolMinor);
7939 processMTSync(mapper);
7940 processSync(mapper);
7941
7942 NotifyMotionArgs args;
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7945 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7946}
7947
7948TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949 addConfigurationProperty("touch.deviceType", "touchScreen");
7950 prepareDisplay(DISPLAY_ORIENTATION_0);
7951 prepareAxes(POSITION | TOUCH | TOOL);
7952 addConfigurationProperty("touch.size.calibration", "diameter");
7953 addConfigurationProperty("touch.size.scale", "10");
7954 addConfigurationProperty("touch.size.bias", "160");
7955 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007956 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957
7958 // These calculations are based on the input device calibration documentation.
7959 // Note: We only provide a single common touch/tool value because the device is assumed
7960 // not to emit separate values for each pointer (isSummed = 1).
7961 int32_t rawX = 100;
7962 int32_t rawY = 200;
7963 int32_t rawX2 = 150;
7964 int32_t rawY2 = 250;
7965 int32_t rawTouchMajor = 5;
7966 int32_t rawToolMajor = 8;
7967
7968 float x = toDisplayX(rawX);
7969 float y = toDisplayY(rawY);
7970 float x2 = toDisplayX(rawX2);
7971 float y2 = toDisplayY(rawY2);
7972 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7973 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7974 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7975
7976 processPosition(mapper, rawX, rawY);
7977 processTouchMajor(mapper, rawTouchMajor);
7978 processToolMajor(mapper, rawToolMajor);
7979 processMTSync(mapper);
7980 processPosition(mapper, rawX2, rawY2);
7981 processTouchMajor(mapper, rawTouchMajor);
7982 processToolMajor(mapper, rawToolMajor);
7983 processMTSync(mapper);
7984 processSync(mapper);
7985
7986 NotifyMotionArgs args;
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7989
7990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007991 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007992 ASSERT_EQ(size_t(2), args.pointerCount);
7993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7994 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7996 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7997}
7998
7999TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000 addConfigurationProperty("touch.deviceType", "touchScreen");
8001 prepareDisplay(DISPLAY_ORIENTATION_0);
8002 prepareAxes(POSITION | TOUCH | TOOL);
8003 addConfigurationProperty("touch.size.calibration", "area");
8004 addConfigurationProperty("touch.size.scale", "43");
8005 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008006 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007
8008 // These calculations are based on the input device calibration documentation.
8009 int32_t rawX = 100;
8010 int32_t rawY = 200;
8011 int32_t rawTouchMajor = 5;
8012 int32_t rawToolMajor = 8;
8013
8014 float x = toDisplayX(rawX);
8015 float y = toDisplayY(rawY);
8016 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8017 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8018 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8019
8020 processPosition(mapper, rawX, rawY);
8021 processTouchMajor(mapper, rawTouchMajor);
8022 processToolMajor(mapper, rawToolMajor);
8023 processMTSync(mapper);
8024 processSync(mapper);
8025
8026 NotifyMotionArgs args;
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8029 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8030}
8031
8032TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008033 addConfigurationProperty("touch.deviceType", "touchScreen");
8034 prepareDisplay(DISPLAY_ORIENTATION_0);
8035 prepareAxes(POSITION | PRESSURE);
8036 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8037 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008038 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039
Michael Wrightaa449c92017-12-13 21:21:43 +00008040 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008041 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008042 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8043 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8044 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8045
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046 // These calculations are based on the input device calibration documentation.
8047 int32_t rawX = 100;
8048 int32_t rawY = 200;
8049 int32_t rawPressure = 60;
8050
8051 float x = toDisplayX(rawX);
8052 float y = toDisplayY(rawY);
8053 float pressure = float(rawPressure) * 0.01f;
8054
8055 processPosition(mapper, rawX, rawY);
8056 processPressure(mapper, rawPressure);
8057 processMTSync(mapper);
8058 processSync(mapper);
8059
8060 NotifyMotionArgs args;
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8063 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8064}
8065
8066TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 addConfigurationProperty("touch.deviceType", "touchScreen");
8068 prepareDisplay(DISPLAY_ORIENTATION_0);
8069 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008070 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071
8072 NotifyMotionArgs motionArgs;
8073 NotifyKeyArgs keyArgs;
8074
8075 processId(mapper, 1);
8076 processPosition(mapper, 100, 200);
8077 processSync(mapper);
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8079 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8080 ASSERT_EQ(0, motionArgs.buttonState);
8081
8082 // press BTN_LEFT, release BTN_LEFT
8083 processKey(mapper, BTN_LEFT, 1);
8084 processSync(mapper);
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8087 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8088
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8091 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8092
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 processKey(mapper, BTN_LEFT, 0);
8094 processSync(mapper);
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008098
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008101 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102
8103 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8104 processKey(mapper, BTN_RIGHT, 1);
8105 processKey(mapper, BTN_MIDDLE, 1);
8106 processSync(mapper);
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8109 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8110 motionArgs.buttonState);
8111
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8114 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8115
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8118 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8119 motionArgs.buttonState);
8120
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 processKey(mapper, BTN_RIGHT, 0);
8122 processSync(mapper);
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008126
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008129 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008130
8131 processKey(mapper, BTN_MIDDLE, 0);
8132 processSync(mapper);
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008136
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008139 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140
8141 // press BTN_BACK, release BTN_BACK
8142 processKey(mapper, BTN_BACK, 1);
8143 processSync(mapper);
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8146 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008147
Michael Wrightd02c5b62014-02-10 15:10:22 -08008148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008150 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8151
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8154 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155
8156 processKey(mapper, BTN_BACK, 0);
8157 processSync(mapper);
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008161
8162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008164 ASSERT_EQ(0, motionArgs.buttonState);
8165
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8167 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8168 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8169
8170 // press BTN_SIDE, release BTN_SIDE
8171 processKey(mapper, BTN_SIDE, 1);
8172 processSync(mapper);
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8174 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8175 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008176
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008179 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8180
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8183 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184
8185 processKey(mapper, BTN_SIDE, 0);
8186 processSync(mapper);
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008190
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008193 ASSERT_EQ(0, motionArgs.buttonState);
8194
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8196 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8197 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8198
8199 // press BTN_FORWARD, release BTN_FORWARD
8200 processKey(mapper, BTN_FORWARD, 1);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8203 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8204 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008205
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008208 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8209
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8212 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213
8214 processKey(mapper, BTN_FORWARD, 0);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008218 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008219
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008222 ASSERT_EQ(0, motionArgs.buttonState);
8223
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8225 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8226 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8227
8228 // press BTN_EXTRA, release BTN_EXTRA
8229 processKey(mapper, BTN_EXTRA, 1);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8232 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8233 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008234
Michael Wrightd02c5b62014-02-10 15:10:22 -08008235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008237 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8238
8239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8241 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242
8243 processKey(mapper, BTN_EXTRA, 0);
8244 processSync(mapper);
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008248
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008251 ASSERT_EQ(0, motionArgs.buttonState);
8252
Michael Wrightd02c5b62014-02-10 15:10:22 -08008253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8254 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8255 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8256
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8258
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259 // press BTN_STYLUS, release BTN_STYLUS
8260 processKey(mapper, BTN_STYLUS, 1);
8261 processSync(mapper);
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008264 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8265
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8268 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269
8270 processKey(mapper, BTN_STYLUS, 0);
8271 processSync(mapper);
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008275
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008278 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279
8280 // press BTN_STYLUS2, release BTN_STYLUS2
8281 processKey(mapper, BTN_STYLUS2, 1);
8282 processSync(mapper);
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008285 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8286
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8289 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290
8291 processKey(mapper, BTN_STYLUS2, 0);
8292 processSync(mapper);
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008295 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008296
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008299 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300
8301 // release touch
8302 processId(mapper, -1);
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8305 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8306 ASSERT_EQ(0, motionArgs.buttonState);
8307}
8308
8309TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 addConfigurationProperty("touch.deviceType", "touchScreen");
8311 prepareDisplay(DISPLAY_ORIENTATION_0);
8312 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008313 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314
8315 NotifyMotionArgs motionArgs;
8316
8317 // default tool type is finger
8318 processId(mapper, 1);
8319 processPosition(mapper, 100, 200);
8320 processSync(mapper);
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8322 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8324
8325 // eraser
8326 processKey(mapper, BTN_TOOL_RUBBER, 1);
8327 processSync(mapper);
8328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8331
8332 // stylus
8333 processKey(mapper, BTN_TOOL_RUBBER, 0);
8334 processKey(mapper, BTN_TOOL_PEN, 1);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8339
8340 // brush
8341 processKey(mapper, BTN_TOOL_PEN, 0);
8342 processKey(mapper, BTN_TOOL_BRUSH, 1);
8343 processSync(mapper);
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8347
8348 // pencil
8349 processKey(mapper, BTN_TOOL_BRUSH, 0);
8350 processKey(mapper, BTN_TOOL_PENCIL, 1);
8351 processSync(mapper);
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8355
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008356 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357 processKey(mapper, BTN_TOOL_PENCIL, 0);
8358 processKey(mapper, BTN_TOOL_AIRBRUSH, 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 // mouse
8365 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8366 processKey(mapper, BTN_TOOL_MOUSE, 1);
8367 processSync(mapper);
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8371
8372 // lens
8373 processKey(mapper, BTN_TOOL_MOUSE, 0);
8374 processKey(mapper, BTN_TOOL_LENS, 1);
8375 processSync(mapper);
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8379
8380 // double-tap
8381 processKey(mapper, BTN_TOOL_LENS, 0);
8382 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8383 processSync(mapper);
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8387
8388 // triple-tap
8389 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8390 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8391 processSync(mapper);
8392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8395
8396 // quad-tap
8397 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8398 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8399 processSync(mapper);
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8403
8404 // finger
8405 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8406 processKey(mapper, BTN_TOOL_FINGER, 1);
8407 processSync(mapper);
8408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8411
8412 // stylus trumps finger
8413 processKey(mapper, BTN_TOOL_PEN, 1);
8414 processSync(mapper);
8415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8418
8419 // eraser trumps stylus
8420 processKey(mapper, BTN_TOOL_RUBBER, 1);
8421 processSync(mapper);
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8425
8426 // mouse trumps eraser
8427 processKey(mapper, BTN_TOOL_MOUSE, 1);
8428 processSync(mapper);
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8432
8433 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8434 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8435 processSync(mapper);
8436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8439
8440 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8441 processToolType(mapper, MT_TOOL_PEN);
8442 processSync(mapper);
8443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8446
8447 // back to default tool type
8448 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8449 processKey(mapper, BTN_TOOL_MOUSE, 0);
8450 processKey(mapper, BTN_TOOL_RUBBER, 0);
8451 processKey(mapper, BTN_TOOL_PEN, 0);
8452 processKey(mapper, BTN_TOOL_FINGER, 0);
8453 processSync(mapper);
8454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8457}
8458
8459TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008460 addConfigurationProperty("touch.deviceType", "touchScreen");
8461 prepareDisplay(DISPLAY_ORIENTATION_0);
8462 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008463 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008464 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465
8466 NotifyMotionArgs motionArgs;
8467
8468 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8469 processId(mapper, 1);
8470 processPosition(mapper, 100, 200);
8471 processSync(mapper);
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8475 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8476
8477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8478 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8480 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8481
8482 // move a little
8483 processPosition(mapper, 150, 250);
8484 processSync(mapper);
8485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8488 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8489
8490 // down when BTN_TOUCH is pressed, pressure defaults to 1
8491 processKey(mapper, BTN_TOUCH, 1);
8492 processSync(mapper);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8496 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8497
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8499 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8501 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8502
8503 // up when BTN_TOUCH is released, hover restored
8504 processKey(mapper, BTN_TOUCH, 0);
8505 processSync(mapper);
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8507 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8509 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8510
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8514 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8515
8516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8519 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8520
8521 // exit hover when pointer goes away
8522 processId(mapper, -1);
8523 processSync(mapper);
8524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8527 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8528}
8529
8530TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008531 addConfigurationProperty("touch.deviceType", "touchScreen");
8532 prepareDisplay(DISPLAY_ORIENTATION_0);
8533 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008534 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535
8536 NotifyMotionArgs motionArgs;
8537
8538 // initially hovering because pressure is 0
8539 processId(mapper, 1);
8540 processPosition(mapper, 100, 200);
8541 processPressure(mapper, 0);
8542 processSync(mapper);
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8546 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8547
8548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8549 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8551 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8552
8553 // move a little
8554 processPosition(mapper, 150, 250);
8555 processSync(mapper);
8556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8559 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8560
8561 // down when pressure becomes non-zero
8562 processPressure(mapper, RAW_PRESSURE_MAX);
8563 processSync(mapper);
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8567 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8568
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8570 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8572 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8573
8574 // up when pressure becomes 0, hover restored
8575 processPressure(mapper, 0);
8576 processSync(mapper);
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8580 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8581
8582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8585 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8586
8587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8588 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8590 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8591
8592 // exit hover when pointer goes away
8593 processId(mapper, -1);
8594 processSync(mapper);
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8598 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8599}
8600
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008601/**
8602 * Set the input device port <--> display port associations, and check that the
8603 * events are routed to the display that matches the display port.
8604 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8605 */
8606TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008607 const std::string usb2 = "USB2";
8608 const uint8_t hdmi1 = 0;
8609 const uint8_t hdmi2 = 1;
8610 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008611 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008612
8613 addConfigurationProperty("touch.deviceType", "touchScreen");
8614 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008615 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008616
8617 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8618 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8619
8620 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8621 // for this input device is specified, and the matching viewport is not present,
8622 // the input device should be disabled (at the mapper level).
8623
8624 // Add viewport for display 2 on hdmi2
8625 prepareSecondaryDisplay(type, hdmi2);
8626 // Send a touch event
8627 processPosition(mapper, 100, 100);
8628 processSync(mapper);
8629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8630
8631 // Add viewport for display 1 on hdmi1
8632 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8633 // Send a touch event again
8634 processPosition(mapper, 100, 100);
8635 processSync(mapper);
8636
8637 NotifyMotionArgs args;
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8639 ASSERT_EQ(DISPLAY_ID, args.displayId);
8640}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008641
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008642TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8643 addConfigurationProperty("touch.deviceType", "touchScreen");
8644 prepareAxes(POSITION);
8645 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8646
8647 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8648
8649 prepareDisplay(DISPLAY_ORIENTATION_0);
8650 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8651
8652 // Send a touch event
8653 processPosition(mapper, 100, 100);
8654 processSync(mapper);
8655
8656 NotifyMotionArgs args;
8657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8658 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8659}
8660
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008661TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008662 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008663 std::shared_ptr<FakePointerController> fakePointerController =
8664 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008665 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008666 fakePointerController->setPosition(100, 200);
8667 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008668 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008669
Garfield Tan888a6a42020-01-09 11:39:16 -08008670 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008671 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008672
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008673 prepareDisplay(DISPLAY_ORIENTATION_0);
8674 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008675 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008676
8677 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008678 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008679
8680 NotifyMotionArgs motionArgs;
8681 processPosition(mapper, 100, 100);
8682 processSync(mapper);
8683
8684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8685 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8686 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8687}
8688
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008689/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008690 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8691 */
8692TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8693 addConfigurationProperty("touch.deviceType", "touchScreen");
8694 prepareAxes(POSITION);
8695 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8696
8697 prepareDisplay(DISPLAY_ORIENTATION_0);
8698 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8699 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8700 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8701 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8702
8703 NotifyMotionArgs args;
8704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8705 ASSERT_EQ(26, args.readTime);
8706
8707 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8708 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8709 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8710
8711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8712 ASSERT_EQ(33, args.readTime);
8713}
8714
8715/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008716 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8717 * events should not be delivered to the listener.
8718 */
8719TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8720 addConfigurationProperty("touch.deviceType", "touchScreen");
8721 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8722 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8723 ViewportType::INTERNAL);
8724 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8725 prepareAxes(POSITION);
8726 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8727
8728 NotifyMotionArgs motionArgs;
8729 processPosition(mapper, 100, 100);
8730 processSync(mapper);
8731
8732 mFakeListener->assertNotifyMotionWasNotCalled();
8733}
8734
Garfield Tanc734e4f2021-01-15 20:01:39 -08008735TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8736 addConfigurationProperty("touch.deviceType", "touchScreen");
8737 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8738 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8739 ViewportType::INTERNAL);
8740 std::optional<DisplayViewport> optionalDisplayViewport =
8741 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8742 ASSERT_TRUE(optionalDisplayViewport.has_value());
8743 DisplayViewport displayViewport = *optionalDisplayViewport;
8744
8745 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8746 prepareAxes(POSITION);
8747 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8748
8749 // Finger down
8750 int32_t x = 100, y = 100;
8751 processPosition(mapper, x, y);
8752 processSync(mapper);
8753
8754 NotifyMotionArgs motionArgs;
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8756 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8757
8758 // Deactivate display viewport
8759 displayViewport.isActive = false;
8760 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8761 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8762
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008763 // The ongoing touch should be canceled immediately
8764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8765 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8766
8767 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008768 x += 10, y += 10;
8769 processPosition(mapper, x, y);
8770 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008772
8773 // Reactivate display viewport
8774 displayViewport.isActive = true;
8775 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8776 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8777
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008778 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008779 x += 10, y += 10;
8780 processPosition(mapper, x, y);
8781 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008784}
8785
Arthur Hung7c645402019-01-25 17:45:42 +08008786TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8787 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008788 prepareAxes(POSITION | ID | SLOT);
8789 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008790 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008791
8792 // Create the second touch screen device, and enable multi fingers.
8793 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008794 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008795 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008796 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008797 std::shared_ptr<InputDevice> device2 =
8798 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008799 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008800
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008801 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8802 0 /*flat*/, 0 /*fuzz*/);
8803 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8804 0 /*flat*/, 0 /*fuzz*/);
8805 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8806 0 /*flat*/, 0 /*fuzz*/);
8807 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8808 0 /*flat*/, 0 /*fuzz*/);
8809 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8810 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8811 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008812
8813 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008814 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008815 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8816 device2->reset(ARBITRARY_TIME);
8817
8818 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008819 std::shared_ptr<FakePointerController> fakePointerController =
8820 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008821 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008822
8823 // Setup policy for associated displays and show touches.
8824 const uint8_t hdmi1 = 0;
8825 const uint8_t hdmi2 = 1;
8826 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8827 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8828 mFakePolicy->setShowTouches(true);
8829
8830 // Create displays.
8831 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008832 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008833
8834 // Default device will reconfigure above, need additional reconfiguration for another device.
8835 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008836 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8837 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008838
8839 // Two fingers down at default display.
8840 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8841 processPosition(mapper, x1, y1);
8842 processId(mapper, 1);
8843 processSlot(mapper, 1);
8844 processPosition(mapper, x2, y2);
8845 processId(mapper, 2);
8846 processSync(mapper);
8847
8848 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8849 fakePointerController->getSpots().find(DISPLAY_ID);
8850 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8851 ASSERT_EQ(size_t(2), iter->second.size());
8852
8853 // Two fingers down at second display.
8854 processPosition(mapper2, x1, y1);
8855 processId(mapper2, 1);
8856 processSlot(mapper2, 1);
8857 processPosition(mapper2, x2, y2);
8858 processId(mapper2, 2);
8859 processSync(mapper2);
8860
8861 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8862 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8863 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008864
8865 // Disable the show touches configuration and ensure the spots are cleared.
8866 mFakePolicy->setShowTouches(false);
8867 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8868 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8869
8870 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008871}
8872
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008873TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008874 prepareAxes(POSITION);
8875 addConfigurationProperty("touch.deviceType", "touchScreen");
8876 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008877 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008878
8879 NotifyMotionArgs motionArgs;
8880 // Unrotated video frame
8881 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8882 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008883 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008884 processPosition(mapper, 100, 200);
8885 processSync(mapper);
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8887 ASSERT_EQ(frames, motionArgs.videoFrames);
8888
8889 // Subsequent touch events should not have any videoframes
8890 // This is implemented separately in FakeEventHub,
8891 // but that should match the behaviour of TouchVideoDevice.
8892 processPosition(mapper, 200, 200);
8893 processSync(mapper);
8894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8895 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8896}
8897
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008898TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008899 prepareAxes(POSITION);
8900 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008901 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008902 // Unrotated video frame
8903 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8904 NotifyMotionArgs motionArgs;
8905
8906 // Test all 4 orientations
8907 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008908 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8909 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8910 clearViewports();
8911 prepareDisplay(orientation);
8912 std::vector<TouchVideoFrame> frames{frame};
8913 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8914 processPosition(mapper, 100, 200);
8915 processSync(mapper);
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8917 ASSERT_EQ(frames, motionArgs.videoFrames);
8918 }
8919}
8920
8921TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
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 frame
8929 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8930 NotifyMotionArgs motionArgs;
8931
8932 // Test all 4 orientations
8933 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008934 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8935 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8936 clearViewports();
8937 prepareDisplay(orientation);
8938 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008939 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008940 processPosition(mapper, 100, 200);
8941 processSync(mapper);
8942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008943 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8944 // compared to the display. This is so that when the window transform (which contains the
8945 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8946 // window's coordinate space.
8947 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008948 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008949
8950 // Release finger.
8951 processSync(mapper);
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008953 }
8954}
8955
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008956TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008957 prepareAxes(POSITION);
8958 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008960 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8961 // so mix these.
8962 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8963 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8964 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8965 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8966 NotifyMotionArgs motionArgs;
8967
8968 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008969 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008970 processPosition(mapper, 100, 200);
8971 processSync(mapper);
8972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008973 ASSERT_EQ(frames, motionArgs.videoFrames);
8974}
8975
8976TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8977 prepareAxes(POSITION);
8978 addConfigurationProperty("touch.deviceType", "touchScreen");
8979 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8980 // orientation-aware are affected by display rotation.
8981 addConfigurationProperty("touch.orientationAware", "0");
8982 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8983 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8984 // so mix these.
8985 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8986 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8987 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8988 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8989 NotifyMotionArgs motionArgs;
8990
8991 prepareDisplay(DISPLAY_ORIENTATION_90);
8992 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8993 processPosition(mapper, 100, 200);
8994 processSync(mapper);
8995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8996 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8997 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8998 // compared to the display. This is so that when the window transform (which contains the
8999 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9000 // window's coordinate space.
9001 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9002 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009003 ASSERT_EQ(frames, motionArgs.videoFrames);
9004}
9005
Arthur Hung9da14732019-09-02 16:16:58 +08009006/**
9007 * If we had defined port associations, but the viewport is not ready, the touch device would be
9008 * expected to be disabled, and it should be enabled after the viewport has found.
9009 */
9010TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009011 constexpr uint8_t hdmi2 = 1;
9012 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009013 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009014
9015 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9016
9017 addConfigurationProperty("touch.deviceType", "touchScreen");
9018 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009019 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009020
9021 ASSERT_EQ(mDevice->isEnabled(), false);
9022
9023 // Add display on hdmi2, the device should be enabled and can receive touch event.
9024 prepareSecondaryDisplay(type, hdmi2);
9025 ASSERT_EQ(mDevice->isEnabled(), true);
9026
9027 // Send a touch event.
9028 processPosition(mapper, 100, 100);
9029 processSync(mapper);
9030
9031 NotifyMotionArgs args;
9032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9033 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9034}
9035
Arthur Hung421eb1c2020-01-16 00:09:42 +08009036TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009037 addConfigurationProperty("touch.deviceType", "touchScreen");
9038 prepareDisplay(DISPLAY_ORIENTATION_0);
9039 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009040 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009041
9042 NotifyMotionArgs motionArgs;
9043
9044 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9045 // finger down
9046 processId(mapper, 1);
9047 processPosition(mapper, x1, y1);
9048 processSync(mapper);
9049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9050 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9052
9053 // finger move
9054 processId(mapper, 1);
9055 processPosition(mapper, x2, y2);
9056 processSync(mapper);
9057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9060
9061 // finger up.
9062 processId(mapper, -1);
9063 processSync(mapper);
9064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9065 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9067
9068 // new finger down
9069 processId(mapper, 1);
9070 processPosition(mapper, x3, y3);
9071 processSync(mapper);
9072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9073 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9075}
9076
9077/**
arthurhungcc7f9802020-04-30 17:55:40 +08009078 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9079 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009080 */
arthurhungcc7f9802020-04-30 17:55:40 +08009081TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009082 addConfigurationProperty("touch.deviceType", "touchScreen");
9083 prepareDisplay(DISPLAY_ORIENTATION_0);
9084 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009085 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009086
9087 NotifyMotionArgs motionArgs;
9088
9089 // default tool type is finger
9090 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009091 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009092 processPosition(mapper, x1, y1);
9093 processSync(mapper);
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9095 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9097
9098 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9099 processToolType(mapper, MT_TOOL_PALM);
9100 processSync(mapper);
9101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9102 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9103
9104 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009105 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009106 processPosition(mapper, x2, y2);
9107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9109
9110 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009111 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009112 processSync(mapper);
9113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9114
9115 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009116 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009117 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009118 processPosition(mapper, x3, y3);
9119 processSync(mapper);
9120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9121 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9123}
9124
arthurhungbf89a482020-04-17 17:37:55 +08009125/**
arthurhungcc7f9802020-04-30 17:55:40 +08009126 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9127 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009128 */
arthurhungcc7f9802020-04-30 17:55:40 +08009129TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009130 addConfigurationProperty("touch.deviceType", "touchScreen");
9131 prepareDisplay(DISPLAY_ORIENTATION_0);
9132 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9133 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9134
9135 NotifyMotionArgs motionArgs;
9136
9137 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009138 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9139 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009140 processPosition(mapper, x1, y1);
9141 processSync(mapper);
9142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9145
9146 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009147 processSlot(mapper, SECOND_SLOT);
9148 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009149 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009150 processSync(mapper);
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009152 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9154
9155 // If the tool type of the first finger changes to MT_TOOL_PALM,
9156 // we expect to receive ACTION_POINTER_UP with cancel flag.
9157 processSlot(mapper, FIRST_SLOT);
9158 processId(mapper, FIRST_TRACKING_ID);
9159 processToolType(mapper, MT_TOOL_PALM);
9160 processSync(mapper);
9161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009162 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009163 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9164
9165 // The following MOVE events of second finger should be processed.
9166 processSlot(mapper, SECOND_SLOT);
9167 processId(mapper, SECOND_TRACKING_ID);
9168 processPosition(mapper, x2 + 1, y2 + 1);
9169 processSync(mapper);
9170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9172 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9173
9174 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9175 // it. Second finger receive move.
9176 processSlot(mapper, FIRST_SLOT);
9177 processId(mapper, INVALID_TRACKING_ID);
9178 processSync(mapper);
9179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9181 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9182
9183 // Second finger keeps moving.
9184 processSlot(mapper, SECOND_SLOT);
9185 processId(mapper, SECOND_TRACKING_ID);
9186 processPosition(mapper, x2 + 2, y2 + 2);
9187 processSync(mapper);
9188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9190 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9191
9192 // Second finger up.
9193 processId(mapper, INVALID_TRACKING_ID);
9194 processSync(mapper);
9195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9197 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9198}
9199
9200/**
9201 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9202 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9203 */
9204TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9205 addConfigurationProperty("touch.deviceType", "touchScreen");
9206 prepareDisplay(DISPLAY_ORIENTATION_0);
9207 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9208 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9209
9210 NotifyMotionArgs motionArgs;
9211
9212 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9213 // First finger down.
9214 processId(mapper, FIRST_TRACKING_ID);
9215 processPosition(mapper, x1, y1);
9216 processSync(mapper);
9217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9218 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9220
9221 // Second finger down.
9222 processSlot(mapper, SECOND_SLOT);
9223 processId(mapper, SECOND_TRACKING_ID);
9224 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009225 processSync(mapper);
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009227 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9229
arthurhungcc7f9802020-04-30 17:55:40 +08009230 // If the tool type of the first finger changes to MT_TOOL_PALM,
9231 // we expect to receive ACTION_POINTER_UP with cancel flag.
9232 processSlot(mapper, FIRST_SLOT);
9233 processId(mapper, FIRST_TRACKING_ID);
9234 processToolType(mapper, MT_TOOL_PALM);
9235 processSync(mapper);
9236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009237 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009238 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9239
9240 // Second finger keeps moving.
9241 processSlot(mapper, SECOND_SLOT);
9242 processId(mapper, SECOND_TRACKING_ID);
9243 processPosition(mapper, x2 + 1, y2 + 1);
9244 processSync(mapper);
9245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9247
9248 // second finger becomes palm, receive cancel due to only 1 finger is active.
9249 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009250 processToolType(mapper, MT_TOOL_PALM);
9251 processSync(mapper);
9252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9253 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9254
arthurhungcc7f9802020-04-30 17:55:40 +08009255 // third finger down.
9256 processSlot(mapper, THIRD_SLOT);
9257 processId(mapper, THIRD_TRACKING_ID);
9258 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009259 processPosition(mapper, x3, y3);
9260 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009264 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9265
9266 // third finger move
9267 processId(mapper, THIRD_TRACKING_ID);
9268 processPosition(mapper, x3 + 1, y3 + 1);
9269 processSync(mapper);
9270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9272
9273 // first finger up, third finger receive move.
9274 processSlot(mapper, FIRST_SLOT);
9275 processId(mapper, INVALID_TRACKING_ID);
9276 processSync(mapper);
9277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9279 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9280
9281 // second finger up, third finger receive move.
9282 processSlot(mapper, SECOND_SLOT);
9283 processId(mapper, INVALID_TRACKING_ID);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9287 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9288
9289 // third finger up.
9290 processSlot(mapper, THIRD_SLOT);
9291 processId(mapper, INVALID_TRACKING_ID);
9292 processSync(mapper);
9293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9294 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9295 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9296}
9297
9298/**
9299 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9300 * and the active finger could still be allowed to receive the events
9301 */
9302TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9303 addConfigurationProperty("touch.deviceType", "touchScreen");
9304 prepareDisplay(DISPLAY_ORIENTATION_0);
9305 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9306 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9307
9308 NotifyMotionArgs motionArgs;
9309
9310 // default tool type is finger
9311 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9312 processId(mapper, FIRST_TRACKING_ID);
9313 processPosition(mapper, x1, y1);
9314 processSync(mapper);
9315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9316 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9318
9319 // Second finger down.
9320 processSlot(mapper, SECOND_SLOT);
9321 processId(mapper, SECOND_TRACKING_ID);
9322 processPosition(mapper, x2, y2);
9323 processSync(mapper);
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009325 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9327
9328 // If the tool type of the second finger changes to MT_TOOL_PALM,
9329 // we expect to receive ACTION_POINTER_UP with cancel flag.
9330 processId(mapper, SECOND_TRACKING_ID);
9331 processToolType(mapper, MT_TOOL_PALM);
9332 processSync(mapper);
9333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009334 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009335 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9336
9337 // The following MOVE event should be processed.
9338 processSlot(mapper, FIRST_SLOT);
9339 processId(mapper, FIRST_TRACKING_ID);
9340 processPosition(mapper, x1 + 1, y1 + 1);
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 up.
9347 processSlot(mapper, SECOND_SLOT);
9348 processId(mapper, INVALID_TRACKING_ID);
9349 processSync(mapper);
9350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9352
9353 // first finger keep moving
9354 processSlot(mapper, FIRST_SLOT);
9355 processId(mapper, FIRST_TRACKING_ID);
9356 processPosition(mapper, x1 + 2, y1 + 2);
9357 processSync(mapper);
9358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9360
9361 // first finger up.
9362 processId(mapper, INVALID_TRACKING_ID);
9363 processSync(mapper);
9364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9365 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9366 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009367}
9368
Arthur Hung9ad18942021-06-19 02:04:46 +00009369/**
9370 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9371 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9372 * cause slot be valid again.
9373 */
9374TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9375 addConfigurationProperty("touch.deviceType", "touchScreen");
9376 prepareDisplay(DISPLAY_ORIENTATION_0);
9377 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9378 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9379
9380 NotifyMotionArgs motionArgs;
9381
9382 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9383 // First finger down.
9384 processId(mapper, FIRST_TRACKING_ID);
9385 processPosition(mapper, x1, y1);
9386 processPressure(mapper, RAW_PRESSURE_MAX);
9387 processSync(mapper);
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9389 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9390 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9391
9392 // First finger move.
9393 processId(mapper, FIRST_TRACKING_ID);
9394 processPosition(mapper, x1 + 1, y1 + 1);
9395 processPressure(mapper, RAW_PRESSURE_MAX);
9396 processSync(mapper);
9397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9399 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9400
9401 // Second finger down.
9402 processSlot(mapper, SECOND_SLOT);
9403 processId(mapper, SECOND_TRACKING_ID);
9404 processPosition(mapper, x2, y2);
9405 processPressure(mapper, RAW_PRESSURE_MAX);
9406 processSync(mapper);
9407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009408 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009409 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9410
9411 // second finger up with some unexpected data.
9412 processSlot(mapper, SECOND_SLOT);
9413 processId(mapper, INVALID_TRACKING_ID);
9414 processPosition(mapper, x2, y2);
9415 processSync(mapper);
9416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009417 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009418 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9419
9420 // first finger up with some unexpected data.
9421 processSlot(mapper, FIRST_SLOT);
9422 processId(mapper, INVALID_TRACKING_ID);
9423 processPosition(mapper, x2, y2);
9424 processPressure(mapper, RAW_PRESSURE_MAX);
9425 processSync(mapper);
9426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9427 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9428 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9429}
9430
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009431// --- MultiTouchInputMapperTest_ExternalDevice ---
9432
9433class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9434protected:
Chris Yea52ade12020-08-27 16:49:20 -07009435 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009436};
9437
9438/**
9439 * Expect fallback to internal viewport if device is external and external viewport is not present.
9440 */
9441TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9442 prepareAxes(POSITION);
9443 addConfigurationProperty("touch.deviceType", "touchScreen");
9444 prepareDisplay(DISPLAY_ORIENTATION_0);
9445 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9446
9447 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9448
9449 NotifyMotionArgs motionArgs;
9450
9451 // Expect the event to be sent to the internal viewport,
9452 // because an external viewport is not present.
9453 processPosition(mapper, 100, 100);
9454 processSync(mapper);
9455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9456 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9457
9458 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009459 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009460 processPosition(mapper, 100, 100);
9461 processSync(mapper);
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9463 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9464}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009465
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009466TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9467 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9468 std::shared_ptr<FakePointerController> fakePointerController =
9469 std::make_shared<FakePointerController>();
9470 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9471 fakePointerController->setPosition(0, 0);
9472 fakePointerController->setButtonState(0);
9473
9474 // prepare device and capture
9475 prepareDisplay(DISPLAY_ORIENTATION_0);
9476 prepareAxes(POSITION | ID | SLOT);
9477 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9478 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9479 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009480 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009481 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9482
9483 // captured touchpad should be a touchpad source
9484 NotifyDeviceResetArgs resetArgs;
9485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9486 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9487
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009488 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009489
9490 const InputDeviceInfo::MotionRange* relRangeX =
9491 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9492 ASSERT_NE(relRangeX, nullptr);
9493 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9494 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9495 const InputDeviceInfo::MotionRange* relRangeY =
9496 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9497 ASSERT_NE(relRangeY, nullptr);
9498 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9499 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9500
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009501 // run captured pointer tests - note that this is unscaled, so input listener events should be
9502 // identical to what the hardware sends (accounting for any
9503 // calibration).
9504 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009505 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009506 processId(mapper, 1);
9507 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9508 processKey(mapper, BTN_TOUCH, 1);
9509 processSync(mapper);
9510
9511 // expect coord[0] to contain initial location of touch 0
9512 NotifyMotionArgs args;
9513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9514 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9515 ASSERT_EQ(1U, args.pointerCount);
9516 ASSERT_EQ(0, args.pointerProperties[0].id);
9517 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9518 ASSERT_NO_FATAL_FAILURE(
9519 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9520
9521 // FINGER 1 DOWN
9522 processSlot(mapper, 1);
9523 processId(mapper, 2);
9524 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9525 processSync(mapper);
9526
9527 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009529 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009530 ASSERT_EQ(2U, args.pointerCount);
9531 ASSERT_EQ(0, args.pointerProperties[0].id);
9532 ASSERT_EQ(1, args.pointerProperties[1].id);
9533 ASSERT_NO_FATAL_FAILURE(
9534 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9535 ASSERT_NO_FATAL_FAILURE(
9536 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9537
9538 // FINGER 1 MOVE
9539 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9540 processSync(mapper);
9541
9542 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9543 // from move
9544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9546 ASSERT_NO_FATAL_FAILURE(
9547 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9548 ASSERT_NO_FATAL_FAILURE(
9549 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9550
9551 // FINGER 0 MOVE
9552 processSlot(mapper, 0);
9553 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9554 processSync(mapper);
9555
9556 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9559 ASSERT_NO_FATAL_FAILURE(
9560 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9561 ASSERT_NO_FATAL_FAILURE(
9562 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9563
9564 // BUTTON DOWN
9565 processKey(mapper, BTN_LEFT, 1);
9566 processSync(mapper);
9567
9568 // touchinputmapper design sends a move before button press
9569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9573
9574 // BUTTON UP
9575 processKey(mapper, BTN_LEFT, 0);
9576 processSync(mapper);
9577
9578 // touchinputmapper design sends a move after button release
9579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9583
9584 // FINGER 0 UP
9585 processId(mapper, -1);
9586 processSync(mapper);
9587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9588 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9589
9590 // FINGER 1 MOVE
9591 processSlot(mapper, 1);
9592 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9593 processSync(mapper);
9594
9595 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9598 ASSERT_EQ(1U, args.pointerCount);
9599 ASSERT_EQ(1, args.pointerProperties[0].id);
9600 ASSERT_NO_FATAL_FAILURE(
9601 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9602
9603 // FINGER 1 UP
9604 processId(mapper, -1);
9605 processKey(mapper, BTN_TOUCH, 0);
9606 processSync(mapper);
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9608 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9609
9610 // non captured touchpad should be a mouse source
9611 mFakePolicy->setPointerCapture(false);
9612 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9614 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9615}
9616
9617TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9618 std::shared_ptr<FakePointerController> fakePointerController =
9619 std::make_shared<FakePointerController>();
9620 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9621 fakePointerController->setPosition(0, 0);
9622 fakePointerController->setButtonState(0);
9623
9624 // prepare device and capture
9625 prepareDisplay(DISPLAY_ORIENTATION_0);
9626 prepareAxes(POSITION | ID | SLOT);
9627 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9628 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009629 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009630 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9631 // run uncaptured pointer tests - pushes out generic events
9632 // FINGER 0 DOWN
9633 processId(mapper, 3);
9634 processPosition(mapper, 100, 100);
9635 processKey(mapper, BTN_TOUCH, 1);
9636 processSync(mapper);
9637
9638 // start at (100,100), cursor should be at (0,0) * scale
9639 NotifyMotionArgs args;
9640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9641 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9642 ASSERT_NO_FATAL_FAILURE(
9643 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9644
9645 // FINGER 0 MOVE
9646 processPosition(mapper, 200, 200);
9647 processSync(mapper);
9648
9649 // compute scaling to help with touch position checking
9650 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9651 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9652 float scale =
9653 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9654
9655 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9657 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9659 0, 0, 0, 0, 0, 0, 0));
9660}
9661
9662TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9663 std::shared_ptr<FakePointerController> fakePointerController =
9664 std::make_shared<FakePointerController>();
9665
9666 prepareDisplay(DISPLAY_ORIENTATION_0);
9667 prepareAxes(POSITION | ID | SLOT);
9668 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009669 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009670 mFakePolicy->setPointerCapture(false);
9671 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9672
9673 // uncaptured touchpad should be a pointer device
9674 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9675
9676 // captured touchpad should be a touchpad device
9677 mFakePolicy->setPointerCapture(true);
9678 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9679 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9680}
9681
HQ Liue6983c72022-04-19 22:14:56 +00009682class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9683protected:
9684 float mPointerMovementScale;
9685 float mPointerXZoomScale;
9686 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9687 addConfigurationProperty("touch.deviceType", "pointer");
9688 std::shared_ptr<FakePointerController> fakePointerController =
9689 std::make_shared<FakePointerController>();
9690 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9691 fakePointerController->setPosition(0, 0);
9692 fakePointerController->setButtonState(0);
9693 prepareDisplay(DISPLAY_ORIENTATION_0);
9694
9695 prepareAxes(POSITION);
9696 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9697 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9698 // needs to be disabled, and the pointer gesture needs to be enabled.
9699 mFakePolicy->setPointerCapture(false);
9700 mFakePolicy->setPointerGestureEnabled(true);
9701 mFakePolicy->setPointerController(fakePointerController);
9702
9703 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9704 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9705 mPointerMovementScale =
9706 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9707 mPointerXZoomScale =
9708 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9709 }
9710
9711 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9712 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9713 /*flat*/ 0,
9714 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9715 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9716 /*flat*/ 0,
9717 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9718 }
9719};
9720
9721/**
9722 * Two fingers down on a pointer mode touch pad. The width
9723 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9724 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9725 * be greater than the both value to be freeform gesture, so that after two
9726 * fingers start to move downwards, the gesture should be swipe.
9727 */
9728TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9729 // The min freeform gesture width is 25units/mm x 30mm = 750
9730 // which is greater than fraction of the diagnal length of the touchpad (349).
9731 // Thus, MaxSwipWidth is 750.
9732 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9733 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9734 NotifyMotionArgs motionArgs;
9735
9736 // Two fingers down at once.
9737 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9738 // Pointer's initial position is used the [0,0] coordinate.
9739 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9740
9741 processId(mapper, FIRST_TRACKING_ID);
9742 processPosition(mapper, x1, y1);
9743 processMTSync(mapper);
9744 processId(mapper, SECOND_TRACKING_ID);
9745 processPosition(mapper, x2, y2);
9746 processMTSync(mapper);
9747 processSync(mapper);
9748
9749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9750 ASSERT_EQ(1U, motionArgs.pointerCount);
9751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009753 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009754 ASSERT_NO_FATAL_FAILURE(
9755 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9756
9757 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9758 // that there should be 1 pointer.
9759 int32_t movingDistance = 200;
9760 y1 += movingDistance;
9761 y2 += movingDistance;
9762
9763 processId(mapper, FIRST_TRACKING_ID);
9764 processPosition(mapper, x1, y1);
9765 processMTSync(mapper);
9766 processId(mapper, SECOND_TRACKING_ID);
9767 processPosition(mapper, x2, y2);
9768 processMTSync(mapper);
9769 processSync(mapper);
9770
9771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9772 ASSERT_EQ(1U, motionArgs.pointerCount);
9773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009775 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9777 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9778 0, 0, 0, 0));
9779}
9780
9781/**
9782 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9783 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9784 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9785 * value to be freeform gesture, so that after two fingers start to move downwards,
9786 * the gesture should be swipe.
9787 */
9788TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9789 // The min freeform gesture width is 5units/mm x 30mm = 150
9790 // which is greater than fraction of the diagnal length of the touchpad (349).
9791 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9792 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9793 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9794 NotifyMotionArgs motionArgs;
9795
9796 // Two fingers down at once.
9797 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9798 // Pointer's initial position is used the [0,0] coordinate.
9799 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9800
9801 processId(mapper, FIRST_TRACKING_ID);
9802 processPosition(mapper, x1, y1);
9803 processMTSync(mapper);
9804 processId(mapper, SECOND_TRACKING_ID);
9805 processPosition(mapper, x2, y2);
9806 processMTSync(mapper);
9807 processSync(mapper);
9808
9809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9810 ASSERT_EQ(1U, motionArgs.pointerCount);
9811 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009813 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009814 ASSERT_NO_FATAL_FAILURE(
9815 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9816
9817 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9818 // and there should be 1 pointer.
9819 int32_t movingDistance = 200;
9820 y1 += movingDistance;
9821 y2 += movingDistance;
9822
9823 processId(mapper, FIRST_TRACKING_ID);
9824 processPosition(mapper, x1, y1);
9825 processMTSync(mapper);
9826 processId(mapper, SECOND_TRACKING_ID);
9827 processPosition(mapper, x2, y2);
9828 processMTSync(mapper);
9829 processSync(mapper);
9830
9831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9832 ASSERT_EQ(1U, motionArgs.pointerCount);
9833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009835 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009836 // New coordinate is the scaled relative coordinate from the initial coordinate.
9837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9838 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9839 0, 0, 0, 0));
9840}
9841
9842/**
9843 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9844 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9845 * freeform gestures after two fingers start to move downwards.
9846 */
9847TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9848 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9849 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9850
9851 NotifyMotionArgs motionArgs;
9852
9853 // Two fingers down at once. Wider than the max swipe width.
9854 // The gesture is expected to be PRESS, then transformed to FREEFORM
9855 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9856
9857 processId(mapper, FIRST_TRACKING_ID);
9858 processPosition(mapper, x1, y1);
9859 processMTSync(mapper);
9860 processId(mapper, SECOND_TRACKING_ID);
9861 processPosition(mapper, x2, y2);
9862 processMTSync(mapper);
9863 processSync(mapper);
9864
9865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9866 ASSERT_EQ(1U, motionArgs.pointerCount);
9867 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009869 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009870 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9871 ASSERT_NO_FATAL_FAILURE(
9872 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9873
9874 int32_t movingDistance = 200;
9875
9876 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9877 // then two down events for two pointers.
9878 y1 += movingDistance;
9879 y2 += movingDistance;
9880
9881 processId(mapper, FIRST_TRACKING_ID);
9882 processPosition(mapper, x1, y1);
9883 processMTSync(mapper);
9884 processId(mapper, SECOND_TRACKING_ID);
9885 processPosition(mapper, x2, y2);
9886 processMTSync(mapper);
9887 processSync(mapper);
9888
9889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9890 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9891 ASSERT_EQ(1U, motionArgs.pointerCount);
9892 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9894 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9895 ASSERT_EQ(1U, motionArgs.pointerCount);
9896 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009899 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009900 ASSERT_EQ(2U, motionArgs.pointerCount);
9901 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009903 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009904 // Two pointers' scaled relative coordinates from their initial centroid.
9905 // Initial y coordinates are 0 as y1 and y2 have the same value.
9906 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9907 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9908 // When pointers move, the new coordinates equal to the initial coordinates plus
9909 // scaled moving distance.
9910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9911 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9912 0, 0, 0, 0));
9913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9914 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9915 0, 0, 0, 0));
9916
9917 // Move two fingers down again, expect one MOVE motion event.
9918 y1 += movingDistance;
9919 y2 += movingDistance;
9920
9921 processId(mapper, FIRST_TRACKING_ID);
9922 processPosition(mapper, x1, y1);
9923 processMTSync(mapper);
9924 processId(mapper, SECOND_TRACKING_ID);
9925 processPosition(mapper, x2, y2);
9926 processMTSync(mapper);
9927 processSync(mapper);
9928
9929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9930 ASSERT_EQ(2U, motionArgs.pointerCount);
9931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009933 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9935 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9936 0, 0, 0, 0, 0));
9937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9938 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9939 0, 0, 0, 0, 0));
9940}
9941
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009942// --- JoystickInputMapperTest ---
9943
9944class JoystickInputMapperTest : public InputMapperTest {
9945protected:
9946 static const int32_t RAW_X_MIN;
9947 static const int32_t RAW_X_MAX;
9948 static const int32_t RAW_Y_MIN;
9949 static const int32_t RAW_Y_MAX;
9950
9951 void SetUp() override {
9952 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9953 }
9954 void prepareAxes() {
9955 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9956 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9957 }
9958
9959 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9961 }
9962
9963 void processSync(JoystickInputMapper& mapper) {
9964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9965 }
9966
9967 void prepareVirtualDisplay(int32_t orientation) {
9968 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9969 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9970 NO_PORT, ViewportType::VIRTUAL);
9971 }
9972};
9973
9974const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9975const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9976const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9977const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9978
9979TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9980 prepareAxes();
9981 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9982
9983 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9984
9985 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9986
9987 // Send an axis event
9988 processAxis(mapper, ABS_X, 100);
9989 processSync(mapper);
9990
9991 NotifyMotionArgs args;
9992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9993 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9994
9995 // Send another axis event
9996 processAxis(mapper, ABS_Y, 100);
9997 processSync(mapper);
9998
9999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10000 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10001}
10002
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010003// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010004
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010005class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010006protected:
10007 static const char* DEVICE_NAME;
10008 static const char* DEVICE_LOCATION;
10009 static const int32_t DEVICE_ID;
10010 static const int32_t DEVICE_GENERATION;
10011 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010012 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010013 static const int32_t EVENTHUB_ID;
10014
10015 std::shared_ptr<FakeEventHub> mFakeEventHub;
10016 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010017 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010018 std::unique_ptr<InstrumentedInputReader> mReader;
10019 std::shared_ptr<InputDevice> mDevice;
10020
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010021 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010022 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010023 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010024 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010025 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010026 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010027 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10028 }
10029
10030 void SetUp() override { SetUp(DEVICE_CLASSES); }
10031
10032 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010033 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010034 mFakePolicy.clear();
10035 }
10036
10037 void configureDevice(uint32_t changes) {
10038 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10039 mReader->requestRefreshConfiguration(changes);
10040 mReader->loopOnce();
10041 }
10042 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
10043 }
10044
10045 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10046 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010047 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010048 InputDeviceIdentifier identifier;
10049 identifier.name = name;
10050 identifier.location = location;
10051 std::shared_ptr<InputDevice> device =
10052 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10053 identifier);
10054 mReader->pushNextDevice(device);
10055 mFakeEventHub->addDevice(eventHubId, name, classes);
10056 mReader->loopOnce();
10057 return device;
10058 }
10059
10060 template <class T, typename... Args>
10061 T& addControllerAndConfigure(Args... args) {
10062 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10063
10064 return controller;
10065 }
10066};
10067
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010068const char* PeripheralControllerTest::DEVICE_NAME = "device";
10069const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10070const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10071const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10072const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010073const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10074 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010075const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010076
10077// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010078class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010079protected:
10080 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010081 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010082 }
10083};
10084
10085TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010086 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010087
10088 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10089 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10090}
10091
10092TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010093 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010094
10095 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10096 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10097}
10098
10099// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010100class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010101protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010102 void SetUp() override {
10103 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10104 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010105};
10106
Chris Ye85758332021-05-16 23:05:17 -070010107TEST_F(LightControllerTest, MonoLight) {
10108 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010109 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010110 .maxBrightness = 255,
10111 .flags = InputLightClass::BRIGHTNESS,
10112 .path = ""};
10113 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010114
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010115 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010116 InputDeviceInfo info;
10117 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010118 std::vector<InputDeviceLightInfo> lights = info.getLights();
10119 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010120 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10121 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10122
10123 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10124 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10125}
10126
10127TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10128 RawLightInfo infoMono = {.id = 1,
10129 .name = "mono_keyboard_backlight",
10130 .maxBrightness = 255,
10131 .flags = InputLightClass::BRIGHTNESS |
10132 InputLightClass::KEYBOARD_BACKLIGHT,
10133 .path = ""};
10134 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10135
10136 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10137 InputDeviceInfo info;
10138 controller.populateDeviceInfo(&info);
10139 std::vector<InputDeviceLightInfo> lights = info.getLights();
10140 ASSERT_EQ(1U, lights.size());
10141 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10142 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010143
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010144 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10145 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010146}
10147
10148TEST_F(LightControllerTest, RGBLight) {
10149 RawLightInfo infoRed = {.id = 1,
10150 .name = "red",
10151 .maxBrightness = 255,
10152 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10153 .path = ""};
10154 RawLightInfo infoGreen = {.id = 2,
10155 .name = "green",
10156 .maxBrightness = 255,
10157 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10158 .path = ""};
10159 RawLightInfo infoBlue = {.id = 3,
10160 .name = "blue",
10161 .maxBrightness = 255,
10162 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10163 .path = ""};
10164 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10165 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10166 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10167
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010168 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010169 InputDeviceInfo info;
10170 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010171 std::vector<InputDeviceLightInfo> lights = info.getLights();
10172 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010173 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10174 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10175 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10176
10177 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10178 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10179}
10180
10181TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10182 RawLightInfo infoRed = {.id = 1,
10183 .name = "red_keyboard_backlight",
10184 .maxBrightness = 255,
10185 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10186 InputLightClass::KEYBOARD_BACKLIGHT,
10187 .path = ""};
10188 RawLightInfo infoGreen = {.id = 2,
10189 .name = "green_keyboard_backlight",
10190 .maxBrightness = 255,
10191 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10192 InputLightClass::KEYBOARD_BACKLIGHT,
10193 .path = ""};
10194 RawLightInfo infoBlue = {.id = 3,
10195 .name = "blue_keyboard_backlight",
10196 .maxBrightness = 255,
10197 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10198 InputLightClass::KEYBOARD_BACKLIGHT,
10199 .path = ""};
10200 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10201 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10202 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10203
10204 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10205 InputDeviceInfo info;
10206 controller.populateDeviceInfo(&info);
10207 std::vector<InputDeviceLightInfo> lights = info.getLights();
10208 ASSERT_EQ(1U, lights.size());
10209 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10210 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10211 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10212
10213 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10214 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10215}
10216
10217TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10218 RawLightInfo infoRed = {.id = 1,
10219 .name = "red",
10220 .maxBrightness = 255,
10221 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10222 .path = ""};
10223 RawLightInfo infoGreen = {.id = 2,
10224 .name = "green",
10225 .maxBrightness = 255,
10226 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10227 .path = ""};
10228 RawLightInfo infoBlue = {.id = 3,
10229 .name = "blue",
10230 .maxBrightness = 255,
10231 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10232 .path = ""};
10233 RawLightInfo infoGlobal = {.id = 3,
10234 .name = "global_keyboard_backlight",
10235 .maxBrightness = 255,
10236 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10237 InputLightClass::KEYBOARD_BACKLIGHT,
10238 .path = ""};
10239 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10240 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10241 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10242 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10243
10244 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10245 InputDeviceInfo info;
10246 controller.populateDeviceInfo(&info);
10247 std::vector<InputDeviceLightInfo> lights = info.getLights();
10248 ASSERT_EQ(1U, lights.size());
10249 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10250 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10251 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010252
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010253 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10254 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010255}
10256
10257TEST_F(LightControllerTest, MultiColorRGBLight) {
10258 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010259 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010260 .maxBrightness = 255,
10261 .flags = InputLightClass::BRIGHTNESS |
10262 InputLightClass::MULTI_INTENSITY |
10263 InputLightClass::MULTI_INDEX,
10264 .path = ""};
10265
10266 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10267
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010268 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010269 InputDeviceInfo info;
10270 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010271 std::vector<InputDeviceLightInfo> lights = info.getLights();
10272 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010273 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10274 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10275 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10276
10277 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10278 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10279}
10280
10281TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10282 RawLightInfo infoColor = {.id = 1,
10283 .name = "multi_color_keyboard_backlight",
10284 .maxBrightness = 255,
10285 .flags = InputLightClass::BRIGHTNESS |
10286 InputLightClass::MULTI_INTENSITY |
10287 InputLightClass::MULTI_INDEX |
10288 InputLightClass::KEYBOARD_BACKLIGHT,
10289 .path = ""};
10290
10291 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10292
10293 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10294 InputDeviceInfo info;
10295 controller.populateDeviceInfo(&info);
10296 std::vector<InputDeviceLightInfo> lights = info.getLights();
10297 ASSERT_EQ(1U, lights.size());
10298 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10299 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10300 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010301
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010302 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10303 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010304}
10305
10306TEST_F(LightControllerTest, PlayerIdLight) {
10307 RawLightInfo info1 = {.id = 1,
10308 .name = "player1",
10309 .maxBrightness = 255,
10310 .flags = InputLightClass::BRIGHTNESS,
10311 .path = ""};
10312 RawLightInfo info2 = {.id = 2,
10313 .name = "player2",
10314 .maxBrightness = 255,
10315 .flags = InputLightClass::BRIGHTNESS,
10316 .path = ""};
10317 RawLightInfo info3 = {.id = 3,
10318 .name = "player3",
10319 .maxBrightness = 255,
10320 .flags = InputLightClass::BRIGHTNESS,
10321 .path = ""};
10322 RawLightInfo info4 = {.id = 4,
10323 .name = "player4",
10324 .maxBrightness = 255,
10325 .flags = InputLightClass::BRIGHTNESS,
10326 .path = ""};
10327 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10328 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10329 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10330 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10331
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010332 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010333 InputDeviceInfo info;
10334 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010335 std::vector<InputDeviceLightInfo> lights = info.getLights();
10336 ASSERT_EQ(1U, lights.size());
10337 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010338 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10339 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010340
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010341 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10342 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10343 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010344}
10345
Michael Wrightd02c5b62014-02-10 15:10:22 -080010346} // namespace android