blob: 8ac8dfc3ac2f68a5f38cf3eebeb759b4bfb5790a [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
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001193 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
1194 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001195 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001197
1198 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001199 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001200 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1201 mViewport = config->getDisplayViewportByPort(*displayPort);
1202 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001203
1204 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001205 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 }
1207
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001208 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001209 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001211 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001212 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213 }
1214
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001215 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001216 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217 mLastEvent = *rawEvent;
1218 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001219 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001220 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 }
1222
Chris Yea52ade12020-08-27 16:49:20 -07001223 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1225 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1226 }
1227
Philip Junker4af3b3d2021-12-14 10:36:55 +01001228 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1229 auto it = mKeyCodeMapping.find(locationKeyCode);
1230 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1231 }
1232
Chris Yea52ade12020-08-27 16:49:20 -07001233 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1235 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1236 }
1237
Chris Yea52ade12020-08-27 16:49:20 -07001238 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1240 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1241 }
1242
Chris Yea52ade12020-08-27 16:49:20 -07001243 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001244 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001245 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001246 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1248 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1249 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 }
1251 }
1252 }
Chris Yea52ade12020-08-27 16:49:20 -07001253 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254 return result;
1255 }
1256
1257 virtual int32_t getMetaState() {
1258 return mMetaState;
1259 }
1260
1261 virtual void fadePointer() {
1262 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001263
1264 virtual std::optional<int32_t> getAssociatedDisplay() {
1265 if (mViewport) {
1266 return std::make_optional(mViewport->displayId);
1267 }
1268 return std::nullopt;
1269 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270};
1271
1272
1273// --- InstrumentedInputReader ---
1274
1275class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001276 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277
1278public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001279 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1280 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001281 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001282 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001284 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001285
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001286 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001287
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001288 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001289 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290 InputDeviceIdentifier identifier;
1291 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001292 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001293 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001294 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295 }
1296
Prabir Pradhan28efc192019-11-05 01:10:04 +00001297 // Make the protected loopOnce method accessible to tests.
1298 using InputReader::loopOnce;
1299
Michael Wrightd02c5b62014-02-10 15:10:22 -08001300protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001301 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1302 const InputDeviceIdentifier& identifier)
1303 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001304 if (!mNextDevices.empty()) {
1305 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1306 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001307 return device;
1308 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001309 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001310 }
1311
arthurhungdcef2dc2020-08-11 14:47:50 +08001312 // --- FakeInputReaderContext ---
1313 class FakeInputReaderContext : public ContextImpl {
1314 int32_t mGlobalMetaState;
1315 bool mUpdateGlobalMetaStateWasCalled;
1316 int32_t mGeneration;
1317
1318 public:
1319 FakeInputReaderContext(InputReader* reader)
1320 : ContextImpl(reader),
1321 mGlobalMetaState(0),
1322 mUpdateGlobalMetaStateWasCalled(false),
1323 mGeneration(1) {}
1324
1325 virtual ~FakeInputReaderContext() {}
1326
1327 void assertUpdateGlobalMetaStateWasCalled() {
1328 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1329 << "Expected updateGlobalMetaState() to have been called.";
1330 mUpdateGlobalMetaStateWasCalled = false;
1331 }
1332
1333 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1334
1335 uint32_t getGeneration() { return mGeneration; }
1336
1337 void updateGlobalMetaState() override {
1338 mUpdateGlobalMetaStateWasCalled = true;
1339 ContextImpl::updateGlobalMetaState();
1340 }
1341
1342 int32_t getGlobalMetaState() override {
1343 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1344 }
1345
1346 int32_t bumpGeneration() override {
1347 mGeneration = ContextImpl::bumpGeneration();
1348 return mGeneration;
1349 }
1350 } mFakeContext;
1351
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001353
1354public:
1355 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001356};
1357
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358// --- InputReaderPolicyTest ---
1359class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001360protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361 sp<FakeInputReaderPolicy> mFakePolicy;
1362
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001363 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001364 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365};
1366
1367/**
1368 * Check that empty set of viewports is an acceptable configuration.
1369 * Also try to get internal viewport two different ways - by type and by uniqueId.
1370 *
1371 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1372 * Such configuration is not currently allowed.
1373 */
1374TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001375 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001376
1377 // We didn't add any viewports yet, so there shouldn't be any.
1378 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001379 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001380 ASSERT_FALSE(internalViewport);
1381
1382 // Add an internal viewport, then clear it
1383 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001384 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001385 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386
1387 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001388 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001390 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391
1392 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001393 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001394 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001395 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396
1397 mFakePolicy->clearViewports();
1398 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001399 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001400 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001401 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001402 ASSERT_FALSE(internalViewport);
1403}
1404
1405TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1406 const std::string internalUniqueId = "local:0";
1407 const std::string externalUniqueId = "local:1";
1408 const std::string virtualUniqueId1 = "virtual:2";
1409 const std::string virtualUniqueId2 = "virtual:3";
1410 constexpr int32_t virtualDisplayId1 = 2;
1411 constexpr int32_t virtualDisplayId2 = 3;
1412
1413 // Add an internal viewport
1414 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1416 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 // Add an external viewport
1418 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001419 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1420 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 // Add an virtual viewport
1422 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001423 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1424 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425 // Add another virtual viewport
1426 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001427 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1428 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001429
1430 // Check matching by type for internal
1431 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001432 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001433 ASSERT_TRUE(internalViewport);
1434 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1435
1436 // Check matching by type for external
1437 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001438 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001439 ASSERT_TRUE(externalViewport);
1440 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1441
1442 // Check matching by uniqueId for virtual viewport #1
1443 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001444 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001446 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001447 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1448 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1449
1450 // Check matching by uniqueId for virtual viewport #2
1451 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001452 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001453 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001454 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1456 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1457}
1458
1459
1460/**
1461 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1462 * that lookup works by checking display id.
1463 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1464 */
1465TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1466 const std::string uniqueId1 = "uniqueId1";
1467 const std::string uniqueId2 = "uniqueId2";
1468 constexpr int32_t displayId1 = 2;
1469 constexpr int32_t displayId2 = 3;
1470
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001471 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1472 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001473 for (const ViewportType& type : types) {
1474 mFakePolicy->clearViewports();
1475 // Add a viewport
1476 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001477 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1478 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001479 // Add another viewport
1480 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001481 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1482 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001483
1484 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001485 std::optional<DisplayViewport> viewport1 =
1486 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001487 ASSERT_TRUE(viewport1);
1488 ASSERT_EQ(displayId1, viewport1->displayId);
1489 ASSERT_EQ(type, viewport1->type);
1490
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001491 std::optional<DisplayViewport> viewport2 =
1492 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001493 ASSERT_TRUE(viewport2);
1494 ASSERT_EQ(displayId2, viewport2->displayId);
1495 ASSERT_EQ(type, viewport2->type);
1496
1497 // When there are multiple viewports of the same kind, and uniqueId is not specified
1498 // in the call to getDisplayViewport, then that situation is not supported.
1499 // The viewports can be stored in any order, so we cannot rely on the order, since that
1500 // is just implementation detail.
1501 // However, we can check that it still returns *a* viewport, we just cannot assert
1502 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001503 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001504 ASSERT_TRUE(someViewport);
1505 }
1506}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001507
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001508/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001509 * When we have multiple internal displays make sure we always return the default display when
1510 * querying by type.
1511 */
1512TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1513 const std::string uniqueId1 = "uniqueId1";
1514 const std::string uniqueId2 = "uniqueId2";
1515 constexpr int32_t nonDefaultDisplayId = 2;
1516 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1517 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1518
1519 // Add the default display first and ensure it gets returned.
1520 mFakePolicy->clearViewports();
1521 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001522 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001523 ViewportType::INTERNAL);
1524 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001525 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001526 ViewportType::INTERNAL);
1527
1528 std::optional<DisplayViewport> viewport =
1529 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1530 ASSERT_TRUE(viewport);
1531 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1532 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1533
1534 // Add the default display second to make sure order doesn't matter.
1535 mFakePolicy->clearViewports();
1536 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001538 ViewportType::INTERNAL);
1539 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001540 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001541 ViewportType::INTERNAL);
1542
1543 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1544 ASSERT_TRUE(viewport);
1545 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1546 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1547}
1548
1549/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001550 * Check getDisplayViewportByPort
1551 */
1552TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001553 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001554 const std::string uniqueId1 = "uniqueId1";
1555 const std::string uniqueId2 = "uniqueId2";
1556 constexpr int32_t displayId1 = 1;
1557 constexpr int32_t displayId2 = 2;
1558 const uint8_t hdmi1 = 0;
1559 const uint8_t hdmi2 = 1;
1560 const uint8_t hdmi3 = 2;
1561
1562 mFakePolicy->clearViewports();
1563 // Add a viewport that's associated with some display port that's not of interest.
1564 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001565 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1566 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001567 // Add another viewport, connected to HDMI1 port
1568 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001569 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1570 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001571
1572 // Check that correct display viewport was returned by comparing the display ports.
1573 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1574 ASSERT_TRUE(hdmi1Viewport);
1575 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1576 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1577
1578 // Check that we can still get the same viewport using the uniqueId
1579 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1580 ASSERT_TRUE(hdmi1Viewport);
1581 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1582 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1583 ASSERT_EQ(type, hdmi1Viewport->type);
1584
1585 // Check that we cannot find a port with "HDMI2", because we never added one
1586 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1587 ASSERT_FALSE(hdmi2Viewport);
1588}
1589
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590// --- InputReaderTest ---
1591
1592class InputReaderTest : public testing::Test {
1593protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001594 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001596 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001597 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598
Chris Yea52ade12020-08-27 16:49:20 -07001599 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001600 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001601 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001602 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001603
Prabir Pradhan28efc192019-11-05 01:10:04 +00001604 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001605 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 }
1607
Chris Yea52ade12020-08-27 16:49:20 -07001608 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001609 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001610 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001611 }
1612
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001613 void addDevice(int32_t eventHubId, const std::string& name,
1614 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616
1617 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001618 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619 }
1620 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001621 mReader->loopOnce();
1622 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001623 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1624 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 }
1626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001628 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001629 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001630 }
1631
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001632 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001633 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001634 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001635 }
1636
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001637 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001638 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001639 ftl::Flags<InputDeviceClass> classes,
1640 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001641 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1643 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001644 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001645 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646 return mapper;
1647 }
1648};
1649
Chris Ye98d3f532020-10-01 21:48:59 -07001650TEST_F(InputReaderTest, PolicyGetInputDevices) {
1651 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001652 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001653 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654
1655 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001656 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001659 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1661 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001662 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001663}
1664
Chris Yee7310032020-09-22 15:36:28 -07001665TEST_F(InputReaderTest, GetMergedInputDevices) {
1666 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1667 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1668 // Add two subdevices to device
1669 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1670 // Must add at least one mapper or the device will be ignored!
1671 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1672 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1673
1674 // Push same device instance for next device to be added, so they'll have same identifier.
1675 mReader->pushNextDevice(device);
1676 mReader->pushNextDevice(device);
1677 ASSERT_NO_FATAL_FAILURE(
1678 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1679 ASSERT_NO_FATAL_FAILURE(
1680 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1681
1682 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001683 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001684}
1685
Chris Yee14523a2020-12-19 13:46:00 -08001686TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1687 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1688 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1689 // Add two subdevices to device
1690 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1691 // Must add at least one mapper or the device will be ignored!
1692 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1693 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1694
1695 // Push same device instance for next device to be added, so they'll have same identifier.
1696 mReader->pushNextDevice(device);
1697 mReader->pushNextDevice(device);
1698 // Sensor device is initially disabled
1699 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1700 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1701 nullptr));
1702 // Device is disabled because the only sub device is a sensor device and disabled initially.
1703 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1704 ASSERT_FALSE(device->isEnabled());
1705 ASSERT_NO_FATAL_FAILURE(
1706 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1707 // The merged device is enabled if any sub device is enabled
1708 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1709 ASSERT_TRUE(device->isEnabled());
1710}
1711
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001712TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001714 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001715 constexpr int32_t eventHubId = 1;
1716 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001719 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001721
Yi Kong9b14ac62018-07-17 13:48:38 -07001722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001723
1724 NotifyDeviceResetArgs resetArgs;
1725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001726 ASSERT_EQ(deviceId, resetArgs.deviceId);
1727
1728 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001729 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001730 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001731
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001733 ASSERT_EQ(deviceId, resetArgs.deviceId);
1734 ASSERT_EQ(device->isEnabled(), false);
1735
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001736 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001737 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001740 ASSERT_EQ(device->isEnabled(), false);
1741
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001742 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001743 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001745 ASSERT_EQ(deviceId, resetArgs.deviceId);
1746 ASSERT_EQ(device->isEnabled(), true);
1747}
1748
Michael Wrightd02c5b62014-02-10 15:10:22 -08001749TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001750 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001751 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001752 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001753 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001754 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001755 AINPUT_SOURCE_KEYBOARD, nullptr);
1756 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001757
1758 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1759 AINPUT_SOURCE_ANY, AKEYCODE_A))
1760 << "Should return unknown when the device id is >= 0 but unknown.";
1761
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001762 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1763 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1764 << "Should return unknown when the device id is valid but the sources are not "
1765 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001766
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001767 ASSERT_EQ(AKEY_STATE_DOWN,
1768 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1769 AKEYCODE_A))
1770 << "Should return value provided by mapper when device id is valid and the device "
1771 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001772
1773 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1774 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1775 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1776
1777 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1778 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1779 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1780}
1781
Philip Junker4af3b3d2021-12-14 10:36:55 +01001782TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1783 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1784 constexpr int32_t eventHubId = 1;
1785 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1786 InputDeviceClass::KEYBOARD,
1787 AINPUT_SOURCE_KEYBOARD, nullptr);
1788 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1789
1790 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1791 << "Should return unknown when the device with the specified id is not found.";
1792
1793 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1794 << "Should return correct mapping when device id is valid and mapping exists.";
1795
1796 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1797 << "Should return the location key code when device id is valid and there's no "
1798 "mapping.";
1799}
1800
1801TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1802 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1803 constexpr int32_t eventHubId = 1;
1804 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1805 InputDeviceClass::JOYSTICK,
1806 AINPUT_SOURCE_GAMEPAD, nullptr);
1807 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1808
1809 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1810 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1811}
1812
Michael Wrightd02c5b62014-02-10 15:10:22 -08001813TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001815 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001817 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001818 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001819 AINPUT_SOURCE_KEYBOARD, nullptr);
1820 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001821
1822 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1823 AINPUT_SOURCE_ANY, KEY_A))
1824 << "Should return unknown when the device id is >= 0 but unknown.";
1825
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001826 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1827 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1828 << "Should return unknown when the device id is valid but the sources are not "
1829 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001831 ASSERT_EQ(AKEY_STATE_DOWN,
1832 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1833 KEY_A))
1834 << "Should return value provided by mapper when device id is valid and the device "
1835 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001836
1837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1838 AINPUT_SOURCE_TRACKBALL, KEY_A))
1839 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1840
1841 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1842 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1843 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1844}
1845
1846TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001848 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001850 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001852 AINPUT_SOURCE_KEYBOARD, nullptr);
1853 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001854
1855 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1856 AINPUT_SOURCE_ANY, SW_LID))
1857 << "Should return unknown when the device id is >= 0 but unknown.";
1858
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001859 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1860 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1861 << "Should return unknown when the device id is valid but the sources are not "
1862 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001864 ASSERT_EQ(AKEY_STATE_DOWN,
1865 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1866 SW_LID))
1867 << "Should return value provided by mapper when device id is valid and the device "
1868 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869
1870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1871 AINPUT_SOURCE_TRACKBALL, SW_LID))
1872 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1873
1874 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1875 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1876 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1877}
1878
1879TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001880 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001881 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001882 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001883 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001884 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001885 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001887 mapper.addSupportedKeyCode(AKEYCODE_A);
1888 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001890 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001891 uint8_t flags[4] = { 0, 0, 0, 1 };
1892
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001893 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894 << "Should return false when device id is >= 0 but unknown.";
1895 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1896
1897 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001898 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001899 << "Should return false when device id is valid but the sources are not supported by "
1900 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001901 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1902
1903 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001904 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001905 keyCodes, flags))
1906 << "Should return value provided by mapper when device id is valid and the device "
1907 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1909
1910 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001911 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1912 << "Should return false when the device id is < 0 but the sources are not supported by "
1913 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1915
1916 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001917 ASSERT_TRUE(
1918 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1919 << "Should return value provided by mapper when device id is < 0 and one of the "
1920 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001921 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1922}
1923
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001924TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001925 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001926 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001927
1928 NotifyConfigurationChangedArgs args;
1929
1930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1931 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1932}
1933
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001934TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001935 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001936 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001937 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001939 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001940 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001941 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001942 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001944 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001945 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001946 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1947
1948 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001949 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001950 ASSERT_EQ(when, event.when);
1951 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001952 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001953 ASSERT_EQ(EV_KEY, event.type);
1954 ASSERT_EQ(KEY_A, event.code);
1955 ASSERT_EQ(1, event.value);
1956}
1957
Garfield Tan1c7bc862020-01-28 13:24:04 -08001958TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001959 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001960 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001961 constexpr int32_t eventHubId = 1;
1962 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001963 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001964 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001965 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001966 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001967
1968 NotifyDeviceResetArgs resetArgs;
1969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001970 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001972 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001973 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001975 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001976 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001978 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001979 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001981 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001982 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001984 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001985 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001987 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001988 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001989}
1990
Garfield Tan1c7bc862020-01-28 13:24:04 -08001991TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1992 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001993 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001994 constexpr int32_t eventHubId = 1;
1995 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1996 // Must add at least one mapper or the device will be ignored!
1997 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001998 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001999 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2000
2001 NotifyDeviceResetArgs resetArgs;
2002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2003 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2004}
2005
Arthur Hungc23540e2018-11-29 20:42:11 +08002006TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002007 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002008 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002009 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002010 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2012 FakeInputMapper& mapper =
2013 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002014 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002015
2016 const uint8_t hdmi1 = 1;
2017
2018 // Associated touch screen with second display.
2019 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2020
2021 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002022 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002023 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002024 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002025 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002026 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002027 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002028 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002029 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002030 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002031
2032 // Add the device, and make sure all of the callbacks are triggered.
2033 // The device is added after the input port associations are processed since
2034 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002035 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002038 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002039
Arthur Hung2c9a3342019-07-23 14:18:59 +08002040 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002041 ASSERT_EQ(deviceId, device->getId());
2042 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2043 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002044
2045 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002046 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002047 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002048 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002049}
2050
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002051TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2052 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002053 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002054 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2055 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2056 // Must add at least one mapper or the device will be ignored!
2057 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2058 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2059 mReader->pushNextDevice(device);
2060 mReader->pushNextDevice(device);
2061 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2062 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2063
2064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2065
2066 NotifyDeviceResetArgs resetArgs;
2067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2068 ASSERT_EQ(deviceId, resetArgs.deviceId);
2069 ASSERT_TRUE(device->isEnabled());
2070 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2071 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2072
2073 disableDevice(deviceId);
2074 mReader->loopOnce();
2075
2076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2077 ASSERT_EQ(deviceId, resetArgs.deviceId);
2078 ASSERT_FALSE(device->isEnabled());
2079 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2080 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2081
2082 enableDevice(deviceId);
2083 mReader->loopOnce();
2084
2085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2086 ASSERT_EQ(deviceId, resetArgs.deviceId);
2087 ASSERT_TRUE(device->isEnabled());
2088 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2090}
2091
2092TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2093 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002094 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002095 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2096 // Add two subdevices to device
2097 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2098 FakeInputMapper& mapperDevice1 =
2099 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2100 FakeInputMapper& mapperDevice2 =
2101 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2102 mReader->pushNextDevice(device);
2103 mReader->pushNextDevice(device);
2104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2105 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2106
2107 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2108 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2109
2110 ASSERT_EQ(AKEY_STATE_DOWN,
2111 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2112 ASSERT_EQ(AKEY_STATE_DOWN,
2113 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2114 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2115 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2116}
2117
Prabir Pradhan7e186182020-11-10 13:56:45 -08002118TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2119 NotifyPointerCaptureChangedArgs args;
2120
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002121 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002122 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2123 mReader->loopOnce();
2124 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002125 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2126 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002127
2128 mFakePolicy->setPointerCapture(false);
2129 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2130 mReader->loopOnce();
2131 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002132 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002133
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002134 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002135 // does not change.
2136 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2137 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002138 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002139}
2140
Chris Ye87143712020-11-10 05:05:58 +00002141class FakeVibratorInputMapper : public FakeInputMapper {
2142public:
2143 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2144 : FakeInputMapper(deviceContext, sources) {}
2145
2146 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2147};
2148
2149TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2150 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002151 ftl::Flags<InputDeviceClass> deviceClass =
2152 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002153 constexpr int32_t eventHubId = 1;
2154 const char* DEVICE_LOCATION = "BLUETOOTH";
2155 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2156 FakeVibratorInputMapper& mapper =
2157 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2158 mReader->pushNextDevice(device);
2159
2160 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2161 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2162
2163 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2164 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2165}
2166
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002167// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002168
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002169class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002170public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002171 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002172
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002173 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002174
Andy Chenf9f1a022022-08-29 20:07:10 -04002175 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2176
Chris Yee2b1e5c2021-03-10 22:45:12 -08002177 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2178
2179 void dump(std::string& dump) override {}
2180
2181 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2182 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002183 }
2184
Chris Yee2b1e5c2021-03-10 22:45:12 -08002185 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2186 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002187 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002188
2189 bool setLightColor(int32_t lightId, int32_t color) override {
2190 getDeviceContext().setLightBrightness(lightId, color >> 24);
2191 return true;
2192 }
2193
2194 std::optional<int32_t> getLightColor(int32_t lightId) override {
2195 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2196 if (!result.has_value()) {
2197 return std::nullopt;
2198 }
2199 return result.value() << 24;
2200 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002201
2202 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2203
2204 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2205
2206private:
2207 InputDeviceContext& mDeviceContext;
2208 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2209 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002210 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002211};
2212
Chris Yee2b1e5c2021-03-10 22:45:12 -08002213TEST_F(InputReaderTest, BatteryGetCapacity) {
2214 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002215 ftl::Flags<InputDeviceClass> deviceClass =
2216 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002217 constexpr int32_t eventHubId = 1;
2218 const char* DEVICE_LOCATION = "BLUETOOTH";
2219 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002220 FakePeripheralController& controller =
2221 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002222 mReader->pushNextDevice(device);
2223
2224 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2225
2226 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2227 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2228}
2229
2230TEST_F(InputReaderTest, BatteryGetStatus) {
2231 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002232 ftl::Flags<InputDeviceClass> deviceClass =
2233 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002234 constexpr int32_t eventHubId = 1;
2235 const char* DEVICE_LOCATION = "BLUETOOTH";
2236 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002237 FakePeripheralController& controller =
2238 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002239 mReader->pushNextDevice(device);
2240
2241 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2242
2243 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2244 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2245}
2246
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002247TEST_F(InputReaderTest, BatteryGetDevicePath) {
2248 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2249 ftl::Flags<InputDeviceClass> deviceClass =
2250 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2251 constexpr int32_t eventHubId = 1;
2252 const char* DEVICE_LOCATION = "BLUETOOTH";
2253 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2254 device->addController<FakePeripheralController>(eventHubId);
2255 mReader->pushNextDevice(device);
2256
2257 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2258
2259 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2260}
2261
Chris Ye3fdbfef2021-01-06 18:45:18 -08002262TEST_F(InputReaderTest, LightGetColor) {
2263 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002264 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002265 constexpr int32_t eventHubId = 1;
2266 const char* DEVICE_LOCATION = "BLUETOOTH";
2267 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002268 FakePeripheralController& controller =
2269 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002270 mReader->pushNextDevice(device);
2271 RawLightInfo info = {.id = 1,
2272 .name = "Mono",
2273 .maxBrightness = 255,
2274 .flags = InputLightClass::BRIGHTNESS,
2275 .path = ""};
2276 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2277 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2278
2279 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002280
Chris Yee2b1e5c2021-03-10 22:45:12 -08002281 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2282 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002283 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2284 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2285}
2286
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002287// --- InputReaderIntegrationTest ---
2288
2289// These tests create and interact with the InputReader only through its interface.
2290// The InputReader is started during SetUp(), which starts its processing in its own
2291// thread. The tests use linux uinput to emulate input devices.
2292// NOTE: Interacting with the physical device while these tests are running may cause
2293// the tests to fail.
2294class InputReaderIntegrationTest : public testing::Test {
2295protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002296 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002297 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002298 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002299
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002300 std::shared_ptr<FakePointerController> mFakePointerController;
2301
Chris Yea52ade12020-08-27 16:49:20 -07002302 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002303 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002304 mFakePointerController = std::make_shared<FakePointerController>();
2305 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002306 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2307 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002308
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002309 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2310 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002311 ASSERT_EQ(mReader->start(), OK);
2312
2313 // Since this test is run on a real device, all the input devices connected
2314 // to the test device will show up in mReader. We wait for those input devices to
2315 // show up before beginning the tests.
2316 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2318 }
2319
Chris Yea52ade12020-08-27 16:49:20 -07002320 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002321 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002322 mReader.reset();
2323 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002324 mFakePolicy.clear();
2325 }
2326};
2327
2328TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2329 // An invalid input device that is only used for this test.
2330 class InvalidUinputDevice : public UinputDevice {
2331 public:
2332 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2333
2334 private:
2335 void configureDevice(int fd, uinput_user_dev* device) override {}
2336 };
2337
2338 const size_t numDevices = mFakePolicy->getInputDevices().size();
2339
2340 // UinputDevice does not set any event or key bits, so InputReader should not
2341 // consider it as a valid device.
2342 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2343 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2344 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2345 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2346
2347 invalidDevice.reset();
2348 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2350 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2351}
2352
2353TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2354 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2355
2356 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2357 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2359 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2360
2361 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002362 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002363 const auto& it =
2364 std::find_if(inputDevices.begin(), inputDevices.end(),
2365 [&keyboard](const InputDeviceInfo& info) {
2366 return info.getIdentifier().name == keyboard->getName();
2367 });
2368
2369 ASSERT_NE(it, inputDevices.end());
2370 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2371 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2372 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002373
2374 keyboard.reset();
2375 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2377 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2378}
2379
2380TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2381 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2382 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2383
2384 NotifyConfigurationChangedArgs configChangedArgs;
2385 ASSERT_NO_FATAL_FAILURE(
2386 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002387 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002388 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2389
2390 NotifyKeyArgs keyArgs;
2391 keyboard->pressAndReleaseHomeKey();
2392 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2393 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002394 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002395 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002396 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002397 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002398 prevTimestamp = keyArgs.eventTime;
2399
2400 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002402 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002403 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002404 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002405}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002407/**
2408 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2409 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2410 * are passed to the listener.
2411 */
2412static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2413TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2414 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2415 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2416 NotifyKeyArgs keyArgs;
2417
2418 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2420 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2421 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2422
2423 controller->pressAndReleaseKey(BTN_GEAR_UP);
2424 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2426 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2427}
2428
Arthur Hungaab25622020-01-16 11:22:11 +08002429// --- TouchProcessTest ---
2430class TouchIntegrationTest : public InputReaderIntegrationTest {
2431protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002432 const std::string UNIQUE_ID = "local:0";
2433
Chris Yea52ade12020-08-27 16:49:20 -07002434 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002435 InputReaderIntegrationTest::SetUp();
2436 // At least add an internal display.
2437 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2438 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002439 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002440
2441 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2442 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2444 }
2445
2446 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2447 int32_t orientation, const std::string& uniqueId,
2448 std::optional<uint8_t> physicalPort,
2449 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002450 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2451 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002452 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2453 }
2454
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002455 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2456 NotifyMotionArgs args;
2457 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2458 EXPECT_EQ(action, args.action);
2459 ASSERT_EQ(points.size(), args.pointerCount);
2460 for (size_t i = 0; i < args.pointerCount; i++) {
2461 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2462 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2463 }
2464 }
2465
Arthur Hungaab25622020-01-16 11:22:11 +08002466 std::unique_ptr<UinputTouchScreen> mDevice;
2467};
2468
2469TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2470 NotifyMotionArgs args;
2471 const Point centerPoint = mDevice->getCenterPoint();
2472
2473 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002474 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002475 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2479
2480 // ACTION_MOVE
2481 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002482 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002483 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2485
2486 // ACTION_UP
2487 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002488 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2490 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2491}
2492
2493TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2494 NotifyMotionArgs args;
2495 const Point centerPoint = mDevice->getCenterPoint();
2496
2497 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002498 mDevice->sendSlot(FIRST_SLOT);
2499 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002500 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002501 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002502 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2503 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2504
2505 // ACTION_POINTER_DOWN (Second slot)
2506 const Point secondPoint = centerPoint + Point(100, 100);
2507 mDevice->sendSlot(SECOND_SLOT);
2508 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002509 mDevice->sendDown(secondPoint);
2510 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002511 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002512 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002513
2514 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002515 mDevice->sendMove(secondPoint + Point(1, 1));
2516 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2519
2520 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002521 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002522 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002523 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002524 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002525
2526 // ACTION_UP
2527 mDevice->sendSlot(FIRST_SLOT);
2528 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002529 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002530 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2531 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2532}
2533
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002534/**
2535 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2536 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2537 * data?
2538 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2539 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2540 * for Pointer 0 only is generated after.
2541 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2542 * events, we will not miss any information.
2543 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2544 * event generated afterwards that contains the newest movement of pointer 0.
2545 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2546 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2547 * losing information about non-palm pointers.
2548 */
2549TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2550 NotifyMotionArgs args;
2551 const Point centerPoint = mDevice->getCenterPoint();
2552
2553 // ACTION_DOWN
2554 mDevice->sendSlot(FIRST_SLOT);
2555 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2556 mDevice->sendDown(centerPoint);
2557 mDevice->sendSync();
2558 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2559
2560 // ACTION_POINTER_DOWN (Second slot)
2561 const Point secondPoint = centerPoint + Point(100, 100);
2562 mDevice->sendSlot(SECOND_SLOT);
2563 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2564 mDevice->sendDown(secondPoint);
2565 mDevice->sendSync();
2566 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2567
2568 // ACTION_MOVE (First slot)
2569 mDevice->sendSlot(FIRST_SLOT);
2570 mDevice->sendMove(centerPoint + Point(5, 5));
2571 // ACTION_POINTER_UP (Second slot)
2572 mDevice->sendSlot(SECOND_SLOT);
2573 mDevice->sendPointerUp();
2574 // Send a single sync for the above 2 pointer updates
2575 mDevice->sendSync();
2576
2577 // First, we should get POINTER_UP for the second pointer
2578 assertReceivedMotion(ACTION_POINTER_1_UP,
2579 {/*first pointer */ centerPoint + Point(5, 5),
2580 /*second pointer*/ secondPoint});
2581
2582 // Next, the MOVE event for the first pointer
2583 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2584}
2585
2586/**
2587 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2588 * move, and then it will go up, all in the same frame.
2589 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2590 * gets sent to the listener.
2591 */
2592TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2593 NotifyMotionArgs args;
2594 const Point centerPoint = mDevice->getCenterPoint();
2595
2596 // ACTION_DOWN
2597 mDevice->sendSlot(FIRST_SLOT);
2598 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2599 mDevice->sendDown(centerPoint);
2600 mDevice->sendSync();
2601 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2602
2603 // ACTION_POINTER_DOWN (Second slot)
2604 const Point secondPoint = centerPoint + Point(100, 100);
2605 mDevice->sendSlot(SECOND_SLOT);
2606 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2607 mDevice->sendDown(secondPoint);
2608 mDevice->sendSync();
2609 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2610
2611 // ACTION_MOVE (First slot)
2612 mDevice->sendSlot(FIRST_SLOT);
2613 mDevice->sendMove(centerPoint + Point(5, 5));
2614 // ACTION_POINTER_UP (Second slot)
2615 mDevice->sendSlot(SECOND_SLOT);
2616 mDevice->sendMove(secondPoint + Point(6, 6));
2617 mDevice->sendPointerUp();
2618 // Send a single sync for the above 2 pointer updates
2619 mDevice->sendSync();
2620
2621 // First, we should get POINTER_UP for the second pointer
2622 // The movement of the second pointer during the liftoff frame is ignored.
2623 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2624 assertReceivedMotion(ACTION_POINTER_1_UP,
2625 {/*first pointer */ centerPoint + Point(5, 5),
2626 /*second pointer*/ secondPoint});
2627
2628 // Next, the MOVE event for the first pointer
2629 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2630}
2631
Arthur Hungaab25622020-01-16 11:22:11 +08002632TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2633 NotifyMotionArgs args;
2634 const Point centerPoint = mDevice->getCenterPoint();
2635
2636 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002637 mDevice->sendSlot(FIRST_SLOT);
2638 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002639 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002640 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002641 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2642 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2643
arthurhungcc7f9802020-04-30 17:55:40 +08002644 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002645 const Point secondPoint = centerPoint + Point(100, 100);
2646 mDevice->sendSlot(SECOND_SLOT);
2647 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2648 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002649 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002650 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002651 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002652
arthurhungcc7f9802020-04-30 17:55:40 +08002653 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002654 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002655 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002656 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2658
arthurhungcc7f9802020-04-30 17:55:40 +08002659 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2660 // a palm event.
2661 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002662 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002663 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002664 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002665 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002666 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002667
arthurhungcc7f9802020-04-30 17:55:40 +08002668 // Send up to second slot, expect first slot send moving.
2669 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002670 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002671 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002673
arthurhungcc7f9802020-04-30 17:55:40 +08002674 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002675 mDevice->sendSlot(FIRST_SLOT);
2676 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002677 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002678
arthurhungcc7f9802020-04-30 17:55:40 +08002679 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2680 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002681}
2682
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684class InputDeviceTest : public testing::Test {
2685protected:
2686 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002687 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688 static const int32_t DEVICE_ID;
2689 static const int32_t DEVICE_GENERATION;
2690 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002691 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002692 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002694 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002696 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002697 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002698 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002699
Chris Yea52ade12020-08-27 16:49:20 -07002700 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002701 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002702 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002703 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002704 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002705 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 InputDeviceIdentifier identifier;
2707 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002708 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002709 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002710 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002711 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002712 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002713 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002714 }
2715
Chris Yea52ade12020-08-27 16:49:20 -07002716 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002717 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002718 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002719 }
2720};
2721
2722const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002723const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002724const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2726const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002727const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002728 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002729const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002730
2731TEST_F(InputDeviceTest, ImmutableProperties) {
2732 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002733 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002734 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735}
2736
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002737TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2738 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2739
2740 // Configuration
2741 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2742 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002743 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002744
2745 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2746}
2747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002748TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2749 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002750}
2751
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2753 // Configuration.
2754 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002755 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002756
2757 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002758 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759
2760 NotifyDeviceResetArgs resetArgs;
2761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2762 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2763 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2764
2765 // Metadata.
2766 ASSERT_TRUE(mDevice->isIgnored());
2767 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2768
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002769 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002771 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2773 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2774
2775 // State queries.
2776 ASSERT_EQ(0, mDevice->getMetaState());
2777
2778 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2779 << "Ignored device should return unknown key code state.";
2780 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2781 << "Ignored device should return unknown scan code state.";
2782 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2783 << "Ignored device should return unknown switch state.";
2784
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002785 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002787 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788 << "Ignored device should never mark any key codes.";
2789 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2790 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2791}
2792
2793TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2794 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002795 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002797 FakeInputMapper& mapper1 =
2798 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002799 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2800 mapper1.setMetaState(AMETA_ALT_ON);
2801 mapper1.addSupportedKeyCode(AKEYCODE_A);
2802 mapper1.addSupportedKeyCode(AKEYCODE_B);
2803 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2804 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2805 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2806 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2807 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002809 FakeInputMapper& mapper2 =
2810 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002811 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812
2813 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002814 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002816 std::string propertyValue;
2817 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002819 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002821 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2822 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002823
2824 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002825 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2827 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828
2829 NotifyDeviceResetArgs resetArgs;
2830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2831 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2832 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2833
2834 // Metadata.
2835 ASSERT_FALSE(mDevice->isIgnored());
2836 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2837
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002838 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002840 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2842 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2843
2844 // State queries.
2845 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2846 << "Should query mappers and combine meta states.";
2847
2848 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2849 << "Should return unknown key code state when source not supported.";
2850 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2851 << "Should return unknown scan code state when source not supported.";
2852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2853 << "Should return unknown switch state when source not supported.";
2854
2855 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2856 << "Should query mapper when source is supported.";
2857 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2858 << "Should query mapper when source is supported.";
2859 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2860 << "Should query mapper when source is supported.";
2861
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002862 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002864 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865 << "Should do nothing when source is unsupported.";
2866 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2867 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2868 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2869 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2870
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002871 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872 << "Should query mapper when source is supported.";
2873 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2874 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2875 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2876 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2877
2878 // Event handling.
2879 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002880 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002881 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002883 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2884 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885}
2886
Arthur Hung2c9a3342019-07-23 14:18:59 +08002887// A single input device is associated with a specific display. Check that:
2888// 1. Device is disabled if the viewport corresponding to the associated display is not found
2889// 2. Device is disabled when setEnabled API is called
2890TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002891 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002892
2893 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002894 std::list<NotifyArgs> unused =
2895 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002896
2897 // Device should be enabled by default.
2898 ASSERT_TRUE(mDevice->isEnabled());
2899
2900 // Prepare associated info.
2901 constexpr uint8_t hdmi = 1;
2902 const std::string UNIQUE_ID = "local:1";
2903
2904 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002905 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2906 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002907 // Device should be disabled because it is associated with a specific display via
2908 // input port <-> display port association, but the corresponding display is not found
2909 ASSERT_FALSE(mDevice->isEnabled());
2910
2911 // Prepare displays.
2912 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002913 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2914 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002915 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2916 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002917 ASSERT_TRUE(mDevice->isEnabled());
2918
2919 // Device should be disabled after set disable.
2920 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002921 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2922 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002923 ASSERT_FALSE(mDevice->isEnabled());
2924
2925 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002926 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2927 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002928 ASSERT_FALSE(mDevice->isEnabled());
2929}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930
Christine Franks1ba71cc2021-04-07 14:37:42 -07002931TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2932 // Device should be enabled by default.
2933 mFakePolicy->clearViewports();
2934 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002935 std::list<NotifyArgs> unused =
2936 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002937 ASSERT_TRUE(mDevice->isEnabled());
2938
2939 // Device should be disabled because it is associated with a specific display, but the
2940 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002941 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002942 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2943 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002944 ASSERT_FALSE(mDevice->isEnabled());
2945
2946 // Device should be enabled when a display is found.
2947 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2948 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2949 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002950 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2951 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002952 ASSERT_TRUE(mDevice->isEnabled());
2953
2954 // Device should be disabled after set disable.
2955 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002956 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2957 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002958 ASSERT_FALSE(mDevice->isEnabled());
2959
2960 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002961 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2962 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002963 ASSERT_FALSE(mDevice->isEnabled());
2964}
2965
Christine Franks2a2293c2022-01-18 11:51:16 -08002966TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2967 mFakePolicy->clearViewports();
2968 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002969 std::list<NotifyArgs> unused =
2970 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002971
Christine Franks2a2293c2022-01-18 11:51:16 -08002972 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2973 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2974 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2975 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002976 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2977 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002978 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2979}
2980
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002981/**
2982 * This test reproduces a crash caused by a dangling reference that remains after device is added
2983 * and removed. The reference is accessed in InputDevice::dump(..);
2984 */
2985TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2986 constexpr int32_t TEST_EVENTHUB_ID = 10;
2987 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2988
2989 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
2990 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
2991 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2992 std::string dumpStr, eventHubDevStr;
2993 device.dump(dumpStr, eventHubDevStr);
2994}
2995
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996// --- InputMapperTest ---
2997
2998class InputMapperTest : public testing::Test {
2999protected:
3000 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003001 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003002 static const int32_t DEVICE_ID;
3003 static const int32_t DEVICE_GENERATION;
3004 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003005 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003006 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003008 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003010 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003011 std::unique_ptr<InstrumentedInputReader> mReader;
3012 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003013
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003014 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003015 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003016 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003017 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003018 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003019 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003020 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003021 // Consume the device reset notification generated when adding a new device.
3022 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003023 }
3024
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003025 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003026 SetUp(DEVICE_CLASSES);
3027 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003028
Chris Yea52ade12020-08-27 16:49:20 -07003029 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003030 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003032 }
3033
3034 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003035 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036 }
3037
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003038 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003039 if (!changes ||
3040 (changes &
3041 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3042 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003043 mReader->requestRefreshConfiguration(changes);
3044 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003045 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003046 std::list<NotifyArgs> out =
3047 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003048 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003049 for (const NotifyArgs& args : out) {
3050 mFakeListener->notify(args);
3051 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003052 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003053 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003054 }
3055
arthurhungdcef2dc2020-08-11 14:47:50 +08003056 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3057 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003058 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003059 InputDeviceIdentifier identifier;
3060 identifier.name = name;
3061 identifier.location = location;
3062 std::shared_ptr<InputDevice> device =
3063 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3064 identifier);
3065 mReader->pushNextDevice(device);
3066 mFakeEventHub->addDevice(eventHubId, name, classes);
3067 mReader->loopOnce();
3068 return device;
3069 }
3070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003071 template <class T, typename... Args>
3072 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003073 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003074 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003075 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3076 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003077 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003078 for (const NotifyArgs& loopArgs : resetArgList) {
3079 mFakeListener->notify(loopArgs);
3080 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003081 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003082 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083 }
3084
3085 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003086 int32_t orientation, const std::string& uniqueId,
3087 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003088 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3089 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003090 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3091 }
3092
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003093 void clearViewports() {
3094 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 }
3096
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003097 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3098 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099 RawEvent event;
3100 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003101 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003102 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 event.type = type;
3104 event.code = code;
3105 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003106 std::list<NotifyArgs> processArgList = mapper.process(&event);
3107 for (const NotifyArgs& args : processArgList) {
3108 mFakeListener->notify(args);
3109 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003110 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003111 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003112 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 }
3114
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003115 void resetMapper(InputMapper& mapper, nsecs_t when) {
3116 const auto resetArgs = mapper.reset(when);
3117 for (const auto args : resetArgs) {
3118 mFakeListener->notify(args);
3119 }
3120 // Loop the reader to flush the input listener queue.
3121 mReader->loopOnce();
3122 }
3123
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124 static void assertMotionRange(const InputDeviceInfo& info,
3125 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3126 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003127 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3129 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3130 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3131 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3132 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3133 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3134 }
3135
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003136 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3137 float size, float touchMajor, float touchMinor, float toolMajor,
3138 float toolMinor, float orientation, float distance,
3139 float scaledAxisEpsilon = 1.f) {
3140 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3141 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3143 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003144 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3145 scaledAxisEpsilon);
3146 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3147 scaledAxisEpsilon);
3148 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3149 scaledAxisEpsilon);
3150 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3151 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3153 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3154 }
3155
Michael Wright17db18e2020-06-26 20:51:44 +01003156 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003158 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 ASSERT_NEAR(x, actualX, 1);
3160 ASSERT_NEAR(y, actualY, 1);
3161 }
3162};
3163
3164const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003165const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003166const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3168const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003169const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3170 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003171const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172
3173// --- SwitchInputMapperTest ---
3174
3175class SwitchInputMapperTest : public InputMapperTest {
3176protected:
3177};
3178
3179TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003180 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003182 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183}
3184
3185TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003186 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003188 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003189 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003191 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003192 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193}
3194
3195TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003196 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003197 std::list<NotifyArgs> out;
3198 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3199 ASSERT_TRUE(out.empty());
3200 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3201 ASSERT_TRUE(out.empty());
3202 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3203 ASSERT_TRUE(out.empty());
3204 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003206 ASSERT_EQ(1u, out.size());
3207 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003208 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003209 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3210 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003211 args.switchMask);
3212 ASSERT_EQ(uint32_t(0), args.policyFlags);
3213}
3214
Chris Ye87143712020-11-10 05:05:58 +00003215// --- VibratorInputMapperTest ---
3216class VibratorInputMapperTest : public InputMapperTest {
3217protected:
3218 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3219};
3220
3221TEST_F(VibratorInputMapperTest, GetSources) {
3222 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3223
3224 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3225}
3226
3227TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3228 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3229
3230 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3231}
3232
3233TEST_F(VibratorInputMapperTest, Vibrate) {
3234 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003235 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003236 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3237
3238 VibrationElement pattern(2);
3239 VibrationSequence sequence(2);
3240 pattern.duration = std::chrono::milliseconds(200);
3241 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3242 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3243 sequence.addElement(pattern);
3244 pattern.duration = std::chrono::milliseconds(500);
3245 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3246 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3247 sequence.addElement(pattern);
3248
3249 std::vector<int64_t> timings = {0, 1};
3250 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3251
3252 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003253 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003254 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003255 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003256 // Verify vibrator state listener was notified.
3257 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003258 ASSERT_EQ(1u, out.size());
3259 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3260 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3261 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003262 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003263 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003264 ASSERT_FALSE(mapper.isVibrating());
3265 // Verify vibrator state listener was notified.
3266 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003267 ASSERT_EQ(1u, out.size());
3268 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3269 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3270 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003271}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272
Chris Yef59a2f42020-10-16 12:55:26 -07003273// --- SensorInputMapperTest ---
3274
3275class SensorInputMapperTest : public InputMapperTest {
3276protected:
3277 static const int32_t ACCEL_RAW_MIN;
3278 static const int32_t ACCEL_RAW_MAX;
3279 static const int32_t ACCEL_RAW_FUZZ;
3280 static const int32_t ACCEL_RAW_FLAT;
3281 static const int32_t ACCEL_RAW_RESOLUTION;
3282
3283 static const int32_t GYRO_RAW_MIN;
3284 static const int32_t GYRO_RAW_MAX;
3285 static const int32_t GYRO_RAW_FUZZ;
3286 static const int32_t GYRO_RAW_FLAT;
3287 static const int32_t GYRO_RAW_RESOLUTION;
3288
3289 static const float GRAVITY_MS2_UNIT;
3290 static const float DEGREE_RADIAN_UNIT;
3291
3292 void prepareAccelAxes();
3293 void prepareGyroAxes();
3294 void setAccelProperties();
3295 void setGyroProperties();
3296 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3297};
3298
3299const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3300const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3301const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3302const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3303const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3304
3305const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3306const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3307const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3308const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3309const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3310
3311const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3312const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3313
3314void SensorInputMapperTest::prepareAccelAxes() {
3315 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3316 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3318 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3319 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3320 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3321}
3322
3323void SensorInputMapperTest::prepareGyroAxes() {
3324 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3325 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3326 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3327 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3328 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3329 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3330}
3331
3332void SensorInputMapperTest::setAccelProperties() {
3333 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3334 /* sensorDataIndex */ 0);
3335 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3336 /* sensorDataIndex */ 1);
3337 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3338 /* sensorDataIndex */ 2);
3339 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3340 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3341 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3342 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3343 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3344}
3345
3346void SensorInputMapperTest::setGyroProperties() {
3347 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3348 /* sensorDataIndex */ 0);
3349 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3350 /* sensorDataIndex */ 1);
3351 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3352 /* sensorDataIndex */ 2);
3353 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3354 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3355 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3356 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3357 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3358}
3359
3360TEST_F(SensorInputMapperTest, GetSources) {
3361 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3362
3363 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3364}
3365
3366TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3367 setAccelProperties();
3368 prepareAccelAxes();
3369 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3370
3371 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3372 std::chrono::microseconds(10000),
3373 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003374 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003380
3381 NotifySensorArgs args;
3382 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3383 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3384 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3385
3386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3387 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3388 ASSERT_EQ(args.deviceId, DEVICE_ID);
3389 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3390 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3391 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3392 ASSERT_EQ(args.values, values);
3393 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3394}
3395
3396TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3397 setGyroProperties();
3398 prepareGyroAxes();
3399 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3400
3401 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3402 std::chrono::microseconds(10000),
3403 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003404 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003410
3411 NotifySensorArgs args;
3412 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3413 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3414 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3415
3416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3417 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3418 ASSERT_EQ(args.deviceId, DEVICE_ID);
3419 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3420 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3421 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3422 ASSERT_EQ(args.values, values);
3423 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3424}
3425
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426// --- KeyboardInputMapperTest ---
3427
3428class KeyboardInputMapperTest : public InputMapperTest {
3429protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003430 const std::string UNIQUE_ID = "local:0";
3431
3432 void prepareDisplay(int32_t orientation);
3433
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003434 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003435 int32_t originalKeyCode, int32_t rotatedKeyCode,
3436 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437};
3438
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003439/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3440 * orientation.
3441 */
3442void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003443 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3444 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003445}
3446
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003447void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003448 int32_t originalScanCode, int32_t originalKeyCode,
3449 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450 NotifyKeyArgs args;
3451
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3454 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3455 ASSERT_EQ(originalScanCode, args.scanCode);
3456 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003457 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3461 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3462 ASSERT_EQ(originalScanCode, args.scanCode);
3463 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003464 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465}
3466
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003468 KeyboardInputMapper& mapper =
3469 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3470 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003472 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003473}
3474
3475TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3476 const int32_t USAGE_A = 0x070004;
3477 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003478 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3479 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003480 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3481 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3482 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003484 KeyboardInputMapper& mapper =
3485 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3486 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003487 // Initial metastate is AMETA_NONE.
3488 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489
3490 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492 NotifyKeyArgs args;
3493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3494 ASSERT_EQ(DEVICE_ID, args.deviceId);
3495 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3496 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3497 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3498 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3499 ASSERT_EQ(KEY_HOME, args.scanCode);
3500 ASSERT_EQ(AMETA_NONE, args.metaState);
3501 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3502 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3503 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3504
3505 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003506 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3508 ASSERT_EQ(DEVICE_ID, args.deviceId);
3509 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3510 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3511 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3512 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3513 ASSERT_EQ(KEY_HOME, args.scanCode);
3514 ASSERT_EQ(AMETA_NONE, args.metaState);
3515 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3516 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3517 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3518
3519 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3523 ASSERT_EQ(DEVICE_ID, args.deviceId);
3524 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3525 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3526 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3527 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3528 ASSERT_EQ(0, args.scanCode);
3529 ASSERT_EQ(AMETA_NONE, args.metaState);
3530 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3531 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3532 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3533
3534 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3536 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3538 ASSERT_EQ(DEVICE_ID, args.deviceId);
3539 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3540 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3541 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3542 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3543 ASSERT_EQ(0, args.scanCode);
3544 ASSERT_EQ(AMETA_NONE, args.metaState);
3545 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3546 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3547 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3548
3549 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3553 ASSERT_EQ(DEVICE_ID, args.deviceId);
3554 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3555 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3556 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3557 ASSERT_EQ(0, args.keyCode);
3558 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3559 ASSERT_EQ(AMETA_NONE, args.metaState);
3560 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3561 ASSERT_EQ(0U, args.policyFlags);
3562 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3563
3564 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3566 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3568 ASSERT_EQ(DEVICE_ID, args.deviceId);
3569 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3570 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3571 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3572 ASSERT_EQ(0, args.keyCode);
3573 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3574 ASSERT_EQ(AMETA_NONE, args.metaState);
3575 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3576 ASSERT_EQ(0U, args.policyFlags);
3577 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3578}
3579
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003580/**
3581 * Ensure that the readTime is set to the time when the EV_KEY is received.
3582 */
3583TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3584 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3585
3586 KeyboardInputMapper& mapper =
3587 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3588 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3589 NotifyKeyArgs args;
3590
3591 // Key down
3592 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3594 ASSERT_EQ(12, args.readTime);
3595
3596 // Key up
3597 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3599 ASSERT_EQ(15, args.readTime);
3600}
3601
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003603 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3604 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003605 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3606 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3607 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003609 KeyboardInputMapper& mapper =
3610 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3611 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612
Arthur Hung95f68612022-04-07 14:08:22 +08003613 // Initial metastate is AMETA_NONE.
3614 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615
3616 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 NotifyKeyArgs args;
3619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3620 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003621 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003622 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623
3624 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003625 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3627 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003628 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629
3630 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003631 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3633 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003634 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635
3636 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003637 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3639 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003640 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003641 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003642}
3643
3644TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003645 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3646 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3647 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3648 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003650 KeyboardInputMapper& mapper =
3651 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3652 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003654 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003655 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3656 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3657 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3658 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3659 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3660 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3661 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3662 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3663}
3664
3665TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3667 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3668 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3669 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003672 KeyboardInputMapper& mapper =
3673 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3674 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003675
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003676 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003677 ASSERT_NO_FATAL_FAILURE(
3678 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3679 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3680 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3681 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3682 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3683 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3684 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003686 clearViewports();
3687 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003688 ASSERT_NO_FATAL_FAILURE(
3689 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3690 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3691 AKEYCODE_DPAD_UP, DISPLAY_ID));
3692 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3693 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3694 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3695 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003697 clearViewports();
3698 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003699 ASSERT_NO_FATAL_FAILURE(
3700 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3701 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3702 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3703 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3704 AKEYCODE_DPAD_UP, DISPLAY_ID));
3705 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3706 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003708 clearViewports();
3709 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003710 ASSERT_NO_FATAL_FAILURE(
3711 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3712 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3713 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3714 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3715 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3716 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3717 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718
3719 // Special case: if orientation changes while key is down, we still emit the same keycode
3720 // in the key up as we did in the key down.
3721 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003722 clearViewports();
3723 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3726 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3727 ASSERT_EQ(KEY_UP, args.scanCode);
3728 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3729
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003730 clearViewports();
3731 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3734 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3735 ASSERT_EQ(KEY_UP, args.scanCode);
3736 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3737}
3738
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003739TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3740 // If the keyboard is not orientation aware,
3741 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003742 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003743
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003744 KeyboardInputMapper& mapper =
3745 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3746 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003747 NotifyKeyArgs args;
3748
3749 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3754 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3755
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003756 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3761 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3762}
3763
3764TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3765 // If the keyboard is orientation aware,
3766 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003767 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003768
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003769 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003770 KeyboardInputMapper& mapper =
3771 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3772 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003773 NotifyKeyArgs args;
3774
3775 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3776 // ^--- already checked by the previous test
3777
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003778 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003779 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3784 ASSERT_EQ(DISPLAY_ID, args.displayId);
3785
3786 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003787 clearViewports();
3788 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003789 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3794 ASSERT_EQ(newDisplayId, args.displayId);
3795}
3796
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003798 KeyboardInputMapper& mapper =
3799 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3800 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003802 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003805 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807}
3808
Philip Junker4af3b3d2021-12-14 10:36:55 +01003809TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3810 KeyboardInputMapper& mapper =
3811 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3812 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3813
3814 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3815 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3816 << "If a mapping is available, the result is equal to the mapping";
3817
3818 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3819 << "If no mapping is available, the result is the key location";
3820}
3821
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003823 KeyboardInputMapper& mapper =
3824 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3825 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003827 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003828 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003830 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003831 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832}
3833
3834TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003835 KeyboardInputMapper& mapper =
3836 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3837 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003839 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003842 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843 ASSERT_TRUE(flags[0]);
3844 ASSERT_FALSE(flags[1]);
3845}
3846
3847TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003848 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3849 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3850 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3851 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3852 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3853 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003855 KeyboardInputMapper& mapper =
3856 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3857 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003858 // Initial metastate is AMETA_NONE.
3859 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860
3861 // Initialization should have turned all of the lights off.
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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865
3866 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003869 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3870 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3871 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003872 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003873
3874 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003877 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3878 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3879 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003880 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881
3882 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003885 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3886 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3887 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003888 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889
3890 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003893 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3894 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3895 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003896 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897
3898 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003901 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3902 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3903 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003904 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905
3906 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003909 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3910 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3911 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003912 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913}
3914
Chris Yea52ade12020-08-27 16:49:20 -07003915TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3916 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3917 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3918 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3919 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3920
3921 KeyboardInputMapper& mapper =
3922 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3923 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3924
Chris Yea52ade12020-08-27 16:49:20 -07003925 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003926 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003927 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3928 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3929 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3930 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3931
3932 NotifyKeyArgs args;
3933 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3936 ASSERT_EQ(AMETA_NONE, args.metaState);
3937 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3938 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3939 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3940
3941 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003942 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3944 ASSERT_EQ(AMETA_NONE, args.metaState);
3945 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3946 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3947 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3948}
3949
Arthur Hung2c9a3342019-07-23 14:18:59 +08003950TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3951 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003952 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3953 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3954 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3955 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003956
3957 // keyboard 2.
3958 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003959 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003960 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003961 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003962 std::shared_ptr<InputDevice> device2 =
3963 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003964 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003965
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003966 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3967 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3968 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3969 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003970
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003971 KeyboardInputMapper& mapper =
3972 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3973 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003974
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003975 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003976 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003977 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003978 std::list<NotifyArgs> unused =
3979 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3980 0 /*changes*/);
3981 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003982
3983 // Prepared displays and associated info.
3984 constexpr uint8_t hdmi1 = 0;
3985 constexpr uint8_t hdmi2 = 1;
3986 const std::string SECONDARY_UNIQUE_ID = "local:1";
3987
3988 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3989 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3990
3991 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003992 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3993 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003994 ASSERT_FALSE(device2->isEnabled());
3995
3996 // Prepare second display.
3997 constexpr int32_t newDisplayId = 2;
3998 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003999 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004000 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004001 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004002 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004003 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4004 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004005
4006 // Device should be enabled after the associated display is found.
4007 ASSERT_TRUE(mDevice->isEnabled());
4008 ASSERT_TRUE(device2->isEnabled());
4009
4010 // Test pad key events
4011 ASSERT_NO_FATAL_FAILURE(
4012 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4013 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4014 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4015 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4016 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4017 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4018 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4019
4020 ASSERT_NO_FATAL_FAILURE(
4021 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4022 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4023 AKEYCODE_DPAD_RIGHT, newDisplayId));
4024 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4025 AKEYCODE_DPAD_DOWN, newDisplayId));
4026 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4027 AKEYCODE_DPAD_LEFT, newDisplayId));
4028}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004029
arthurhungc903df12020-08-11 15:08:42 +08004030TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4031 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4032 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4033 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4034 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4035 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4036 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4037
4038 KeyboardInputMapper& mapper =
4039 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4040 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004041 // Initial metastate is AMETA_NONE.
4042 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004043
4044 // Initialization should have turned all of the lights off.
4045 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4046 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4047 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4048
4049 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004052 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4053 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4054
4055 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004058 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4059 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4060
4061 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004064 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4065 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4066
4067 mFakeEventHub->removeDevice(EVENTHUB_ID);
4068 mReader->loopOnce();
4069
4070 // keyboard 2 should default toggle keys.
4071 const std::string USB2 = "USB2";
4072 const std::string DEVICE_NAME2 = "KEYBOARD2";
4073 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4074 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4075 std::shared_ptr<InputDevice> device2 =
4076 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004077 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004078 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4079 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4080 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4081 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4082 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4083 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4084
arthurhung6fe95782020-10-05 22:41:16 +08004085 KeyboardInputMapper& mapper2 =
4086 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4087 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004088 std::list<NotifyArgs> unused =
4089 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4090 0 /*changes*/);
4091 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004092
4093 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4094 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4095 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004096 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4097 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004098}
4099
Arthur Hungcb40a002021-08-03 14:31:01 +00004100TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4101 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4102 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4103 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4104
4105 // Suppose we have two mappers. (DPAD + KEYBOARD)
4106 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4107 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4108 KeyboardInputMapper& mapper =
4109 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4110 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004111 // Initial metastate is AMETA_NONE.
4112 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004113
4114 mReader->toggleCapsLockState(DEVICE_ID);
4115 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4116}
4117
Arthur Hungfb3cc112022-04-13 07:39:50 +00004118TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4119 // keyboard 1.
4120 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4121 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4122 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4124 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4125 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4126
4127 KeyboardInputMapper& mapper1 =
4128 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4129 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4130
4131 // keyboard 2.
4132 const std::string USB2 = "USB2";
4133 const std::string DEVICE_NAME2 = "KEYBOARD2";
4134 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4135 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4136 std::shared_ptr<InputDevice> device2 =
4137 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4138 ftl::Flags<InputDeviceClass>(0));
4139 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4140 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4141 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4142 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4143 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4144 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4145
4146 KeyboardInputMapper& mapper2 =
4147 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4148 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004149 std::list<NotifyArgs> unused =
4150 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4151 0 /*changes*/);
4152 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004153
Arthur Hung95f68612022-04-07 14:08:22 +08004154 // Initial metastate is AMETA_NONE.
4155 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4156 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4157
4158 // Toggle num lock on and off.
4159 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4160 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004161 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4162 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4163 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4164
4165 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4166 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4167 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4168 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4169 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4170
4171 // Toggle caps lock on and off.
4172 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4173 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4174 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4175 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4176 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4177
4178 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4179 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4180 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4181 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4182 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4183
4184 // Toggle scroll lock on and off.
4185 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4186 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4187 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4188 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4189 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4190
4191 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4192 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4193 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4194 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4195 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4196}
4197
Arthur Hung2141d542022-08-23 07:45:21 +00004198TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4199 const int32_t USAGE_A = 0x070004;
4200 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4201 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4202
4203 KeyboardInputMapper& mapper =
4204 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4205 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4206 // Key down by scan code.
4207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4208 NotifyKeyArgs args;
4209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4210 ASSERT_EQ(DEVICE_ID, args.deviceId);
4211 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4212 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4213 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4214 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4215 ASSERT_EQ(KEY_HOME, args.scanCode);
4216 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4217
4218 // Disable device, it should synthesize cancellation events for down events.
4219 mFakePolicy->addDisabledDevice(DEVICE_ID);
4220 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4221
4222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4223 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4224 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4225 ASSERT_EQ(KEY_HOME, args.scanCode);
4226 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4227}
4228
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004229// --- KeyboardInputMapperTest_ExternalDevice ---
4230
4231class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4232protected:
Chris Yea52ade12020-08-27 16:49:20 -07004233 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004234};
4235
4236TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004237 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4238 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004239
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004240 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4241 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4242 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4243 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004244
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004245 KeyboardInputMapper& mapper =
4246 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4247 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004248
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004250 NotifyKeyArgs args;
4251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4252 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4253
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004254 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4256 ASSERT_EQ(uint32_t(0), args.policyFlags);
4257
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4260 ASSERT_EQ(uint32_t(0), args.policyFlags);
4261
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004262 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4264 ASSERT_EQ(uint32_t(0), args.policyFlags);
4265
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4268 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4269
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004270 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4272 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4273}
4274
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004275TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004276 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004277
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004278 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4279 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4280 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004281
Powei Fengd041c5d2019-05-03 17:11:33 -07004282 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004283 KeyboardInputMapper& mapper =
4284 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4285 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004286
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004288 NotifyKeyArgs args;
4289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4290 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4291
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004292 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4294 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4295
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4298 ASSERT_EQ(uint32_t(0), args.policyFlags);
4299
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004300 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4302 ASSERT_EQ(uint32_t(0), args.policyFlags);
4303
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4306 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4307
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004308 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4310 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4311}
4312
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313// --- CursorInputMapperTest ---
4314
4315class CursorInputMapperTest : public InputMapperTest {
4316protected:
4317 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4318
Michael Wright17db18e2020-06-26 20:51:44 +01004319 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320
Chris Yea52ade12020-08-27 16:49:20 -07004321 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322 InputMapperTest::SetUp();
4323
Michael Wright17db18e2020-06-26 20:51:44 +01004324 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004325 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 }
4327
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004328 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4329 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004330
4331 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004332 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4333 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4334 }
4335
4336 void prepareSecondaryDisplay() {
4337 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4338 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4339 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004340 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341
4342 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4343 float pressure) {
4344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4345 0.0f, 0.0f, 0.0f, EPSILON));
4346 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347};
4348
4349const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4350
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004351void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4352 int32_t originalY, int32_t rotatedX,
4353 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 NotifyMotionArgs args;
4355
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004361 ASSERT_NO_FATAL_FAILURE(
4362 assertCursorPointerCoords(args.pointerCoords[0],
4363 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4364 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365}
4366
4367TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004369 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004371 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372}
4373
4374TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004376 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004378 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379}
4380
4381TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004383 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384
4385 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004386 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387
4388 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004389 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4390 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4392 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4393
4394 // When the bounds are set, then there should be a valid motion range.
4395 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4396
4397 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004398 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399
4400 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4401 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4402 1, 800 - 1, 0.0f, 0.0f));
4403 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4404 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4405 2, 480 - 1, 0.0f, 0.0f));
4406 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4407 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4408 0.0f, 1.0f, 0.0f, 0.0f));
4409}
4410
4411TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004413 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414
4415 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004416 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417
4418 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4419 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4420 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4421 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4422 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4423 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4424 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4425 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4426 0.0f, 1.0f, 0.0f, 0.0f));
4427}
4428
4429TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004431 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432
arthurhungdcef2dc2020-08-11 14:47:50 +08004433 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
4435 NotifyMotionArgs args;
4436
4437 // Button press.
4438 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4442 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4443 ASSERT_EQ(DEVICE_ID, args.deviceId);
4444 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4445 ASSERT_EQ(uint32_t(0), args.policyFlags);
4446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4447 ASSERT_EQ(0, args.flags);
4448 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4449 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4450 ASSERT_EQ(0, args.edgeFlags);
4451 ASSERT_EQ(uint32_t(1), args.pointerCount);
4452 ASSERT_EQ(0, args.pointerProperties[0].id);
4453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004454 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4456 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4458
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4460 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4461 ASSERT_EQ(DEVICE_ID, args.deviceId);
4462 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4463 ASSERT_EQ(uint32_t(0), args.policyFlags);
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4465 ASSERT_EQ(0, args.flags);
4466 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4467 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4468 ASSERT_EQ(0, args.edgeFlags);
4469 ASSERT_EQ(uint32_t(1), args.pointerCount);
4470 ASSERT_EQ(0, args.pointerProperties[0].id);
4471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004472 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004473 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4474 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4475 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4476
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004478 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4479 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4481 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4482 ASSERT_EQ(DEVICE_ID, args.deviceId);
4483 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4484 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004485 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4486 ASSERT_EQ(0, args.flags);
4487 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4488 ASSERT_EQ(0, args.buttonState);
4489 ASSERT_EQ(0, args.edgeFlags);
4490 ASSERT_EQ(uint32_t(1), args.pointerCount);
4491 ASSERT_EQ(0, args.pointerProperties[0].id);
4492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004493 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004494 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4495 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4496 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4497
4498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4499 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4500 ASSERT_EQ(DEVICE_ID, args.deviceId);
4501 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4502 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4504 ASSERT_EQ(0, args.flags);
4505 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4506 ASSERT_EQ(0, args.buttonState);
4507 ASSERT_EQ(0, args.edgeFlags);
4508 ASSERT_EQ(uint32_t(1), args.pointerCount);
4509 ASSERT_EQ(0, args.pointerProperties[0].id);
4510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004511 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4513 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4514 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4515}
4516
4517TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
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 // Motion in X but not Y.
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_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004528 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4529 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4530 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531
4532 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004537 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4538 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539}
4540
4541TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004543 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544
4545 NotifyMotionArgs args;
4546
4547 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004552 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004556 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004557
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004562 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004563 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004564
4565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004567 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568}
4569
4570TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004572 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573
4574 NotifyMotionArgs args;
4575
4576 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004583 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4584 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4585 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004589 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4590 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4591 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004592
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004599 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4600 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4601 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602
4603 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004607 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004608 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004609
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004612 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613}
4614
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004615TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004616 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004618 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4619 // need to be rotated.
4620 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004621 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004623 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4625 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4626 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4627 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4628 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4629 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4630 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4631 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4632}
4633
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004634TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004635 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004637 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4638 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004639 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004641 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004642 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4644 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4645 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4646 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4647 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4648 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4649 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4650 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4651
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004652 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004653 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004654 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4655 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4656 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4657 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4658 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4659 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4660 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4661 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004663 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004664 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4666 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4667 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4668 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4669 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4670 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4671 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4672 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4673
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004674 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004675 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004676 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4677 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4678 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4679 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4680 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4681 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4682 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4683 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684}
4685
4686TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004688 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689
4690 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4691 mFakePointerController->setPosition(100, 200);
4692 mFakePointerController->setButtonState(0);
4693
4694 NotifyMotionArgs motionArgs;
4695 NotifyKeyArgs keyArgs;
4696
4697 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004698 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4701 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4702 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4703 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004704 ASSERT_NO_FATAL_FAILURE(
4705 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4709 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4710 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, 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_LEFT, 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(0, motionArgs.buttonState);
4719 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004720 ASSERT_NO_FATAL_FAILURE(
4721 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722
4723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004724 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_EQ(0, motionArgs.buttonState);
4726 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004727 ASSERT_NO_FATAL_FAILURE(
4728 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004729
4730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004732 ASSERT_EQ(0, motionArgs.buttonState);
4733 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004734 ASSERT_NO_FATAL_FAILURE(
4735 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736
4737 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4742 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4743 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4744 motionArgs.buttonState);
4745 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4746 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004747 ASSERT_NO_FATAL_FAILURE(
4748 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4751 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4752 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4753 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4754 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004755 ASSERT_NO_FATAL_FAILURE(
4756 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004757
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4759 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4760 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4761 motionArgs.buttonState);
4762 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4763 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004764 ASSERT_NO_FATAL_FAILURE(
4765 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004766
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4772 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004773 ASSERT_NO_FATAL_FAILURE(
4774 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004775
4776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004778 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4779 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004780 ASSERT_NO_FATAL_FAILURE(
4781 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004786 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4787 ASSERT_EQ(0, motionArgs.buttonState);
4788 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004789 ASSERT_NO_FATAL_FAILURE(
4790 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004793
4794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 ASSERT_EQ(0, motionArgs.buttonState);
4796 ASSERT_EQ(0, mFakePointerController->getButtonState());
4797 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004798 ASSERT_NO_FATAL_FAILURE(
4799 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004800
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4802 ASSERT_EQ(0, motionArgs.buttonState);
4803 ASSERT_EQ(0, mFakePointerController->getButtonState());
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004805 ASSERT_NO_FATAL_FAILURE(
4806 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807
4808 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4812 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4813 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004816 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4818 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004819 ASSERT_NO_FATAL_FAILURE(
4820 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004821
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4824 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4825 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004826 ASSERT_NO_FATAL_FAILURE(
4827 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004832 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_EQ(0, motionArgs.buttonState);
4834 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004835 ASSERT_NO_FATAL_FAILURE(
4836 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004837
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004840 ASSERT_EQ(0, motionArgs.buttonState);
4841 ASSERT_EQ(0, mFakePointerController->getButtonState());
4842
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004843 ASSERT_NO_FATAL_FAILURE(
4844 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4846 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4847 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4848
4849 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4853 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4854 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004855
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004857 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4859 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004860 ASSERT_NO_FATAL_FAILURE(
4861 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004862
4863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4865 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4866 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004867 ASSERT_NO_FATAL_FAILURE(
4868 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004873 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874 ASSERT_EQ(0, motionArgs.buttonState);
4875 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004876 ASSERT_NO_FATAL_FAILURE(
4877 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004878
4879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4880 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4881 ASSERT_EQ(0, motionArgs.buttonState);
4882 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004883 ASSERT_NO_FATAL_FAILURE(
4884 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004885
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4887 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4888 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4889
4890 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4894 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4895 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004896
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004898 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4900 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004901 ASSERT_NO_FATAL_FAILURE(
4902 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004903
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4905 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4906 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4907 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004908 ASSERT_NO_FATAL_FAILURE(
4909 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915 ASSERT_EQ(0, motionArgs.buttonState);
4916 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004917 ASSERT_NO_FATAL_FAILURE(
4918 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004919
4920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4921 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4922 ASSERT_EQ(0, motionArgs.buttonState);
4923 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004924 ASSERT_NO_FATAL_FAILURE(
4925 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004926
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4928 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4929 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4930
4931 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4935 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4936 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004937
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004939 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4941 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004942 ASSERT_NO_FATAL_FAILURE(
4943 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004944
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4946 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4947 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4948 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004949 ASSERT_NO_FATAL_FAILURE(
4950 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956 ASSERT_EQ(0, motionArgs.buttonState);
4957 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004958 ASSERT_NO_FATAL_FAILURE(
4959 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004960
4961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4962 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4963 ASSERT_EQ(0, motionArgs.buttonState);
4964 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004965 ASSERT_NO_FATAL_FAILURE(
4966 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004967
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4969 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4970 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4971}
4972
4973TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004975 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976
4977 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4978 mFakePointerController->setPosition(100, 200);
4979 mFakePointerController->setButtonState(0);
4980
4981 NotifyMotionArgs args;
4982
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4985 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004987 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4988 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4990 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 +01004991 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004992}
4993
4994TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004995 addConfigurationProperty("cursor.mode", "pointer");
4996 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004998
4999 NotifyDeviceResetArgs resetArgs;
5000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5001 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5002 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5003
5004 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5005 mFakePointerController->setPosition(100, 200);
5006 mFakePointerController->setButtonState(0);
5007
5008 NotifyMotionArgs args;
5009
5010 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5015 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5018 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 +01005019 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005020
5021 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5025 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5026 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5028 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5030 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5031 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5033 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5034
5035 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005036 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5037 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5039 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5042 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5044 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5045 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5047 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5048
5049 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5054 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5057 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 +01005058 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005059
5060 // Disable pointer capture and check that the device generation got bumped
5061 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005062 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005063 mFakePolicy->setPointerCapture(false);
5064 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005065 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005066
5067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005068 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5069
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5074 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5077 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 +01005078 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079}
5080
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005081/**
5082 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5083 * pointer acceleration or speed processing should not be applied.
5084 */
5085TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5086 addConfigurationProperty("cursor.mode", "pointer");
5087 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5088 100.f /*high threshold*/, 10.f /*acceleration*/);
5089 mFakePolicy->setVelocityControlParams(testParams);
5090 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5091
5092 NotifyDeviceResetArgs resetArgs;
5093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5094 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5095 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5096
5097 NotifyMotionArgs args;
5098
5099 // Move and verify scale is applied.
5100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5104 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5105 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5106 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5107 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5108 ASSERT_GT(relX, 10);
5109 ASSERT_GT(relY, 20);
5110
5111 // Enable Pointer Capture
5112 mFakePolicy->setPointerCapture(true);
5113 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5114 NotifyPointerCaptureChangedArgs captureArgs;
5115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5116 ASSERT_TRUE(captureArgs.request.enable);
5117
5118 // Move and verify scale is not applied.
5119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5123 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5125 ASSERT_EQ(10, args.pointerCoords[0].getX());
5126 ASSERT_EQ(20, args.pointerCoords[0].getY());
5127}
5128
Prabir Pradhan208360b2022-06-24 18:37:04 +00005129TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5130 addConfigurationProperty("cursor.mode", "pointer");
5131 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5132
5133 NotifyDeviceResetArgs resetArgs;
5134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5135 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5136 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5137
5138 // Ensure the display is rotated.
5139 prepareDisplay(DISPLAY_ORIENTATION_90);
5140
5141 NotifyMotionArgs args;
5142
5143 // Verify that the coordinates are rotated.
5144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5148 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5149 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5150 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5151 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5152
5153 // Enable Pointer Capture.
5154 mFakePolicy->setPointerCapture(true);
5155 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5156 NotifyPointerCaptureChangedArgs captureArgs;
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5158 ASSERT_TRUE(captureArgs.request.enable);
5159
5160 // Move and verify rotation is not applied.
5161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5165 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5167 ASSERT_EQ(10, args.pointerCoords[0].getX());
5168 ASSERT_EQ(20, args.pointerCoords[0].getY());
5169}
5170
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005171TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005172 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005173
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005174 // Set up the default display.
5175 prepareDisplay(DISPLAY_ORIENTATION_90);
5176
5177 // Set up the secondary display as the display on which the pointer should be shown.
5178 // The InputDevice is not associated with any display.
5179 prepareSecondaryDisplay();
5180 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005181 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5182
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005183 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005184 mFakePointerController->setPosition(100, 200);
5185 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005186
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005187 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005192 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5193 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5194 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005195 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005196}
5197
5198TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5199 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5200
5201 // Set up the default display.
5202 prepareDisplay(DISPLAY_ORIENTATION_90);
5203
5204 // Set up the secondary display as the display on which the pointer should be shown,
5205 // and associate the InputDevice with the secondary display.
5206 prepareSecondaryDisplay();
5207 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5208 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5209 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5210
5211 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5212 mFakePointerController->setPosition(100, 200);
5213 mFakePointerController->setButtonState(0);
5214
5215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005219 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5220 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5221 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005222 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5223}
5224
5225TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5226 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5227
5228 // Set up the default display as the display on which the pointer should be shown.
5229 prepareDisplay(DISPLAY_ORIENTATION_90);
5230 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5231
5232 // Associate the InputDevice with the secondary display.
5233 prepareSecondaryDisplay();
5234 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5235 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5236
5237 // The mapper should not generate any events because it is associated with a display that is
5238 // different from the pointer display.
5239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5241 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005243}
5244
Michael Wrightd02c5b62014-02-10 15:10:22 -08005245// --- TouchInputMapperTest ---
5246
5247class TouchInputMapperTest : public InputMapperTest {
5248protected:
5249 static const int32_t RAW_X_MIN;
5250 static const int32_t RAW_X_MAX;
5251 static const int32_t RAW_Y_MIN;
5252 static const int32_t RAW_Y_MAX;
5253 static const int32_t RAW_TOUCH_MIN;
5254 static const int32_t RAW_TOUCH_MAX;
5255 static const int32_t RAW_TOOL_MIN;
5256 static const int32_t RAW_TOOL_MAX;
5257 static const int32_t RAW_PRESSURE_MIN;
5258 static const int32_t RAW_PRESSURE_MAX;
5259 static const int32_t RAW_ORIENTATION_MIN;
5260 static const int32_t RAW_ORIENTATION_MAX;
5261 static const int32_t RAW_DISTANCE_MIN;
5262 static const int32_t RAW_DISTANCE_MAX;
5263 static const int32_t RAW_TILT_MIN;
5264 static const int32_t RAW_TILT_MAX;
5265 static const int32_t RAW_ID_MIN;
5266 static const int32_t RAW_ID_MAX;
5267 static const int32_t RAW_SLOT_MIN;
5268 static const int32_t RAW_SLOT_MAX;
5269 static const float X_PRECISION;
5270 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005271 static const float X_PRECISION_VIRTUAL;
5272 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273
5274 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005275 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276
5277 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5278
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005279 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005280 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005281
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282 enum Axes {
5283 POSITION = 1 << 0,
5284 TOUCH = 1 << 1,
5285 TOOL = 1 << 2,
5286 PRESSURE = 1 << 3,
5287 ORIENTATION = 1 << 4,
5288 MINOR = 1 << 5,
5289 ID = 1 << 6,
5290 DISTANCE = 1 << 7,
5291 TILT = 1 << 8,
5292 SLOT = 1 << 9,
5293 TOOL_TYPE = 1 << 10,
5294 };
5295
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005296 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5297 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005298 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005300 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005301 int32_t toRawX(float displayX);
5302 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005303 int32_t toRotatedRawX(float displayX);
5304 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005305 float toCookedX(float rawX, float rawY);
5306 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005307 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005308 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005310 float toDisplayY(int32_t rawY, int32_t displayHeight);
5311
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312};
5313
5314const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5315const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5316const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5317const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5318const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5319const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5320const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5321const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005322const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5323const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5325const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5326const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5327const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5328const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5329const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5330const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5331const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5332const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5333const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5334const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5335const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005336const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5337 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5338const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5339 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005340const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5341 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342
5343const float TouchInputMapperTest::GEOMETRIC_SCALE =
5344 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5345 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5346
5347const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5348 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5349 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5350};
5351
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005352void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005353 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5354 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005355}
5356
5357void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5358 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5359 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360}
5361
Santos Cordonfa5cf462017-04-05 10:37:00 -07005362void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005363 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5364 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5365 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005366}
5367
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005369 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5370 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5371 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5372 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373}
5374
Jason Gerecke489fda82012-09-07 17:19:40 -07005375void TouchInputMapperTest::prepareLocationCalibration() {
5376 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5377}
5378
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379int32_t TouchInputMapperTest::toRawX(float displayX) {
5380 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5381}
5382
5383int32_t TouchInputMapperTest::toRawY(float displayY) {
5384 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5385}
5386
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005387int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5388 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5389}
5390
5391int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5392 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5393}
5394
Jason Gerecke489fda82012-09-07 17:19:40 -07005395float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5396 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5397 return rawX;
5398}
5399
5400float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5401 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5402 return rawY;
5403}
5404
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005406 return toDisplayX(rawX, DISPLAY_WIDTH);
5407}
5408
5409float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5410 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411}
5412
5413float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005414 return toDisplayY(rawY, DISPLAY_HEIGHT);
5415}
5416
5417float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5418 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419}
5420
5421
5422// --- SingleTouchInputMapperTest ---
5423
5424class SingleTouchInputMapperTest : public TouchInputMapperTest {
5425protected:
5426 void prepareButtons();
5427 void prepareAxes(int axes);
5428
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005429 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5430 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5431 void processUp(SingleTouchInputMapper& mappery);
5432 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5433 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5434 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5435 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5436 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5437 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438};
5439
5440void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005441 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442}
5443
5444void SingleTouchInputMapperTest::prepareAxes(int axes) {
5445 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005446 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5447 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 }
5449 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005450 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5451 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452 }
5453 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005454 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5455 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456 }
5457 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005458 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5459 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460 }
5461 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005462 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5463 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464 }
5465}
5466
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005467void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471}
5472
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005473void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476}
5477
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005478void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480}
5481
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005482void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484}
5485
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005486void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5487 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489}
5490
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005491void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493}
5494
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005495void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5496 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499}
5500
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005501void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5502 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504}
5505
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005506void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508}
5509
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 prepareButtons();
5512 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005513 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005515 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516}
5517
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 prepareButtons();
5520 prepareAxes(POSITION);
5521 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005522 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005523
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005524 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005525}
5526
5527TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 addConfigurationProperty("touch.deviceType", "touchScreen");
5529 prepareDisplay(DISPLAY_ORIENTATION_0);
5530 prepareButtons();
5531 prepareAxes(POSITION);
5532 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005533 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534
5535 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005536 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537
5538 // Virtual key is down.
5539 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5540 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5541 processDown(mapper, x, y);
5542 processSync(mapper);
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005545 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546
5547 // Virtual key is up.
5548 processUp(mapper);
5549 processSync(mapper);
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5551
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005552 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553}
5554
5555TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 addConfigurationProperty("touch.deviceType", "touchScreen");
5557 prepareDisplay(DISPLAY_ORIENTATION_0);
5558 prepareButtons();
5559 prepareAxes(POSITION);
5560 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005561 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562
5563 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005564 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565
5566 // Virtual key is down.
5567 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5568 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5569 processDown(mapper, x, y);
5570 processSync(mapper);
5571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005573 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005574
5575 // Virtual key is up.
5576 processUp(mapper);
5577 processSync(mapper);
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005580 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581}
5582
5583TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 addConfigurationProperty("touch.deviceType", "touchScreen");
5585 prepareDisplay(DISPLAY_ORIENTATION_0);
5586 prepareButtons();
5587 prepareAxes(POSITION);
5588 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005589 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005592 ASSERT_TRUE(
5593 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 ASSERT_TRUE(flags[0]);
5595 ASSERT_FALSE(flags[1]);
5596}
5597
5598TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 addConfigurationProperty("touch.deviceType", "touchScreen");
5600 prepareDisplay(DISPLAY_ORIENTATION_0);
5601 prepareButtons();
5602 prepareAxes(POSITION);
5603 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005604 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605
arthurhungdcef2dc2020-08-11 14:47:50 +08005606 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607
5608 NotifyKeyArgs args;
5609
5610 // Press virtual key.
5611 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5612 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5613 processDown(mapper, x, y);
5614 processSync(mapper);
5615
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5617 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5618 ASSERT_EQ(DEVICE_ID, args.deviceId);
5619 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5620 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5621 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5622 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5623 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5624 ASSERT_EQ(KEY_HOME, args.scanCode);
5625 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5626 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5627
5628 // Release virtual key.
5629 processUp(mapper);
5630 processSync(mapper);
5631
5632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5633 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5634 ASSERT_EQ(DEVICE_ID, args.deviceId);
5635 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5636 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5637 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5638 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5639 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5640 ASSERT_EQ(KEY_HOME, args.scanCode);
5641 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5642 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5643
5644 // Should not have sent any motions.
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5646}
5647
5648TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 addConfigurationProperty("touch.deviceType", "touchScreen");
5650 prepareDisplay(DISPLAY_ORIENTATION_0);
5651 prepareButtons();
5652 prepareAxes(POSITION);
5653 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005654 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655
arthurhungdcef2dc2020-08-11 14:47:50 +08005656 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657
5658 NotifyKeyArgs keyArgs;
5659
5660 // Press virtual key.
5661 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5662 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5663 processDown(mapper, x, y);
5664 processSync(mapper);
5665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5667 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5668 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5669 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5670 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5671 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5672 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5673 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5674 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5675 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5676 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5677
5678 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5679 // into the display area.
5680 y -= 100;
5681 processMove(mapper, x, y);
5682 processSync(mapper);
5683
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5685 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5686 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5687 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5688 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5689 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5690 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5691 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5692 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5693 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5694 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5695 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5696
5697 NotifyMotionArgs motionArgs;
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5699 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5700 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5701 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5702 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5703 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5704 ASSERT_EQ(0, motionArgs.flags);
5705 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5706 ASSERT_EQ(0, motionArgs.buttonState);
5707 ASSERT_EQ(0, motionArgs.edgeFlags);
5708 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5709 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5712 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5713 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5714 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5715 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5716
5717 // Keep moving out of bounds. Should generate a pointer move.
5718 y -= 50;
5719 processMove(mapper, x, y);
5720 processSync(mapper);
5721
5722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5723 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5724 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5725 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5726 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5728 ASSERT_EQ(0, motionArgs.flags);
5729 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5730 ASSERT_EQ(0, motionArgs.buttonState);
5731 ASSERT_EQ(0, motionArgs.edgeFlags);
5732 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5733 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5736 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5737 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5738 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5739 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5740
5741 // Release out of bounds. Should generate a pointer up.
5742 processUp(mapper);
5743 processSync(mapper);
5744
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5746 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5747 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5748 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5749 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5750 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5751 ASSERT_EQ(0, motionArgs.flags);
5752 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5753 ASSERT_EQ(0, motionArgs.buttonState);
5754 ASSERT_EQ(0, motionArgs.edgeFlags);
5755 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5756 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5759 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5760 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5761 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5762 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5763
5764 // Should not have sent any more keys or motions.
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5767}
5768
5769TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770 addConfigurationProperty("touch.deviceType", "touchScreen");
5771 prepareDisplay(DISPLAY_ORIENTATION_0);
5772 prepareButtons();
5773 prepareAxes(POSITION);
5774 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005775 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005776
arthurhungdcef2dc2020-08-11 14:47:50 +08005777 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778
5779 NotifyMotionArgs motionArgs;
5780
5781 // Initially go down out of bounds.
5782 int32_t x = -10;
5783 int32_t y = -10;
5784 processDown(mapper, x, y);
5785 processSync(mapper);
5786
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5788
5789 // Move into the display area. Should generate a pointer down.
5790 x = 50;
5791 y = 75;
5792 processMove(mapper, x, y);
5793 processSync(mapper);
5794
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5797 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5798 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5799 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5801 ASSERT_EQ(0, motionArgs.flags);
5802 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5803 ASSERT_EQ(0, motionArgs.buttonState);
5804 ASSERT_EQ(0, motionArgs.edgeFlags);
5805 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5806 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5809 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5810 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5811 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5812 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5813
5814 // Release. Should generate a pointer up.
5815 processUp(mapper);
5816 processSync(mapper);
5817
5818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5819 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5820 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5821 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5822 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5824 ASSERT_EQ(0, motionArgs.flags);
5825 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5826 ASSERT_EQ(0, motionArgs.buttonState);
5827 ASSERT_EQ(0, motionArgs.edgeFlags);
5828 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5829 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5832 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5833 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5834 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5835 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5836
5837 // Should not have sent any more keys or motions.
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5840}
5841
Santos Cordonfa5cf462017-04-05 10:37:00 -07005842TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005843 addConfigurationProperty("touch.deviceType", "touchScreen");
5844 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5845
5846 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5847 prepareButtons();
5848 prepareAxes(POSITION);
5849 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005850 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005851
arthurhungdcef2dc2020-08-11 14:47:50 +08005852 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005853
5854 NotifyMotionArgs motionArgs;
5855
5856 // Down.
5857 int32_t x = 100;
5858 int32_t y = 125;
5859 processDown(mapper, x, y);
5860 processSync(mapper);
5861
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5863 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5864 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5865 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5866 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5867 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5868 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5869 ASSERT_EQ(0, motionArgs.flags);
5870 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5871 ASSERT_EQ(0, motionArgs.buttonState);
5872 ASSERT_EQ(0, motionArgs.edgeFlags);
5873 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5874 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5877 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5878 1, 0, 0, 0, 0, 0, 0, 0));
5879 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5880 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5881 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5882
5883 // Move.
5884 x += 50;
5885 y += 75;
5886 processMove(mapper, x, y);
5887 processSync(mapper);
5888
5889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5890 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5891 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5892 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5893 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5894 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5896 ASSERT_EQ(0, motionArgs.flags);
5897 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5898 ASSERT_EQ(0, motionArgs.buttonState);
5899 ASSERT_EQ(0, motionArgs.edgeFlags);
5900 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5901 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5904 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5905 1, 0, 0, 0, 0, 0, 0, 0));
5906 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5907 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5908 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5909
5910 // Up.
5911 processUp(mapper);
5912 processSync(mapper);
5913
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5915 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5916 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5917 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5918 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5919 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5920 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5921 ASSERT_EQ(0, motionArgs.flags);
5922 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5923 ASSERT_EQ(0, motionArgs.buttonState);
5924 ASSERT_EQ(0, motionArgs.edgeFlags);
5925 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5926 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5929 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5930 1, 0, 0, 0, 0, 0, 0, 0));
5931 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5932 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5933 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5934
5935 // Should not have sent any more keys or motions.
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5938}
5939
Michael Wrightd02c5b62014-02-10 15:10:22 -08005940TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 addConfigurationProperty("touch.deviceType", "touchScreen");
5942 prepareDisplay(DISPLAY_ORIENTATION_0);
5943 prepareButtons();
5944 prepareAxes(POSITION);
5945 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005946 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947
arthurhungdcef2dc2020-08-11 14:47:50 +08005948 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949
5950 NotifyMotionArgs motionArgs;
5951
5952 // Down.
5953 int32_t x = 100;
5954 int32_t y = 125;
5955 processDown(mapper, x, y);
5956 processSync(mapper);
5957
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5959 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5960 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5961 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5962 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5963 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5964 ASSERT_EQ(0, motionArgs.flags);
5965 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5966 ASSERT_EQ(0, motionArgs.buttonState);
5967 ASSERT_EQ(0, motionArgs.edgeFlags);
5968 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5969 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5972 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5973 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5974 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5975 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5976
5977 // Move.
5978 x += 50;
5979 y += 75;
5980 processMove(mapper, x, y);
5981 processSync(mapper);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5985 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5986 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5987 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5988 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5989 ASSERT_EQ(0, motionArgs.flags);
5990 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5991 ASSERT_EQ(0, motionArgs.buttonState);
5992 ASSERT_EQ(0, motionArgs.edgeFlags);
5993 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5994 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5997 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5998 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5999 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6000 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6001
6002 // Up.
6003 processUp(mapper);
6004 processSync(mapper);
6005
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6008 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6009 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6010 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6011 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6012 ASSERT_EQ(0, motionArgs.flags);
6013 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6014 ASSERT_EQ(0, motionArgs.buttonState);
6015 ASSERT_EQ(0, motionArgs.edgeFlags);
6016 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6017 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6020 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6021 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6022 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6023 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6024
6025 // Should not have sent any more keys or motions.
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6028}
6029
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006030TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 addConfigurationProperty("touch.deviceType", "touchScreen");
6032 prepareButtons();
6033 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006034 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6035 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006036 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037
6038 NotifyMotionArgs args;
6039
6040 // Rotation 90.
6041 prepareDisplay(DISPLAY_ORIENTATION_90);
6042 processDown(mapper, toRawX(50), toRawY(75));
6043 processSync(mapper);
6044
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6046 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6047 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6048
6049 processUp(mapper);
6050 processSync(mapper);
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6052}
6053
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006054TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055 addConfigurationProperty("touch.deviceType", "touchScreen");
6056 prepareButtons();
6057 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006058 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6059 // orientation-aware are affected by display rotation.
6060 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006061 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062
6063 NotifyMotionArgs args;
6064
6065 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006066 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067 prepareDisplay(DISPLAY_ORIENTATION_0);
6068 processDown(mapper, toRawX(50), toRawY(75));
6069 processSync(mapper);
6070
6071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6072 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6073 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6074
6075 processUp(mapper);
6076 processSync(mapper);
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6078
6079 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006080 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006082 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083 processSync(mapper);
6084
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6086 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6087 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6088
6089 processUp(mapper);
6090 processSync(mapper);
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6092
6093 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006094 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095 prepareDisplay(DISPLAY_ORIENTATION_180);
6096 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6097 processSync(mapper);
6098
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6100 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6101 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6102
6103 processUp(mapper);
6104 processSync(mapper);
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6106
6107 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006108 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006110 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 processSync(mapper);
6112
6113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6114 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6115 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6116
6117 processUp(mapper);
6118 processSync(mapper);
6119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6120}
6121
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006122TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6123 addConfigurationProperty("touch.deviceType", "touchScreen");
6124 prepareButtons();
6125 prepareAxes(POSITION);
6126 addConfigurationProperty("touch.orientationAware", "1");
6127 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6128 clearViewports();
6129 prepareDisplay(DISPLAY_ORIENTATION_0);
6130 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6131 NotifyMotionArgs args;
6132
6133 // Orientation 0.
6134 processDown(mapper, toRawX(50), toRawY(75));
6135 processSync(mapper);
6136
6137 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6138 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6139 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6140
6141 processUp(mapper);
6142 processSync(mapper);
6143 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6144}
6145
6146TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6147 addConfigurationProperty("touch.deviceType", "touchScreen");
6148 prepareButtons();
6149 prepareAxes(POSITION);
6150 addConfigurationProperty("touch.orientationAware", "1");
6151 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6152 clearViewports();
6153 prepareDisplay(DISPLAY_ORIENTATION_0);
6154 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6155 NotifyMotionArgs args;
6156
6157 // Orientation 90.
6158 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6159 processSync(mapper);
6160
6161 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6162 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6163 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6164
6165 processUp(mapper);
6166 processSync(mapper);
6167 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6168}
6169
6170TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6171 addConfigurationProperty("touch.deviceType", "touchScreen");
6172 prepareButtons();
6173 prepareAxes(POSITION);
6174 addConfigurationProperty("touch.orientationAware", "1");
6175 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6176 clearViewports();
6177 prepareDisplay(DISPLAY_ORIENTATION_0);
6178 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6179 NotifyMotionArgs args;
6180
6181 // Orientation 180.
6182 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6183 processSync(mapper);
6184
6185 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6186 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6187 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6188
6189 processUp(mapper);
6190 processSync(mapper);
6191 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6192}
6193
6194TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6195 addConfigurationProperty("touch.deviceType", "touchScreen");
6196 prepareButtons();
6197 prepareAxes(POSITION);
6198 addConfigurationProperty("touch.orientationAware", "1");
6199 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6200 clearViewports();
6201 prepareDisplay(DISPLAY_ORIENTATION_0);
6202 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6203 NotifyMotionArgs args;
6204
6205 // Orientation 270.
6206 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6207 processSync(mapper);
6208
6209 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6210 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6211 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6212
6213 processUp(mapper);
6214 processSync(mapper);
6215 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6216}
6217
6218TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6219 addConfigurationProperty("touch.deviceType", "touchScreen");
6220 prepareButtons();
6221 prepareAxes(POSITION);
6222 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6223 // orientation-aware are affected by display rotation.
6224 addConfigurationProperty("touch.orientationAware", "0");
6225 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6226 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6227
6228 NotifyMotionArgs args;
6229
6230 // Orientation 90, Rotation 0.
6231 clearViewports();
6232 prepareDisplay(DISPLAY_ORIENTATION_0);
6233 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6234 processSync(mapper);
6235
6236 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6237 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6238 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6239
6240 processUp(mapper);
6241 processSync(mapper);
6242 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6243
6244 // Orientation 90, Rotation 90.
6245 clearViewports();
6246 prepareDisplay(DISPLAY_ORIENTATION_90);
6247 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6248 processSync(mapper);
6249
6250 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6251 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6252 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6253
6254 processUp(mapper);
6255 processSync(mapper);
6256 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6257
6258 // Orientation 90, Rotation 180.
6259 clearViewports();
6260 prepareDisplay(DISPLAY_ORIENTATION_180);
6261 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6262 processSync(mapper);
6263
6264 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6265 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6266 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6267
6268 processUp(mapper);
6269 processSync(mapper);
6270 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6271
6272 // Orientation 90, Rotation 270.
6273 clearViewports();
6274 prepareDisplay(DISPLAY_ORIENTATION_270);
6275 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6276 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6277 processSync(mapper);
6278
6279 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6280 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6281 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6282
6283 processUp(mapper);
6284 processSync(mapper);
6285 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6286}
6287
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289 addConfigurationProperty("touch.deviceType", "touchScreen");
6290 prepareDisplay(DISPLAY_ORIENTATION_0);
6291 prepareButtons();
6292 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006293 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006294
6295 // These calculations are based on the input device calibration documentation.
6296 int32_t rawX = 100;
6297 int32_t rawY = 200;
6298 int32_t rawPressure = 10;
6299 int32_t rawToolMajor = 12;
6300 int32_t rawDistance = 2;
6301 int32_t rawTiltX = 30;
6302 int32_t rawTiltY = 110;
6303
6304 float x = toDisplayX(rawX);
6305 float y = toDisplayY(rawY);
6306 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6307 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6308 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6309 float distance = float(rawDistance);
6310
6311 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6312 float tiltScale = M_PI / 180;
6313 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6314 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6315 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6316 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6317
6318 processDown(mapper, rawX, rawY);
6319 processPressure(mapper, rawPressure);
6320 processToolMajor(mapper, rawToolMajor);
6321 processDistance(mapper, rawDistance);
6322 processTilt(mapper, rawTiltX, rawTiltY);
6323 processSync(mapper);
6324
6325 NotifyMotionArgs args;
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6328 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6329 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6330}
6331
Jason Gerecke489fda82012-09-07 17:19:40 -07006332TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006333 addConfigurationProperty("touch.deviceType", "touchScreen");
6334 prepareDisplay(DISPLAY_ORIENTATION_0);
6335 prepareLocationCalibration();
6336 prepareButtons();
6337 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006338 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006339
6340 int32_t rawX = 100;
6341 int32_t rawY = 200;
6342
6343 float x = toDisplayX(toCookedX(rawX, rawY));
6344 float y = toDisplayY(toCookedY(rawX, rawY));
6345
6346 processDown(mapper, rawX, rawY);
6347 processSync(mapper);
6348
6349 NotifyMotionArgs args;
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6352 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6353}
6354
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356 addConfigurationProperty("touch.deviceType", "touchScreen");
6357 prepareDisplay(DISPLAY_ORIENTATION_0);
6358 prepareButtons();
6359 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006360 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361
6362 NotifyMotionArgs motionArgs;
6363 NotifyKeyArgs keyArgs;
6364
6365 processDown(mapper, 100, 200);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6369 ASSERT_EQ(0, motionArgs.buttonState);
6370
6371 // press BTN_LEFT, release BTN_LEFT
6372 processKey(mapper, BTN_LEFT, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6376 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6377
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6380 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6381
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382 processKey(mapper, BTN_LEFT, 0);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391
6392 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6393 processKey(mapper, BTN_RIGHT, 1);
6394 processKey(mapper, BTN_MIDDLE, 1);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6398 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6399 motionArgs.buttonState);
6400
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6403 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6404
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6407 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6408 motionArgs.buttonState);
6409
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410 processKey(mapper, BTN_RIGHT, 0);
6411 processSync(mapper);
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006413 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006418 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419
6420 processKey(mapper, BTN_MIDDLE, 0);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006425
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006428 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429
6430 // press BTN_BACK, release BTN_BACK
6431 processKey(mapper, BTN_BACK, 1);
6432 processSync(mapper);
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6434 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6435 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006436
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6440
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6442 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6443 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444
6445 processKey(mapper, BTN_BACK, 0);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006449 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006450
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006453 ASSERT_EQ(0, motionArgs.buttonState);
6454
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6456 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6457 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6458
6459 // press BTN_SIDE, release BTN_SIDE
6460 processKey(mapper, BTN_SIDE, 1);
6461 processSync(mapper);
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6463 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6464 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006465
Michael Wrightd02c5b62014-02-10 15:10:22 -08006466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006468 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6469
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6472 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473
6474 processKey(mapper, BTN_SIDE, 0);
6475 processSync(mapper);
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006482 ASSERT_EQ(0, motionArgs.buttonState);
6483
Michael Wrightd02c5b62014-02-10 15:10:22 -08006484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6485 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6486 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6487
6488 // press BTN_FORWARD, release BTN_FORWARD
6489 processKey(mapper, BTN_FORWARD, 1);
6490 processSync(mapper);
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6492 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6493 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006494
Michael Wrightd02c5b62014-02-10 15:10:22 -08006495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006497 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6498
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6500 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6501 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006502
6503 processKey(mapper, BTN_FORWARD, 0);
6504 processSync(mapper);
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006507 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006508
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006511 ASSERT_EQ(0, motionArgs.buttonState);
6512
Michael Wrightd02c5b62014-02-10 15:10:22 -08006513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6514 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6515 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6516
6517 // press BTN_EXTRA, release BTN_EXTRA
6518 processKey(mapper, BTN_EXTRA, 1);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6521 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6522 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006523
Michael Wrightd02c5b62014-02-10 15:10:22 -08006524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006526 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6527
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6529 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6530 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006531
6532 processKey(mapper, BTN_EXTRA, 0);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006535 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006536 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006537
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006540 ASSERT_EQ(0, motionArgs.buttonState);
6541
Michael Wrightd02c5b62014-02-10 15:10:22 -08006542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6543 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6544 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6545
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6547
Michael Wrightd02c5b62014-02-10 15:10:22 -08006548 // press BTN_STYLUS, release BTN_STYLUS
6549 processKey(mapper, BTN_STYLUS, 1);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006553 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6554
6555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6557 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006558
6559 processKey(mapper, BTN_STYLUS, 0);
6560 processSync(mapper);
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006562 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006564
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006567 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006568
6569 // press BTN_STYLUS2, release BTN_STYLUS2
6570 processKey(mapper, BTN_STYLUS2, 1);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006574 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6575
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6577 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6578 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006579
6580 processKey(mapper, BTN_STYLUS2, 0);
6581 processSync(mapper);
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006583 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006584 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006585
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006588 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006589
6590 // release touch
6591 processUp(mapper);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6595 ASSERT_EQ(0, motionArgs.buttonState);
6596}
6597
6598TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006599 addConfigurationProperty("touch.deviceType", "touchScreen");
6600 prepareDisplay(DISPLAY_ORIENTATION_0);
6601 prepareButtons();
6602 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006603 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006604
6605 NotifyMotionArgs motionArgs;
6606
6607 // default tool type is finger
6608 processDown(mapper, 100, 200);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6613
6614 // eraser
6615 processKey(mapper, BTN_TOOL_RUBBER, 1);
6616 processSync(mapper);
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6620
6621 // stylus
6622 processKey(mapper, BTN_TOOL_RUBBER, 0);
6623 processKey(mapper, BTN_TOOL_PEN, 1);
6624 processSync(mapper);
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6628
6629 // brush
6630 processKey(mapper, BTN_TOOL_PEN, 0);
6631 processKey(mapper, BTN_TOOL_BRUSH, 1);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6636
6637 // pencil
6638 processKey(mapper, BTN_TOOL_BRUSH, 0);
6639 processKey(mapper, BTN_TOOL_PENCIL, 1);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6644
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006645 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006646 processKey(mapper, BTN_TOOL_PENCIL, 0);
6647 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6648 processSync(mapper);
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6652
6653 // mouse
6654 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6655 processKey(mapper, BTN_TOOL_MOUSE, 1);
6656 processSync(mapper);
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6660
6661 // lens
6662 processKey(mapper, BTN_TOOL_MOUSE, 0);
6663 processKey(mapper, BTN_TOOL_LENS, 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 // double-tap
6670 processKey(mapper, BTN_TOOL_LENS, 0);
6671 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6672 processSync(mapper);
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6676
6677 // triple-tap
6678 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6679 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6680 processSync(mapper);
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6683 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6684
6685 // quad-tap
6686 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6687 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6688 processSync(mapper);
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6691 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6692
6693 // finger
6694 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6695 processKey(mapper, BTN_TOOL_FINGER, 1);
6696 processSync(mapper);
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6700
6701 // stylus trumps finger
6702 processKey(mapper, BTN_TOOL_PEN, 1);
6703 processSync(mapper);
6704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6707
6708 // eraser trumps stylus
6709 processKey(mapper, BTN_TOOL_RUBBER, 1);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6714
6715 // mouse trumps eraser
6716 processKey(mapper, BTN_TOOL_MOUSE, 1);
6717 processSync(mapper);
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6721
6722 // back to default tool type
6723 processKey(mapper, BTN_TOOL_MOUSE, 0);
6724 processKey(mapper, BTN_TOOL_RUBBER, 0);
6725 processKey(mapper, BTN_TOOL_PEN, 0);
6726 processKey(mapper, BTN_TOOL_FINGER, 0);
6727 processSync(mapper);
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6731}
6732
6733TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006734 addConfigurationProperty("touch.deviceType", "touchScreen");
6735 prepareDisplay(DISPLAY_ORIENTATION_0);
6736 prepareButtons();
6737 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006738 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006739 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006740
6741 NotifyMotionArgs motionArgs;
6742
6743 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6744 processKey(mapper, BTN_TOOL_FINGER, 1);
6745 processMove(mapper, 100, 200);
6746 processSync(mapper);
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6751
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6755 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6756
6757 // move a little
6758 processMove(mapper, 150, 250);
6759 processSync(mapper);
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6763 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6764
6765 // down when BTN_TOUCH is pressed, pressure defaults to 1
6766 processKey(mapper, BTN_TOUCH, 1);
6767 processSync(mapper);
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6769 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6771 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6772
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6774 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6776 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6777
6778 // up when BTN_TOUCH is released, hover restored
6779 processKey(mapper, BTN_TOUCH, 0);
6780 processSync(mapper);
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6782 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6784 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6785
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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_HOVER_MOVE, motionArgs.action);
6793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6794 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6795
6796 // exit hover when pointer goes away
6797 processKey(mapper, BTN_TOOL_FINGER, 0);
6798 processSync(mapper);
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6802 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6803}
6804
6805TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006806 addConfigurationProperty("touch.deviceType", "touchScreen");
6807 prepareDisplay(DISPLAY_ORIENTATION_0);
6808 prepareButtons();
6809 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006810 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006811
6812 NotifyMotionArgs motionArgs;
6813
6814 // initially hovering because pressure is 0
6815 processDown(mapper, 100, 200);
6816 processPressure(mapper, 0);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6819 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6821 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6822
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6824 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6826 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6827
6828 // move a little
6829 processMove(mapper, 150, 250);
6830 processSync(mapper);
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6832 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6834 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6835
6836 // down when pressure is non-zero
6837 processPressure(mapper, RAW_PRESSURE_MAX);
6838 processSync(mapper);
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6840 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6842 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6843
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6845 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6847 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6848
6849 // up when pressure becomes 0, hover restored
6850 processPressure(mapper, 0);
6851 processSync(mapper);
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6853 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6855 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6856
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6858 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6860 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6861
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6863 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6865 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6866
6867 // exit hover when pointer goes away
6868 processUp(mapper);
6869 processSync(mapper);
6870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6871 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6873 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6874}
6875
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006876TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6877 addConfigurationProperty("touch.deviceType", "touchScreen");
6878 prepareDisplay(DISPLAY_ORIENTATION_0);
6879 prepareButtons();
6880 prepareAxes(POSITION | PRESSURE);
6881 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6882
6883 // Touch down.
6884 processDown(mapper, 100, 200);
6885 processPressure(mapper, 1);
6886 processSync(mapper);
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6888 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6889
6890 // Reset the mapper. This should cancel the ongoing gesture.
6891 resetMapper(mapper, ARBITRARY_TIME);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6893 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6894
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6896}
6897
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006898TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6899 addConfigurationProperty("touch.deviceType", "touchScreen");
6900 prepareDisplay(DISPLAY_ORIENTATION_0);
6901 prepareButtons();
6902 prepareAxes(POSITION | PRESSURE);
6903 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6904
6905 // Set the initial state for the touch pointer.
6906 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6907 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6908 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6909 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6910
6911 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006912 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6913 // does not generate any events.
6914 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006915
6916 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6917 // the recreated touch state to generate a down event.
6918 processSync(mapper);
6919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6920 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6921
6922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6923}
6924
lilinnan687e58f2022-07-19 16:00:50 +08006925TEST_F(SingleTouchInputMapperTest,
6926 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6927 addConfigurationProperty("touch.deviceType", "touchScreen");
6928 prepareDisplay(DISPLAY_ORIENTATION_0);
6929 prepareButtons();
6930 prepareAxes(POSITION);
6931 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6932 NotifyMotionArgs motionArgs;
6933
6934 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006935 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006936 processSync(mapper);
6937
6938 // We should receive a down event
6939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6940 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6941
6942 // Change display id
6943 clearViewports();
6944 prepareSecondaryDisplay(ViewportType::INTERNAL);
6945
6946 // We should receive a cancel event
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6948 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6949 // Then receive reset called
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6951}
6952
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006953TEST_F(SingleTouchInputMapperTest,
6954 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6955 addConfigurationProperty("touch.deviceType", "touchScreen");
6956 prepareDisplay(DISPLAY_ORIENTATION_0);
6957 prepareButtons();
6958 prepareAxes(POSITION);
6959 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6961 NotifyMotionArgs motionArgs;
6962
6963 // Start a new gesture.
6964 processDown(mapper, 100, 200);
6965 processSync(mapper);
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6967 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6968
6969 // Make the viewport inactive. This will put the device in disabled mode.
6970 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6971 viewport->isActive = false;
6972 mFakePolicy->updateViewport(*viewport);
6973 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6974
6975 // We should receive a cancel event for the ongoing gesture.
6976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6977 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6978 // Then we should be notified that the device was reset.
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6980
6981 // No events are generated while the viewport is inactive.
6982 processMove(mapper, 101, 201);
6983 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006984 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006985 processSync(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6987
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006988 // Start a new gesture while the viewport is still inactive.
6989 processDown(mapper, 300, 400);
6990 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6991 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6992 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6993 processSync(mapper);
6994
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006995 // Make the viewport active again. The device should resume processing events.
6996 viewport->isActive = true;
6997 mFakePolicy->updateViewport(*viewport);
6998 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6999
7000 // The device is reset because it changes back to direct mode, without generating any events.
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7003
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007004 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007005 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7007 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007008
7009 // No more events.
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7012}
7013
Prabir Pradhan5632d622021-09-06 07:57:20 -07007014// --- TouchDisplayProjectionTest ---
7015
7016class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7017public:
7018 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7019 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7020 // rotated equivalent of the given un-rotated physical display bounds.
7021 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7022 uint32_t inverseRotationFlags;
7023 auto width = DISPLAY_WIDTH;
7024 auto height = DISPLAY_HEIGHT;
7025 switch (orientation) {
7026 case DISPLAY_ORIENTATION_90:
7027 inverseRotationFlags = ui::Transform::ROT_270;
7028 std::swap(width, height);
7029 break;
7030 case DISPLAY_ORIENTATION_180:
7031 inverseRotationFlags = ui::Transform::ROT_180;
7032 break;
7033 case DISPLAY_ORIENTATION_270:
7034 inverseRotationFlags = ui::Transform::ROT_90;
7035 std::swap(width, height);
7036 break;
7037 case DISPLAY_ORIENTATION_0:
7038 inverseRotationFlags = ui::Transform::ROT_0;
7039 break;
7040 default:
7041 FAIL() << "Invalid orientation: " << orientation;
7042 }
7043
7044 const ui::Transform rotation(inverseRotationFlags, width, height);
7045 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7046
7047 std::optional<DisplayViewport> internalViewport =
7048 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7049 DisplayViewport& v = *internalViewport;
7050 v.displayId = DISPLAY_ID;
7051 v.orientation = orientation;
7052
7053 v.logicalLeft = 0;
7054 v.logicalTop = 0;
7055 v.logicalRight = 100;
7056 v.logicalBottom = 100;
7057
7058 v.physicalLeft = rotatedPhysicalDisplay.left;
7059 v.physicalTop = rotatedPhysicalDisplay.top;
7060 v.physicalRight = rotatedPhysicalDisplay.right;
7061 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7062
7063 v.deviceWidth = width;
7064 v.deviceHeight = height;
7065
7066 v.isActive = true;
7067 v.uniqueId = UNIQUE_ID;
7068 v.type = ViewportType::INTERNAL;
7069 mFakePolicy->updateViewport(v);
7070 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7071 }
7072
7073 void assertReceivedMove(const Point& point) {
7074 NotifyMotionArgs motionArgs;
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7077 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7079 1, 0, 0, 0, 0, 0, 0, 0));
7080 }
7081};
7082
7083TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7084 addConfigurationProperty("touch.deviceType", "touchScreen");
7085 prepareDisplay(DISPLAY_ORIENTATION_0);
7086
7087 prepareButtons();
7088 prepareAxes(POSITION);
7089 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7090
7091 NotifyMotionArgs motionArgs;
7092
7093 // Configure the DisplayViewport such that the logical display maps to a subsection of
7094 // the display panel called the physical display. Here, the physical display is bounded by the
7095 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7096 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7097 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7098 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7099
7100 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7101 DISPLAY_ORIENTATION_270}) {
7102 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7103
7104 // Touches outside the physical display should be ignored, and should not generate any
7105 // events. Ensure touches at the following points that lie outside of the physical display
7106 // area do not generate any events.
7107 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7108 processDown(mapper, toRawX(point.x), toRawY(point.y));
7109 processSync(mapper);
7110 processUp(mapper);
7111 processSync(mapper);
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7113 << "Unexpected event generated for touch outside physical display at point: "
7114 << point.x << ", " << point.y;
7115 }
7116 }
7117}
7118
7119TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7120 addConfigurationProperty("touch.deviceType", "touchScreen");
7121 prepareDisplay(DISPLAY_ORIENTATION_0);
7122
7123 prepareButtons();
7124 prepareAxes(POSITION);
7125 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7126
7127 NotifyMotionArgs motionArgs;
7128
7129 // Configure the DisplayViewport such that the logical display maps to a subsection of
7130 // the display panel called the physical display. Here, the physical display is bounded by the
7131 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7132 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7133
7134 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7135 DISPLAY_ORIENTATION_270}) {
7136 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7137
7138 // Touches that start outside the physical display should be ignored until it enters the
7139 // physical display bounds, at which point it should generate a down event. Start a touch at
7140 // the point (5, 100), which is outside the physical display bounds.
7141 static const Point kOutsidePoint{5, 100};
7142 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7143 processSync(mapper);
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7145
7146 // Move the touch into the physical display area. This should generate a pointer down.
7147 processMove(mapper, toRawX(11), toRawY(21));
7148 processSync(mapper);
7149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7151 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7152 ASSERT_NO_FATAL_FAILURE(
7153 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7154
7155 // Move the touch inside the physical display area. This should generate a pointer move.
7156 processMove(mapper, toRawX(69), toRawY(159));
7157 processSync(mapper);
7158 assertReceivedMove({69, 159});
7159
7160 // Move outside the physical display area. Since the pointer is already down, this should
7161 // now continue generating events.
7162 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7163 processSync(mapper);
7164 assertReceivedMove(kOutsidePoint);
7165
7166 // Release. This should generate a pointer up.
7167 processUp(mapper);
7168 processSync(mapper);
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7170 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7172 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7173
7174 // Ensure no more events were generated.
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7177 }
7178}
7179
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180// --- MultiTouchInputMapperTest ---
7181
7182class MultiTouchInputMapperTest : public TouchInputMapperTest {
7183protected:
7184 void prepareAxes(int axes);
7185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007186 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7187 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7188 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7189 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7190 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7191 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7192 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7193 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7194 void processId(MultiTouchInputMapper& mapper, int32_t id);
7195 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7196 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7197 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7198 void processMTSync(MultiTouchInputMapper& mapper);
7199 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200};
7201
7202void MultiTouchInputMapperTest::prepareAxes(int axes) {
7203 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007204 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7205 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206 }
7207 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007208 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7209 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007211 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7212 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007213 }
7214 }
7215 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007216 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7217 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007220 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007221 }
7222 }
7223 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007224 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7225 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226 }
7227 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007228 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7229 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230 }
7231 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007232 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7233 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007234 }
7235 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007236 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7237 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007238 }
7239 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007240 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7241 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007242 }
7243 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007244 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007245 }
7246}
7247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007248void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7249 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007252}
7253
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007254void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7255 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007257}
7258
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007259void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7260 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007262}
7263
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007264void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007266}
7267
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007268void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007270}
7271
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007272void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7273 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007275}
7276
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007277void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007279}
7280
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007281void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007283}
7284
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007285void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007287}
7288
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007289void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007291}
7292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007293void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007295}
7296
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007297void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7298 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007300}
7301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007302void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304}
7305
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007306void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007308}
7309
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007311 addConfigurationProperty("touch.deviceType", "touchScreen");
7312 prepareDisplay(DISPLAY_ORIENTATION_0);
7313 prepareAxes(POSITION);
7314 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007315 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007316
arthurhungdcef2dc2020-08-11 14:47:50 +08007317 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007318
7319 NotifyMotionArgs motionArgs;
7320
7321 // Two fingers down at once.
7322 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7323 processPosition(mapper, x1, y1);
7324 processMTSync(mapper);
7325 processPosition(mapper, x2, y2);
7326 processMTSync(mapper);
7327 processSync(mapper);
7328
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7330 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7331 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7332 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7333 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7334 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7335 ASSERT_EQ(0, motionArgs.flags);
7336 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7337 ASSERT_EQ(0, motionArgs.buttonState);
7338 ASSERT_EQ(0, motionArgs.edgeFlags);
7339 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7343 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7344 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7345 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7347
7348 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);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007353 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007354 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(2), motionArgs.pointerCount);
7359 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7361 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7364 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7366 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7367 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7368 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7370
7371 // Move.
7372 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7373 processPosition(mapper, x1, y1);
7374 processMTSync(mapper);
7375 processPosition(mapper, x2, y2);
7376 processMTSync(mapper);
7377 processSync(mapper);
7378
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7381 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7382 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7383 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7385 ASSERT_EQ(0, motionArgs.flags);
7386 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7387 ASSERT_EQ(0, motionArgs.buttonState);
7388 ASSERT_EQ(0, motionArgs.edgeFlags);
7389 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7390 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7392 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7395 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7397 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7398 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7399 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7400 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7401
7402 // First finger up.
7403 x2 += 15; y2 -= 20;
7404 processPosition(mapper, x2, y2);
7405 processMTSync(mapper);
7406 processSync(mapper);
7407
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7410 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7411 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7412 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007413 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007414 ASSERT_EQ(0, motionArgs.flags);
7415 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7416 ASSERT_EQ(0, motionArgs.buttonState);
7417 ASSERT_EQ(0, motionArgs.edgeFlags);
7418 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7419 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7421 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7424 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7426 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7427 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7428 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7430
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7432 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7433 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7434 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7435 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7437 ASSERT_EQ(0, motionArgs.flags);
7438 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7439 ASSERT_EQ(0, motionArgs.buttonState);
7440 ASSERT_EQ(0, motionArgs.edgeFlags);
7441 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7442 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7445 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7446 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7447 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7448 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7449
7450 // Move.
7451 x2 += 20; y2 -= 25;
7452 processPosition(mapper, x2, y2);
7453 processMTSync(mapper);
7454 processSync(mapper);
7455
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7458 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7459 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7460 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7462 ASSERT_EQ(0, motionArgs.flags);
7463 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7464 ASSERT_EQ(0, motionArgs.buttonState);
7465 ASSERT_EQ(0, motionArgs.edgeFlags);
7466 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7467 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7470 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7471 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7472 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7473 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7474
7475 // New finger down.
7476 int32_t x3 = 700, y3 = 300;
7477 processPosition(mapper, x2, y2);
7478 processMTSync(mapper);
7479 processPosition(mapper, x3, y3);
7480 processMTSync(mapper);
7481 processSync(mapper);
7482
7483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7484 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7485 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7486 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7487 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007488 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007489 ASSERT_EQ(0, motionArgs.flags);
7490 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7491 ASSERT_EQ(0, motionArgs.buttonState);
7492 ASSERT_EQ(0, motionArgs.edgeFlags);
7493 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7496 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7499 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7501 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7502 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7503 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7504 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7505
7506 // Second finger up.
7507 x3 += 30; y3 -= 20;
7508 processPosition(mapper, x3, y3);
7509 processMTSync(mapper);
7510 processSync(mapper);
7511
7512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7513 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7514 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7515 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7516 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007517 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007518 ASSERT_EQ(0, motionArgs.flags);
7519 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7520 ASSERT_EQ(0, motionArgs.buttonState);
7521 ASSERT_EQ(0, motionArgs.edgeFlags);
7522 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7523 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7525 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7528 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7530 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7531 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7532 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7533 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7534
7535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7536 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7537 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7538 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7539 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7541 ASSERT_EQ(0, motionArgs.flags);
7542 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7543 ASSERT_EQ(0, motionArgs.buttonState);
7544 ASSERT_EQ(0, motionArgs.edgeFlags);
7545 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7546 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7549 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7550 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7551 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7553
7554 // Last finger up.
7555 processMTSync(mapper);
7556 processSync(mapper);
7557
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7560 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7561 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7562 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7563 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7564 ASSERT_EQ(0, motionArgs.flags);
7565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7566 ASSERT_EQ(0, motionArgs.buttonState);
7567 ASSERT_EQ(0, motionArgs.edgeFlags);
7568 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7572 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7573 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7574 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7575 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7576
7577 // Should not have sent any more keys or motions.
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7580}
7581
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007582TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7583 addConfigurationProperty("touch.deviceType", "touchScreen");
7584 prepareDisplay(DISPLAY_ORIENTATION_0);
7585
7586 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7587 /*fuzz*/ 0, /*resolution*/ 10);
7588 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7589 /*fuzz*/ 0, /*resolution*/ 11);
7590 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7591 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7592 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7593 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7594 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7595 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7596 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7597 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7598
7599 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7600
7601 // X and Y axes
7602 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7603 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7604 // Touch major and minor
7605 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7606 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7607 // Tool major and minor
7608 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7609 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7610}
7611
7612TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7613 addConfigurationProperty("touch.deviceType", "touchScreen");
7614 prepareDisplay(DISPLAY_ORIENTATION_0);
7615
7616 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7617 /*fuzz*/ 0, /*resolution*/ 10);
7618 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7619 /*fuzz*/ 0, /*resolution*/ 11);
7620
7621 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7622
7623 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7624
7625 // Touch major and minor
7626 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7627 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7628 // Tool major and minor
7629 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7630 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7631}
7632
Michael Wrightd02c5b62014-02-10 15:10:22 -08007633TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007634 addConfigurationProperty("touch.deviceType", "touchScreen");
7635 prepareDisplay(DISPLAY_ORIENTATION_0);
7636 prepareAxes(POSITION | ID);
7637 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007638 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007639
arthurhungdcef2dc2020-08-11 14:47:50 +08007640 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007641
7642 NotifyMotionArgs motionArgs;
7643
7644 // Two fingers down at once.
7645 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7646 processPosition(mapper, x1, y1);
7647 processId(mapper, 1);
7648 processMTSync(mapper);
7649 processPosition(mapper, x2, y2);
7650 processId(mapper, 2);
7651 processMTSync(mapper);
7652 processSync(mapper);
7653
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7655 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7656 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7660 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7661
7662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007663 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7665 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7666 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7667 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7670 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7672 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7673
7674 // Move.
7675 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7676 processPosition(mapper, x1, y1);
7677 processId(mapper, 1);
7678 processMTSync(mapper);
7679 processPosition(mapper, x2, y2);
7680 processId(mapper, 2);
7681 processMTSync(mapper);
7682 processSync(mapper);
7683
7684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7685 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7686 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7687 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7689 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7692 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7694 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7695
7696 // First finger up.
7697 x2 += 15; y2 -= 20;
7698 processPosition(mapper, x2, y2);
7699 processId(mapper, 2);
7700 processMTSync(mapper);
7701 processSync(mapper);
7702
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007704 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007705 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7706 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7707 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7708 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7711 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7713 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7714
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7717 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7718 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7721 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7722
7723 // Move.
7724 x2 += 20; y2 -= 25;
7725 processPosition(mapper, x2, y2);
7726 processId(mapper, 2);
7727 processMTSync(mapper);
7728 processSync(mapper);
7729
7730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7732 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7733 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7736 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7737
7738 // New finger down.
7739 int32_t x3 = 700, y3 = 300;
7740 processPosition(mapper, x2, y2);
7741 processId(mapper, 2);
7742 processMTSync(mapper);
7743 processPosition(mapper, x3, y3);
7744 processId(mapper, 3);
7745 processMTSync(mapper);
7746 processSync(mapper);
7747
7748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007749 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007750 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7751 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7753 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7756 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7758 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7759
7760 // Second finger up.
7761 x3 += 30; y3 -= 20;
7762 processPosition(mapper, x3, y3);
7763 processId(mapper, 3);
7764 processMTSync(mapper);
7765 processSync(mapper);
7766
7767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007768 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007769 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7770 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7772 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7775 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7777 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7778
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7781 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7782 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7785 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7786
7787 // Last finger up.
7788 processMTSync(mapper);
7789 processSync(mapper);
7790
7791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7792 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7793 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7794 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7797 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7798
7799 // Should not have sent any more keys or motions.
7800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7802}
7803
7804TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007805 addConfigurationProperty("touch.deviceType", "touchScreen");
7806 prepareDisplay(DISPLAY_ORIENTATION_0);
7807 prepareAxes(POSITION | ID | SLOT);
7808 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007809 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810
arthurhungdcef2dc2020-08-11 14:47:50 +08007811 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007812
7813 NotifyMotionArgs motionArgs;
7814
7815 // Two fingers down at once.
7816 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7817 processPosition(mapper, x1, y1);
7818 processId(mapper, 1);
7819 processSlot(mapper, 1);
7820 processPosition(mapper, x2, y2);
7821 processId(mapper, 2);
7822 processSync(mapper);
7823
7824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7825 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7826 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7827 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7830 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7831
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007833 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007834 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7835 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7836 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7837 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7840 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7842 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7843
7844 // Move.
7845 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7846 processSlot(mapper, 0);
7847 processPosition(mapper, x1, y1);
7848 processSlot(mapper, 1);
7849 processPosition(mapper, x2, y2);
7850 processSync(mapper);
7851
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7854 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7855 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7857 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7860 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7862 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7863
7864 // First finger up.
7865 x2 += 15; y2 -= 20;
7866 processSlot(mapper, 0);
7867 processId(mapper, -1);
7868 processSlot(mapper, 1);
7869 processPosition(mapper, x2, y2);
7870 processSync(mapper);
7871
7872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007873 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007874 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7875 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7876 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7877 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7880 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7882 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7883
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7886 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7887 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7890 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7891
7892 // Move.
7893 x2 += 20; y2 -= 25;
7894 processPosition(mapper, x2, y2);
7895 processSync(mapper);
7896
7897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7899 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7900 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7903 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7904
7905 // New finger down.
7906 int32_t x3 = 700, y3 = 300;
7907 processPosition(mapper, x2, y2);
7908 processSlot(mapper, 0);
7909 processId(mapper, 3);
7910 processPosition(mapper, x3, y3);
7911 processSync(mapper);
7912
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007914 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7916 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7918 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7921 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7923 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7924
7925 // Second finger up.
7926 x3 += 30; y3 -= 20;
7927 processSlot(mapper, 1);
7928 processId(mapper, -1);
7929 processSlot(mapper, 0);
7930 processPosition(mapper, x3, y3);
7931 processSync(mapper);
7932
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007934 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7936 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7938 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7939 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7941 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7943 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7944
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7947 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7948 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7949 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7951 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7952
7953 // Last finger up.
7954 processId(mapper, -1);
7955 processSync(mapper);
7956
7957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7958 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7959 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7960 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7963 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7964
7965 // Should not have sent any more keys or motions.
7966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7968}
7969
7970TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971 addConfigurationProperty("touch.deviceType", "touchScreen");
7972 prepareDisplay(DISPLAY_ORIENTATION_0);
7973 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007974 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975
7976 // These calculations are based on the input device calibration documentation.
7977 int32_t rawX = 100;
7978 int32_t rawY = 200;
7979 int32_t rawTouchMajor = 7;
7980 int32_t rawTouchMinor = 6;
7981 int32_t rawToolMajor = 9;
7982 int32_t rawToolMinor = 8;
7983 int32_t rawPressure = 11;
7984 int32_t rawDistance = 0;
7985 int32_t rawOrientation = 3;
7986 int32_t id = 5;
7987
7988 float x = toDisplayX(rawX);
7989 float y = toDisplayY(rawY);
7990 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7991 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7992 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7993 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7994 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7995 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7996 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7997 float distance = float(rawDistance);
7998
7999 processPosition(mapper, rawX, rawY);
8000 processTouchMajor(mapper, rawTouchMajor);
8001 processTouchMinor(mapper, rawTouchMinor);
8002 processToolMajor(mapper, rawToolMajor);
8003 processToolMinor(mapper, rawToolMinor);
8004 processPressure(mapper, rawPressure);
8005 processOrientation(mapper, rawOrientation);
8006 processDistance(mapper, rawDistance);
8007 processId(mapper, id);
8008 processMTSync(mapper);
8009 processSync(mapper);
8010
8011 NotifyMotionArgs args;
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8013 ASSERT_EQ(0, args.pointerProperties[0].id);
8014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8015 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8016 orientation, distance));
8017}
8018
8019TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008020 addConfigurationProperty("touch.deviceType", "touchScreen");
8021 prepareDisplay(DISPLAY_ORIENTATION_0);
8022 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8023 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025
8026 // These calculations are based on the input device calibration documentation.
8027 int32_t rawX = 100;
8028 int32_t rawY = 200;
8029 int32_t rawTouchMajor = 140;
8030 int32_t rawTouchMinor = 120;
8031 int32_t rawToolMajor = 180;
8032 int32_t rawToolMinor = 160;
8033
8034 float x = toDisplayX(rawX);
8035 float y = toDisplayY(rawY);
8036 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8037 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8038 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8039 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8040 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8041
8042 processPosition(mapper, rawX, rawY);
8043 processTouchMajor(mapper, rawTouchMajor);
8044 processTouchMinor(mapper, rawTouchMinor);
8045 processToolMajor(mapper, rawToolMajor);
8046 processToolMinor(mapper, rawToolMinor);
8047 processMTSync(mapper);
8048 processSync(mapper);
8049
8050 NotifyMotionArgs args;
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8053 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8054}
8055
8056TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 addConfigurationProperty("touch.deviceType", "touchScreen");
8058 prepareDisplay(DISPLAY_ORIENTATION_0);
8059 prepareAxes(POSITION | TOUCH | TOOL);
8060 addConfigurationProperty("touch.size.calibration", "diameter");
8061 addConfigurationProperty("touch.size.scale", "10");
8062 addConfigurationProperty("touch.size.bias", "160");
8063 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008064 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065
8066 // These calculations are based on the input device calibration documentation.
8067 // Note: We only provide a single common touch/tool value because the device is assumed
8068 // not to emit separate values for each pointer (isSummed = 1).
8069 int32_t rawX = 100;
8070 int32_t rawY = 200;
8071 int32_t rawX2 = 150;
8072 int32_t rawY2 = 250;
8073 int32_t rawTouchMajor = 5;
8074 int32_t rawToolMajor = 8;
8075
8076 float x = toDisplayX(rawX);
8077 float y = toDisplayY(rawY);
8078 float x2 = toDisplayX(rawX2);
8079 float y2 = toDisplayY(rawY2);
8080 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8081 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8082 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8083
8084 processPosition(mapper, rawX, rawY);
8085 processTouchMajor(mapper, rawTouchMajor);
8086 processToolMajor(mapper, rawToolMajor);
8087 processMTSync(mapper);
8088 processPosition(mapper, rawX2, rawY2);
8089 processTouchMajor(mapper, rawTouchMajor);
8090 processToolMajor(mapper, rawToolMajor);
8091 processMTSync(mapper);
8092 processSync(mapper);
8093
8094 NotifyMotionArgs args;
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8097
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008099 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_EQ(size_t(2), args.pointerCount);
8101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8102 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8104 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8105}
8106
8107TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108 addConfigurationProperty("touch.deviceType", "touchScreen");
8109 prepareDisplay(DISPLAY_ORIENTATION_0);
8110 prepareAxes(POSITION | TOUCH | TOOL);
8111 addConfigurationProperty("touch.size.calibration", "area");
8112 addConfigurationProperty("touch.size.scale", "43");
8113 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008114 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115
8116 // These calculations are based on the input device calibration documentation.
8117 int32_t rawX = 100;
8118 int32_t rawY = 200;
8119 int32_t rawTouchMajor = 5;
8120 int32_t rawToolMajor = 8;
8121
8122 float x = toDisplayX(rawX);
8123 float y = toDisplayY(rawY);
8124 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8125 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8126 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8127
8128 processPosition(mapper, rawX, rawY);
8129 processTouchMajor(mapper, rawTouchMajor);
8130 processToolMajor(mapper, rawToolMajor);
8131 processMTSync(mapper);
8132 processSync(mapper);
8133
8134 NotifyMotionArgs args;
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8137 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8138}
8139
8140TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 addConfigurationProperty("touch.deviceType", "touchScreen");
8142 prepareDisplay(DISPLAY_ORIENTATION_0);
8143 prepareAxes(POSITION | PRESSURE);
8144 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8145 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008146 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147
Michael Wrightaa449c92017-12-13 21:21:43 +00008148 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008149 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008150 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8151 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8152 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8153
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154 // These calculations are based on the input device calibration documentation.
8155 int32_t rawX = 100;
8156 int32_t rawY = 200;
8157 int32_t rawPressure = 60;
8158
8159 float x = toDisplayX(rawX);
8160 float y = toDisplayY(rawY);
8161 float pressure = float(rawPressure) * 0.01f;
8162
8163 processPosition(mapper, rawX, rawY);
8164 processPressure(mapper, rawPressure);
8165 processMTSync(mapper);
8166 processSync(mapper);
8167
8168 NotifyMotionArgs args;
8169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8171 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8172}
8173
8174TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 addConfigurationProperty("touch.deviceType", "touchScreen");
8176 prepareDisplay(DISPLAY_ORIENTATION_0);
8177 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008178 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179
8180 NotifyMotionArgs motionArgs;
8181 NotifyKeyArgs keyArgs;
8182
8183 processId(mapper, 1);
8184 processPosition(mapper, 100, 200);
8185 processSync(mapper);
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8188 ASSERT_EQ(0, motionArgs.buttonState);
8189
8190 // press BTN_LEFT, release BTN_LEFT
8191 processKey(mapper, BTN_LEFT, 1);
8192 processSync(mapper);
8193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8195 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8196
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8199 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8200
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 processKey(mapper, BTN_LEFT, 0);
8202 processSync(mapper);
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008206
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210
8211 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8212 processKey(mapper, BTN_RIGHT, 1);
8213 processKey(mapper, BTN_MIDDLE, 1);
8214 processSync(mapper);
8215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8217 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8218 motionArgs.buttonState);
8219
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8222 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8223
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8226 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8227 motionArgs.buttonState);
8228
Michael Wrightd02c5b62014-02-10 15:10:22 -08008229 processKey(mapper, BTN_RIGHT, 0);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008234
8235 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_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238
8239 processKey(mapper, BTN_MIDDLE, 0);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008244
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008248
8249 // press BTN_BACK, release BTN_BACK
8250 processKey(mapper, BTN_BACK, 1);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8253 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8254 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008255
Michael Wrightd02c5b62014-02-10 15:10:22 -08008256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008258 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8259
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8262 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263
8264 processKey(mapper, BTN_BACK, 0);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008269
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008272 ASSERT_EQ(0, motionArgs.buttonState);
8273
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8275 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8276 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8277
8278 // press BTN_SIDE, release BTN_SIDE
8279 processKey(mapper, BTN_SIDE, 1);
8280 processSync(mapper);
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8282 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8283 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008284
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008287 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8288
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8290 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8291 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292
8293 processKey(mapper, BTN_SIDE, 0);
8294 processSync(mapper);
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008298
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008301 ASSERT_EQ(0, motionArgs.buttonState);
8302
Michael Wrightd02c5b62014-02-10 15:10:22 -08008303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8304 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8305 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8306
8307 // press BTN_FORWARD, release BTN_FORWARD
8308 processKey(mapper, BTN_FORWARD, 1);
8309 processSync(mapper);
8310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8311 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8312 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008313
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008316 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8317
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8320 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321
8322 processKey(mapper, BTN_FORWARD, 0);
8323 processSync(mapper);
8324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008327
8328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008330 ASSERT_EQ(0, motionArgs.buttonState);
8331
Michael Wrightd02c5b62014-02-10 15:10:22 -08008332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8333 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8334 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8335
8336 // press BTN_EXTRA, release BTN_EXTRA
8337 processKey(mapper, BTN_EXTRA, 1);
8338 processSync(mapper);
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8340 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8341 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008342
Michael Wrightd02c5b62014-02-10 15:10:22 -08008343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008345 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8346
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8349 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008350
8351 processKey(mapper, BTN_EXTRA, 0);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008356
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008359 ASSERT_EQ(0, motionArgs.buttonState);
8360
Michael Wrightd02c5b62014-02-10 15:10:22 -08008361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8362 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8363 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8364
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8366
Michael Wrightd02c5b62014-02-10 15:10:22 -08008367 // press BTN_STYLUS, release BTN_STYLUS
8368 processKey(mapper, BTN_STYLUS, 1);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008372 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8373
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8375 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8376 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008377
8378 processKey(mapper, BTN_STYLUS, 0);
8379 processSync(mapper);
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008381 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008382 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008383
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008386 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387
8388 // press BTN_STYLUS2, release BTN_STYLUS2
8389 processKey(mapper, BTN_STYLUS2, 1);
8390 processSync(mapper);
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008393 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8394
8395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8397 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008398
8399 processKey(mapper, BTN_STYLUS2, 0);
8400 processSync(mapper);
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008403 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008404
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008407 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408
8409 // release touch
8410 processId(mapper, -1);
8411 processSync(mapper);
8412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8413 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8414 ASSERT_EQ(0, motionArgs.buttonState);
8415}
8416
8417TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008418 addConfigurationProperty("touch.deviceType", "touchScreen");
8419 prepareDisplay(DISPLAY_ORIENTATION_0);
8420 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008421 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422
8423 NotifyMotionArgs motionArgs;
8424
8425 // default tool type is finger
8426 processId(mapper, 1);
8427 processPosition(mapper, 100, 200);
8428 processSync(mapper);
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8432
8433 // eraser
8434 processKey(mapper, BTN_TOOL_RUBBER, 1);
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_ERASER, motionArgs.pointerProperties[0].toolType);
8439
8440 // stylus
8441 processKey(mapper, BTN_TOOL_RUBBER, 0);
8442 processKey(mapper, BTN_TOOL_PEN, 1);
8443 processSync(mapper);
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8447
8448 // brush
8449 processKey(mapper, BTN_TOOL_PEN, 0);
8450 processKey(mapper, BTN_TOOL_BRUSH, 1);
8451 processSync(mapper);
8452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8455
8456 // pencil
8457 processKey(mapper, BTN_TOOL_BRUSH, 0);
8458 processKey(mapper, BTN_TOOL_PENCIL, 1);
8459 processSync(mapper);
8460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8463
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008464 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465 processKey(mapper, BTN_TOOL_PENCIL, 0);
8466 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8467 processSync(mapper);
8468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8471
8472 // mouse
8473 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8474 processKey(mapper, BTN_TOOL_MOUSE, 1);
8475 processSync(mapper);
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8479
8480 // lens
8481 processKey(mapper, BTN_TOOL_MOUSE, 0);
8482 processKey(mapper, BTN_TOOL_LENS, 1);
8483 processSync(mapper);
8484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8487
8488 // double-tap
8489 processKey(mapper, BTN_TOOL_LENS, 0);
8490 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8491 processSync(mapper);
8492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8495
8496 // triple-tap
8497 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8498 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8499 processSync(mapper);
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8503
8504 // quad-tap
8505 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8506 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8511
8512 // finger
8513 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8514 processKey(mapper, BTN_TOOL_FINGER, 1);
8515 processSync(mapper);
8516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8519
8520 // stylus trumps finger
8521 processKey(mapper, BTN_TOOL_PEN, 1);
8522 processSync(mapper);
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8526
8527 // eraser trumps stylus
8528 processKey(mapper, BTN_TOOL_RUBBER, 1);
8529 processSync(mapper);
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8533
8534 // mouse trumps eraser
8535 processKey(mapper, BTN_TOOL_MOUSE, 1);
8536 processSync(mapper);
8537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8540
8541 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8542 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8543 processSync(mapper);
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8547
8548 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8549 processToolType(mapper, MT_TOOL_PEN);
8550 processSync(mapper);
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8554
8555 // back to default tool type
8556 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8557 processKey(mapper, BTN_TOOL_MOUSE, 0);
8558 processKey(mapper, BTN_TOOL_RUBBER, 0);
8559 processKey(mapper, BTN_TOOL_PEN, 0);
8560 processKey(mapper, BTN_TOOL_FINGER, 0);
8561 processSync(mapper);
8562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8565}
8566
8567TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568 addConfigurationProperty("touch.deviceType", "touchScreen");
8569 prepareDisplay(DISPLAY_ORIENTATION_0);
8570 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008571 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008572 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008573
8574 NotifyMotionArgs motionArgs;
8575
8576 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8577 processId(mapper, 1);
8578 processPosition(mapper, 100, 200);
8579 processSync(mapper);
8580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8583 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8584
8585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8588 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8589
8590 // move a little
8591 processPosition(mapper, 150, 250);
8592 processSync(mapper);
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8594 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8596 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8597
8598 // down when BTN_TOUCH is pressed, pressure defaults to 1
8599 processKey(mapper, BTN_TOUCH, 1);
8600 processSync(mapper);
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8604 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8605
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8607 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8609 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8610
8611 // up when BTN_TOUCH is released, hover restored
8612 processKey(mapper, BTN_TOUCH, 0);
8613 processSync(mapper);
8614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8615 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8617 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8618
8619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8620 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8622 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8623
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8625 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8627 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8628
8629 // exit hover when pointer goes away
8630 processId(mapper, -1);
8631 processSync(mapper);
8632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8635 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8636}
8637
8638TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008639 addConfigurationProperty("touch.deviceType", "touchScreen");
8640 prepareDisplay(DISPLAY_ORIENTATION_0);
8641 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008642 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008643
8644 NotifyMotionArgs motionArgs;
8645
8646 // initially hovering because pressure is 0
8647 processId(mapper, 1);
8648 processPosition(mapper, 100, 200);
8649 processPressure(mapper, 0);
8650 processSync(mapper);
8651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8654 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8655
8656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8657 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8659 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8660
8661 // move a little
8662 processPosition(mapper, 150, 250);
8663 processSync(mapper);
8664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8665 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8667 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8668
8669 // down when pressure becomes non-zero
8670 processPressure(mapper, RAW_PRESSURE_MAX);
8671 processSync(mapper);
8672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8675 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8676
8677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8678 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8680 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8681
8682 // up when pressure becomes 0, hover restored
8683 processPressure(mapper, 0);
8684 processSync(mapper);
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8686 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8688 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8689
8690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8691 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8693 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8694
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8696 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8698 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8699
8700 // exit hover when pointer goes away
8701 processId(mapper, -1);
8702 processSync(mapper);
8703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8704 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8706 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8707}
8708
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008709/**
8710 * Set the input device port <--> display port associations, and check that the
8711 * events are routed to the display that matches the display port.
8712 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8713 */
8714TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008715 const std::string usb2 = "USB2";
8716 const uint8_t hdmi1 = 0;
8717 const uint8_t hdmi2 = 1;
8718 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008719 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008720
8721 addConfigurationProperty("touch.deviceType", "touchScreen");
8722 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008723 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008724
8725 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8726 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8727
8728 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8729 // for this input device is specified, and the matching viewport is not present,
8730 // the input device should be disabled (at the mapper level).
8731
8732 // Add viewport for display 2 on hdmi2
8733 prepareSecondaryDisplay(type, hdmi2);
8734 // Send a touch event
8735 processPosition(mapper, 100, 100);
8736 processSync(mapper);
8737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8738
8739 // Add viewport for display 1 on hdmi1
8740 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8741 // Send a touch event again
8742 processPosition(mapper, 100, 100);
8743 processSync(mapper);
8744
8745 NotifyMotionArgs args;
8746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8747 ASSERT_EQ(DISPLAY_ID, args.displayId);
8748}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008749
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008750TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8751 addConfigurationProperty("touch.deviceType", "touchScreen");
8752 prepareAxes(POSITION);
8753 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8754
8755 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8756
8757 prepareDisplay(DISPLAY_ORIENTATION_0);
8758 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8759
8760 // Send a touch event
8761 processPosition(mapper, 100, 100);
8762 processSync(mapper);
8763
8764 NotifyMotionArgs args;
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8766 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8767}
8768
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008769TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008770 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008771 std::shared_ptr<FakePointerController> fakePointerController =
8772 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008773 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008774 fakePointerController->setPosition(100, 200);
8775 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008776 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008777
Garfield Tan888a6a42020-01-09 11:39:16 -08008778 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008779 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008780
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008781 prepareDisplay(DISPLAY_ORIENTATION_0);
8782 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008783 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008784
8785 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008786 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008787
8788 NotifyMotionArgs motionArgs;
8789 processPosition(mapper, 100, 100);
8790 processSync(mapper);
8791
8792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8793 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8794 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8795}
8796
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008797/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008798 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8799 */
8800TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8801 addConfigurationProperty("touch.deviceType", "touchScreen");
8802 prepareAxes(POSITION);
8803 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8804
8805 prepareDisplay(DISPLAY_ORIENTATION_0);
8806 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8807 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8808 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8809 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8810
8811 NotifyMotionArgs args;
8812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8813 ASSERT_EQ(26, args.readTime);
8814
8815 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8816 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8817 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8818
8819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8820 ASSERT_EQ(33, args.readTime);
8821}
8822
8823/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008824 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8825 * events should not be delivered to the listener.
8826 */
8827TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8828 addConfigurationProperty("touch.deviceType", "touchScreen");
8829 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8830 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8831 ViewportType::INTERNAL);
8832 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8833 prepareAxes(POSITION);
8834 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8835
8836 NotifyMotionArgs motionArgs;
8837 processPosition(mapper, 100, 100);
8838 processSync(mapper);
8839
8840 mFakeListener->assertNotifyMotionWasNotCalled();
8841}
8842
Garfield Tanc734e4f2021-01-15 20:01:39 -08008843TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8844 addConfigurationProperty("touch.deviceType", "touchScreen");
8845 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8846 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8847 ViewportType::INTERNAL);
8848 std::optional<DisplayViewport> optionalDisplayViewport =
8849 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8850 ASSERT_TRUE(optionalDisplayViewport.has_value());
8851 DisplayViewport displayViewport = *optionalDisplayViewport;
8852
8853 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8854 prepareAxes(POSITION);
8855 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8856
8857 // Finger down
8858 int32_t x = 100, y = 100;
8859 processPosition(mapper, x, y);
8860 processSync(mapper);
8861
8862 NotifyMotionArgs motionArgs;
8863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8864 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8865
8866 // Deactivate display viewport
8867 displayViewport.isActive = false;
8868 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8869 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8870
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008871 // The ongoing touch should be canceled immediately
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8873 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8874
8875 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008876 x += 10, y += 10;
8877 processPosition(mapper, x, y);
8878 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008880
8881 // Reactivate display viewport
8882 displayViewport.isActive = true;
8883 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8884 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8885
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008886 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008887 x += 10, y += 10;
8888 processPosition(mapper, x, y);
8889 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8891 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008892}
8893
Arthur Hung7c645402019-01-25 17:45:42 +08008894TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8895 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008896 prepareAxes(POSITION | ID | SLOT);
8897 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008898 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008899
8900 // Create the second touch screen device, and enable multi fingers.
8901 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008902 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008903 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008904 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008905 std::shared_ptr<InputDevice> device2 =
8906 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008907 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008908
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008909 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8910 0 /*flat*/, 0 /*fuzz*/);
8911 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8912 0 /*flat*/, 0 /*fuzz*/);
8913 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8914 0 /*flat*/, 0 /*fuzz*/);
8915 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8916 0 /*flat*/, 0 /*fuzz*/);
8917 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8918 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8919 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008920
8921 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008922 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008923 std::list<NotifyArgs> unused =
8924 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8925 0 /*changes*/);
8926 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008927
8928 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008929 std::shared_ptr<FakePointerController> fakePointerController =
8930 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008931 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008932
8933 // Setup policy for associated displays and show touches.
8934 const uint8_t hdmi1 = 0;
8935 const uint8_t hdmi2 = 1;
8936 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8937 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8938 mFakePolicy->setShowTouches(true);
8939
8940 // Create displays.
8941 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008942 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008943
8944 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008945 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8946 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8947 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008948
8949 // Two fingers down at default display.
8950 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8951 processPosition(mapper, x1, y1);
8952 processId(mapper, 1);
8953 processSlot(mapper, 1);
8954 processPosition(mapper, x2, y2);
8955 processId(mapper, 2);
8956 processSync(mapper);
8957
8958 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8959 fakePointerController->getSpots().find(DISPLAY_ID);
8960 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8961 ASSERT_EQ(size_t(2), iter->second.size());
8962
8963 // Two fingers down at second display.
8964 processPosition(mapper2, x1, y1);
8965 processId(mapper2, 1);
8966 processSlot(mapper2, 1);
8967 processPosition(mapper2, x2, y2);
8968 processId(mapper2, 2);
8969 processSync(mapper2);
8970
8971 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8972 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8973 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008974
8975 // Disable the show touches configuration and ensure the spots are cleared.
8976 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008977 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8978 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008979
8980 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008981}
8982
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008983TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008984 prepareAxes(POSITION);
8985 addConfigurationProperty("touch.deviceType", "touchScreen");
8986 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008987 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008988
8989 NotifyMotionArgs motionArgs;
8990 // Unrotated video frame
8991 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8992 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008993 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008994 processPosition(mapper, 100, 200);
8995 processSync(mapper);
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8997 ASSERT_EQ(frames, motionArgs.videoFrames);
8998
8999 // Subsequent touch events should not have any videoframes
9000 // This is implemented separately in FakeEventHub,
9001 // but that should match the behaviour of TouchVideoDevice.
9002 processPosition(mapper, 200, 200);
9003 processSync(mapper);
9004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9005 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9006}
9007
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009008TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009009 prepareAxes(POSITION);
9010 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009011 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009012 // Unrotated video frame
9013 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9014 NotifyMotionArgs motionArgs;
9015
9016 // Test all 4 orientations
9017 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009018 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9019 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9020 clearViewports();
9021 prepareDisplay(orientation);
9022 std::vector<TouchVideoFrame> frames{frame};
9023 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9024 processPosition(mapper, 100, 200);
9025 processSync(mapper);
9026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9027 ASSERT_EQ(frames, motionArgs.videoFrames);
9028 }
9029}
9030
9031TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9032 prepareAxes(POSITION);
9033 addConfigurationProperty("touch.deviceType", "touchScreen");
9034 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9035 // orientation-aware are affected by display rotation.
9036 addConfigurationProperty("touch.orientationAware", "0");
9037 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9038 // Unrotated video frame
9039 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9040 NotifyMotionArgs motionArgs;
9041
9042 // Test all 4 orientations
9043 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009044 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9045 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9046 clearViewports();
9047 prepareDisplay(orientation);
9048 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009049 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009050 processPosition(mapper, 100, 200);
9051 processSync(mapper);
9052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009053 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9054 // compared to the display. This is so that when the window transform (which contains the
9055 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9056 // window's coordinate space.
9057 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009058 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009059
9060 // Release finger.
9061 processSync(mapper);
9062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009063 }
9064}
9065
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009066TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009067 prepareAxes(POSITION);
9068 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009069 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009070 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9071 // so mix these.
9072 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9073 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9074 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9075 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9076 NotifyMotionArgs motionArgs;
9077
9078 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009079 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009080 processPosition(mapper, 100, 200);
9081 processSync(mapper);
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009083 ASSERT_EQ(frames, motionArgs.videoFrames);
9084}
9085
9086TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9087 prepareAxes(POSITION);
9088 addConfigurationProperty("touch.deviceType", "touchScreen");
9089 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9090 // orientation-aware are affected by display rotation.
9091 addConfigurationProperty("touch.orientationAware", "0");
9092 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9093 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9094 // so mix these.
9095 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9096 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9097 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9098 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9099 NotifyMotionArgs motionArgs;
9100
9101 prepareDisplay(DISPLAY_ORIENTATION_90);
9102 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9103 processPosition(mapper, 100, 200);
9104 processSync(mapper);
9105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9106 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9107 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9108 // compared to the display. This is so that when the window transform (which contains the
9109 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9110 // window's coordinate space.
9111 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9112 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009113 ASSERT_EQ(frames, motionArgs.videoFrames);
9114}
9115
Arthur Hung9da14732019-09-02 16:16:58 +08009116/**
9117 * If we had defined port associations, but the viewport is not ready, the touch device would be
9118 * expected to be disabled, and it should be enabled after the viewport has found.
9119 */
9120TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009121 constexpr uint8_t hdmi2 = 1;
9122 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009123 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009124
9125 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9126
9127 addConfigurationProperty("touch.deviceType", "touchScreen");
9128 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009129 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009130
9131 ASSERT_EQ(mDevice->isEnabled(), false);
9132
9133 // Add display on hdmi2, the device should be enabled and can receive touch event.
9134 prepareSecondaryDisplay(type, hdmi2);
9135 ASSERT_EQ(mDevice->isEnabled(), true);
9136
9137 // Send a touch event.
9138 processPosition(mapper, 100, 100);
9139 processSync(mapper);
9140
9141 NotifyMotionArgs args;
9142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9143 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9144}
9145
Arthur Hung421eb1c2020-01-16 00:09:42 +08009146TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009147 addConfigurationProperty("touch.deviceType", "touchScreen");
9148 prepareDisplay(DISPLAY_ORIENTATION_0);
9149 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009150 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009151
9152 NotifyMotionArgs motionArgs;
9153
9154 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9155 // finger down
9156 processId(mapper, 1);
9157 processPosition(mapper, x1, y1);
9158 processSync(mapper);
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9160 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9162
9163 // finger move
9164 processId(mapper, 1);
9165 processPosition(mapper, x2, y2);
9166 processSync(mapper);
9167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9170
9171 // finger up.
9172 processId(mapper, -1);
9173 processSync(mapper);
9174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9175 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9177
9178 // new finger down
9179 processId(mapper, 1);
9180 processPosition(mapper, x3, y3);
9181 processSync(mapper);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9183 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9185}
9186
9187/**
arthurhungcc7f9802020-04-30 17:55:40 +08009188 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9189 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009190 */
arthurhungcc7f9802020-04-30 17:55:40 +08009191TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009192 addConfigurationProperty("touch.deviceType", "touchScreen");
9193 prepareDisplay(DISPLAY_ORIENTATION_0);
9194 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009195 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009196
9197 NotifyMotionArgs motionArgs;
9198
9199 // default tool type is finger
9200 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009201 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009202 processPosition(mapper, x1, y1);
9203 processSync(mapper);
9204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9205 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9207
9208 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9209 processToolType(mapper, MT_TOOL_PALM);
9210 processSync(mapper);
9211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9212 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9213
9214 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009215 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009216 processPosition(mapper, x2, y2);
9217 processSync(mapper);
9218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9219
9220 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009221 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009222 processSync(mapper);
9223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9224
9225 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009226 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009227 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009228 processPosition(mapper, x3, y3);
9229 processSync(mapper);
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9231 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9233}
9234
arthurhungbf89a482020-04-17 17:37:55 +08009235/**
arthurhungcc7f9802020-04-30 17:55:40 +08009236 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9237 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009238 */
arthurhungcc7f9802020-04-30 17:55:40 +08009239TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009240 addConfigurationProperty("touch.deviceType", "touchScreen");
9241 prepareDisplay(DISPLAY_ORIENTATION_0);
9242 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9243 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9244
9245 NotifyMotionArgs motionArgs;
9246
9247 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009248 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9249 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009250 processPosition(mapper, x1, y1);
9251 processSync(mapper);
9252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9253 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9255
9256 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009257 processSlot(mapper, SECOND_SLOT);
9258 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009259 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009262 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9264
9265 // If the tool type of the first finger changes to MT_TOOL_PALM,
9266 // we expect to receive ACTION_POINTER_UP with cancel flag.
9267 processSlot(mapper, FIRST_SLOT);
9268 processId(mapper, FIRST_TRACKING_ID);
9269 processToolType(mapper, MT_TOOL_PALM);
9270 processSync(mapper);
9271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009272 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009273 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9274
9275 // The following MOVE events of second finger should be processed.
9276 processSlot(mapper, SECOND_SLOT);
9277 processId(mapper, SECOND_TRACKING_ID);
9278 processPosition(mapper, x2 + 1, y2 + 1);
9279 processSync(mapper);
9280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9282 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9283
9284 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9285 // it. Second finger receive move.
9286 processSlot(mapper, FIRST_SLOT);
9287 processId(mapper, INVALID_TRACKING_ID);
9288 processSync(mapper);
9289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9291 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9292
9293 // Second finger keeps moving.
9294 processSlot(mapper, SECOND_SLOT);
9295 processId(mapper, SECOND_TRACKING_ID);
9296 processPosition(mapper, x2 + 2, y2 + 2);
9297 processSync(mapper);
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9300 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9301
9302 // Second finger up.
9303 processId(mapper, INVALID_TRACKING_ID);
9304 processSync(mapper);
9305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9306 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9307 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9308}
9309
9310/**
9311 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9312 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9313 */
9314TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9315 addConfigurationProperty("touch.deviceType", "touchScreen");
9316 prepareDisplay(DISPLAY_ORIENTATION_0);
9317 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9318 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9319
9320 NotifyMotionArgs motionArgs;
9321
9322 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9323 // First finger down.
9324 processId(mapper, FIRST_TRACKING_ID);
9325 processPosition(mapper, x1, y1);
9326 processSync(mapper);
9327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9328 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9330
9331 // Second finger down.
9332 processSlot(mapper, SECOND_SLOT);
9333 processId(mapper, SECOND_TRACKING_ID);
9334 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009335 processSync(mapper);
9336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009337 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9339
arthurhungcc7f9802020-04-30 17:55:40 +08009340 // If the tool type of the first finger changes to MT_TOOL_PALM,
9341 // we expect to receive ACTION_POINTER_UP with cancel flag.
9342 processSlot(mapper, FIRST_SLOT);
9343 processId(mapper, FIRST_TRACKING_ID);
9344 processToolType(mapper, MT_TOOL_PALM);
9345 processSync(mapper);
9346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009347 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009348 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9349
9350 // Second finger keeps moving.
9351 processSlot(mapper, SECOND_SLOT);
9352 processId(mapper, SECOND_TRACKING_ID);
9353 processPosition(mapper, x2 + 1, y2 + 1);
9354 processSync(mapper);
9355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9357
9358 // second finger becomes palm, receive cancel due to only 1 finger is active.
9359 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009360 processToolType(mapper, MT_TOOL_PALM);
9361 processSync(mapper);
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9363 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9364
arthurhungcc7f9802020-04-30 17:55:40 +08009365 // third finger down.
9366 processSlot(mapper, THIRD_SLOT);
9367 processId(mapper, THIRD_TRACKING_ID);
9368 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009369 processPosition(mapper, x3, y3);
9370 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9372 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009374 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9375
9376 // third finger move
9377 processId(mapper, THIRD_TRACKING_ID);
9378 processPosition(mapper, x3 + 1, y3 + 1);
9379 processSync(mapper);
9380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9382
9383 // first finger up, third finger receive move.
9384 processSlot(mapper, FIRST_SLOT);
9385 processId(mapper, INVALID_TRACKING_ID);
9386 processSync(mapper);
9387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9389 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9390
9391 // second finger up, third finger receive move.
9392 processSlot(mapper, SECOND_SLOT);
9393 processId(mapper, INVALID_TRACKING_ID);
9394 processSync(mapper);
9395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9397 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9398
9399 // third finger up.
9400 processSlot(mapper, THIRD_SLOT);
9401 processId(mapper, INVALID_TRACKING_ID);
9402 processSync(mapper);
9403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9404 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9405 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9406}
9407
9408/**
9409 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9410 * and the active finger could still be allowed to receive the events
9411 */
9412TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9413 addConfigurationProperty("touch.deviceType", "touchScreen");
9414 prepareDisplay(DISPLAY_ORIENTATION_0);
9415 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9416 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9417
9418 NotifyMotionArgs motionArgs;
9419
9420 // default tool type is finger
9421 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9422 processId(mapper, FIRST_TRACKING_ID);
9423 processPosition(mapper, x1, y1);
9424 processSync(mapper);
9425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9426 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9428
9429 // Second finger down.
9430 processSlot(mapper, SECOND_SLOT);
9431 processId(mapper, SECOND_TRACKING_ID);
9432 processPosition(mapper, x2, y2);
9433 processSync(mapper);
9434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009435 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9437
9438 // If the tool type of the second finger changes to MT_TOOL_PALM,
9439 // we expect to receive ACTION_POINTER_UP with cancel flag.
9440 processId(mapper, SECOND_TRACKING_ID);
9441 processToolType(mapper, MT_TOOL_PALM);
9442 processSync(mapper);
9443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009444 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009445 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9446
9447 // The following MOVE event should be processed.
9448 processSlot(mapper, FIRST_SLOT);
9449 processId(mapper, FIRST_TRACKING_ID);
9450 processPosition(mapper, x1 + 1, y1 + 1);
9451 processSync(mapper);
9452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9454 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9455
9456 // second finger up.
9457 processSlot(mapper, SECOND_SLOT);
9458 processId(mapper, INVALID_TRACKING_ID);
9459 processSync(mapper);
9460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9462
9463 // first finger keep moving
9464 processSlot(mapper, FIRST_SLOT);
9465 processId(mapper, FIRST_TRACKING_ID);
9466 processPosition(mapper, x1 + 2, y1 + 2);
9467 processSync(mapper);
9468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9470
9471 // first finger up.
9472 processId(mapper, INVALID_TRACKING_ID);
9473 processSync(mapper);
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9475 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9476 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009477}
9478
Arthur Hung9ad18942021-06-19 02:04:46 +00009479/**
9480 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9481 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9482 * cause slot be valid again.
9483 */
9484TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9485 addConfigurationProperty("touch.deviceType", "touchScreen");
9486 prepareDisplay(DISPLAY_ORIENTATION_0);
9487 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9488 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9489
9490 NotifyMotionArgs motionArgs;
9491
9492 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9493 // First finger down.
9494 processId(mapper, FIRST_TRACKING_ID);
9495 processPosition(mapper, x1, y1);
9496 processPressure(mapper, RAW_PRESSURE_MAX);
9497 processSync(mapper);
9498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9499 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9500 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9501
9502 // First finger move.
9503 processId(mapper, FIRST_TRACKING_ID);
9504 processPosition(mapper, x1 + 1, y1 + 1);
9505 processPressure(mapper, RAW_PRESSURE_MAX);
9506 processSync(mapper);
9507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9509 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9510
9511 // Second finger down.
9512 processSlot(mapper, SECOND_SLOT);
9513 processId(mapper, SECOND_TRACKING_ID);
9514 processPosition(mapper, x2, y2);
9515 processPressure(mapper, RAW_PRESSURE_MAX);
9516 processSync(mapper);
9517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009518 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009519 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9520
9521 // second finger up with some unexpected data.
9522 processSlot(mapper, SECOND_SLOT);
9523 processId(mapper, INVALID_TRACKING_ID);
9524 processPosition(mapper, x2, y2);
9525 processSync(mapper);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009527 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009528 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9529
9530 // first finger up with some unexpected data.
9531 processSlot(mapper, FIRST_SLOT);
9532 processId(mapper, INVALID_TRACKING_ID);
9533 processPosition(mapper, x2, y2);
9534 processPressure(mapper, RAW_PRESSURE_MAX);
9535 processSync(mapper);
9536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9537 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9538 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9539}
9540
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009541TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9542 addConfigurationProperty("touch.deviceType", "touchScreen");
9543 prepareDisplay(DISPLAY_ORIENTATION_0);
9544 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9545 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9546
9547 // First finger down.
9548 processId(mapper, FIRST_TRACKING_ID);
9549 processPosition(mapper, 100, 200);
9550 processPressure(mapper, RAW_PRESSURE_MAX);
9551 processSync(mapper);
9552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9553 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9554
9555 // Second finger down.
9556 processSlot(mapper, SECOND_SLOT);
9557 processId(mapper, SECOND_TRACKING_ID);
9558 processPosition(mapper, 300, 400);
9559 processPressure(mapper, RAW_PRESSURE_MAX);
9560 processSync(mapper);
9561 ASSERT_NO_FATAL_FAILURE(
9562 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9563
9564 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009565 // preserved. Resetting should cancel the ongoing gesture.
9566 resetMapper(mapper, ARBITRARY_TIME);
9567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9568 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009569
9570 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9571 // the existing touch state to generate a down event.
9572 processPosition(mapper, 301, 302);
9573 processSync(mapper);
9574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9575 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9577 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9578
9579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9580}
9581
9582TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9583 addConfigurationProperty("touch.deviceType", "touchScreen");
9584 prepareDisplay(DISPLAY_ORIENTATION_0);
9585 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9586 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9587
9588 // First finger touches down and releases.
9589 processId(mapper, FIRST_TRACKING_ID);
9590 processPosition(mapper, 100, 200);
9591 processPressure(mapper, RAW_PRESSURE_MAX);
9592 processSync(mapper);
9593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9594 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9595 processId(mapper, INVALID_TRACKING_ID);
9596 processSync(mapper);
9597 ASSERT_NO_FATAL_FAILURE(
9598 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9599
9600 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9601 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009602 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9604
9605 // Send an empty sync frame. Since there are no pointers, no events are generated.
9606 processSync(mapper);
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9608}
9609
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009610// --- MultiTouchInputMapperTest_ExternalDevice ---
9611
9612class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9613protected:
Chris Yea52ade12020-08-27 16:49:20 -07009614 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009615};
9616
9617/**
9618 * Expect fallback to internal viewport if device is external and external viewport is not present.
9619 */
9620TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9621 prepareAxes(POSITION);
9622 addConfigurationProperty("touch.deviceType", "touchScreen");
9623 prepareDisplay(DISPLAY_ORIENTATION_0);
9624 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9625
9626 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9627
9628 NotifyMotionArgs motionArgs;
9629
9630 // Expect the event to be sent to the internal viewport,
9631 // because an external viewport is not present.
9632 processPosition(mapper, 100, 100);
9633 processSync(mapper);
9634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9635 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9636
9637 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009638 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009639 processPosition(mapper, 100, 100);
9640 processSync(mapper);
9641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9642 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9643}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009644
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009645TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9646 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9647 std::shared_ptr<FakePointerController> fakePointerController =
9648 std::make_shared<FakePointerController>();
9649 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9650 fakePointerController->setPosition(0, 0);
9651 fakePointerController->setButtonState(0);
9652
9653 // prepare device and capture
9654 prepareDisplay(DISPLAY_ORIENTATION_0);
9655 prepareAxes(POSITION | ID | SLOT);
9656 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9657 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9658 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009659 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009660 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9661
9662 // captured touchpad should be a touchpad source
9663 NotifyDeviceResetArgs resetArgs;
9664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9665 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9666
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009667 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009668
9669 const InputDeviceInfo::MotionRange* relRangeX =
9670 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9671 ASSERT_NE(relRangeX, nullptr);
9672 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9673 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9674 const InputDeviceInfo::MotionRange* relRangeY =
9675 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9676 ASSERT_NE(relRangeY, nullptr);
9677 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9678 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9679
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009680 // run captured pointer tests - note that this is unscaled, so input listener events should be
9681 // identical to what the hardware sends (accounting for any
9682 // calibration).
9683 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009684 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009685 processId(mapper, 1);
9686 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9687 processKey(mapper, BTN_TOUCH, 1);
9688 processSync(mapper);
9689
9690 // expect coord[0] to contain initial location of touch 0
9691 NotifyMotionArgs args;
9692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9693 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9694 ASSERT_EQ(1U, args.pointerCount);
9695 ASSERT_EQ(0, args.pointerProperties[0].id);
9696 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9697 ASSERT_NO_FATAL_FAILURE(
9698 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9699
9700 // FINGER 1 DOWN
9701 processSlot(mapper, 1);
9702 processId(mapper, 2);
9703 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9704 processSync(mapper);
9705
9706 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009708 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009709 ASSERT_EQ(2U, args.pointerCount);
9710 ASSERT_EQ(0, args.pointerProperties[0].id);
9711 ASSERT_EQ(1, args.pointerProperties[1].id);
9712 ASSERT_NO_FATAL_FAILURE(
9713 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9714 ASSERT_NO_FATAL_FAILURE(
9715 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9716
9717 // FINGER 1 MOVE
9718 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9719 processSync(mapper);
9720
9721 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9722 // from move
9723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9725 ASSERT_NO_FATAL_FAILURE(
9726 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9727 ASSERT_NO_FATAL_FAILURE(
9728 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9729
9730 // FINGER 0 MOVE
9731 processSlot(mapper, 0);
9732 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9733 processSync(mapper);
9734
9735 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9738 ASSERT_NO_FATAL_FAILURE(
9739 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9740 ASSERT_NO_FATAL_FAILURE(
9741 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9742
9743 // BUTTON DOWN
9744 processKey(mapper, BTN_LEFT, 1);
9745 processSync(mapper);
9746
9747 // touchinputmapper design sends a move before button press
9748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9751 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9752
9753 // BUTTON UP
9754 processKey(mapper, BTN_LEFT, 0);
9755 processSync(mapper);
9756
9757 // touchinputmapper design sends a move after button release
9758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9759 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9762
9763 // FINGER 0 UP
9764 processId(mapper, -1);
9765 processSync(mapper);
9766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9767 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9768
9769 // FINGER 1 MOVE
9770 processSlot(mapper, 1);
9771 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9772 processSync(mapper);
9773
9774 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9777 ASSERT_EQ(1U, args.pointerCount);
9778 ASSERT_EQ(1, args.pointerProperties[0].id);
9779 ASSERT_NO_FATAL_FAILURE(
9780 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9781
9782 // FINGER 1 UP
9783 processId(mapper, -1);
9784 processKey(mapper, BTN_TOUCH, 0);
9785 processSync(mapper);
9786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9787 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9788
9789 // non captured touchpad should be a mouse source
9790 mFakePolicy->setPointerCapture(false);
9791 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9793 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9794}
9795
9796TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9797 std::shared_ptr<FakePointerController> fakePointerController =
9798 std::make_shared<FakePointerController>();
9799 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9800 fakePointerController->setPosition(0, 0);
9801 fakePointerController->setButtonState(0);
9802
9803 // prepare device and capture
9804 prepareDisplay(DISPLAY_ORIENTATION_0);
9805 prepareAxes(POSITION | ID | SLOT);
9806 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9807 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009808 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009809 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9810 // run uncaptured pointer tests - pushes out generic events
9811 // FINGER 0 DOWN
9812 processId(mapper, 3);
9813 processPosition(mapper, 100, 100);
9814 processKey(mapper, BTN_TOUCH, 1);
9815 processSync(mapper);
9816
9817 // start at (100,100), cursor should be at (0,0) * scale
9818 NotifyMotionArgs args;
9819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9820 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9821 ASSERT_NO_FATAL_FAILURE(
9822 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9823
9824 // FINGER 0 MOVE
9825 processPosition(mapper, 200, 200);
9826 processSync(mapper);
9827
9828 // compute scaling to help with touch position checking
9829 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9830 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9831 float scale =
9832 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9833
9834 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9838 0, 0, 0, 0, 0, 0, 0));
9839}
9840
9841TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9842 std::shared_ptr<FakePointerController> fakePointerController =
9843 std::make_shared<FakePointerController>();
9844
9845 prepareDisplay(DISPLAY_ORIENTATION_0);
9846 prepareAxes(POSITION | ID | SLOT);
9847 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009848 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009849 mFakePolicy->setPointerCapture(false);
9850 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9851
9852 // uncaptured touchpad should be a pointer device
9853 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9854
9855 // captured touchpad should be a touchpad device
9856 mFakePolicy->setPointerCapture(true);
9857 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9858 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9859}
9860
HQ Liue6983c72022-04-19 22:14:56 +00009861class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9862protected:
9863 float mPointerMovementScale;
9864 float mPointerXZoomScale;
9865 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9866 addConfigurationProperty("touch.deviceType", "pointer");
9867 std::shared_ptr<FakePointerController> fakePointerController =
9868 std::make_shared<FakePointerController>();
9869 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9870 fakePointerController->setPosition(0, 0);
9871 fakePointerController->setButtonState(0);
9872 prepareDisplay(DISPLAY_ORIENTATION_0);
9873
9874 prepareAxes(POSITION);
9875 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9876 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9877 // needs to be disabled, and the pointer gesture needs to be enabled.
9878 mFakePolicy->setPointerCapture(false);
9879 mFakePolicy->setPointerGestureEnabled(true);
9880 mFakePolicy->setPointerController(fakePointerController);
9881
9882 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9883 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9884 mPointerMovementScale =
9885 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9886 mPointerXZoomScale =
9887 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9888 }
9889
9890 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9891 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9892 /*flat*/ 0,
9893 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9894 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9895 /*flat*/ 0,
9896 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9897 }
9898};
9899
9900/**
9901 * Two fingers down on a pointer mode touch pad. The width
9902 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9903 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9904 * be greater than the both value to be freeform gesture, so that after two
9905 * fingers start to move downwards, the gesture should be swipe.
9906 */
9907TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9908 // The min freeform gesture width is 25units/mm x 30mm = 750
9909 // which is greater than fraction of the diagnal length of the touchpad (349).
9910 // Thus, MaxSwipWidth is 750.
9911 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9912 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9913 NotifyMotionArgs motionArgs;
9914
9915 // Two fingers down at once.
9916 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9917 // Pointer's initial position is used the [0,0] coordinate.
9918 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9919
9920 processId(mapper, FIRST_TRACKING_ID);
9921 processPosition(mapper, x1, y1);
9922 processMTSync(mapper);
9923 processId(mapper, SECOND_TRACKING_ID);
9924 processPosition(mapper, x2, y2);
9925 processMTSync(mapper);
9926 processSync(mapper);
9927
9928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9929 ASSERT_EQ(1U, motionArgs.pointerCount);
9930 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009932 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009933 ASSERT_NO_FATAL_FAILURE(
9934 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9935
9936 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9937 // that there should be 1 pointer.
9938 int32_t movingDistance = 200;
9939 y1 += movingDistance;
9940 y2 += movingDistance;
9941
9942 processId(mapper, FIRST_TRACKING_ID);
9943 processPosition(mapper, x1, y1);
9944 processMTSync(mapper);
9945 processId(mapper, SECOND_TRACKING_ID);
9946 processPosition(mapper, x2, y2);
9947 processMTSync(mapper);
9948 processSync(mapper);
9949
9950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9951 ASSERT_EQ(1U, motionArgs.pointerCount);
9952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009954 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9956 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9957 0, 0, 0, 0));
9958}
9959
9960/**
9961 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9962 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9963 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9964 * value to be freeform gesture, so that after two fingers start to move downwards,
9965 * the gesture should be swipe.
9966 */
9967TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9968 // The min freeform gesture width is 5units/mm x 30mm = 150
9969 // which is greater than fraction of the diagnal length of the touchpad (349).
9970 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9971 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9972 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9973 NotifyMotionArgs motionArgs;
9974
9975 // Two fingers down at once.
9976 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9977 // Pointer's initial position is used the [0,0] coordinate.
9978 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9979
9980 processId(mapper, FIRST_TRACKING_ID);
9981 processPosition(mapper, x1, y1);
9982 processMTSync(mapper);
9983 processId(mapper, SECOND_TRACKING_ID);
9984 processPosition(mapper, x2, y2);
9985 processMTSync(mapper);
9986 processSync(mapper);
9987
9988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9989 ASSERT_EQ(1U, motionArgs.pointerCount);
9990 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009992 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009993 ASSERT_NO_FATAL_FAILURE(
9994 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9995
9996 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9997 // and there should be 1 pointer.
9998 int32_t movingDistance = 200;
9999 y1 += movingDistance;
10000 y2 += movingDistance;
10001
10002 processId(mapper, FIRST_TRACKING_ID);
10003 processPosition(mapper, x1, y1);
10004 processMTSync(mapper);
10005 processId(mapper, SECOND_TRACKING_ID);
10006 processPosition(mapper, x2, y2);
10007 processMTSync(mapper);
10008 processSync(mapper);
10009
10010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10011 ASSERT_EQ(1U, motionArgs.pointerCount);
10012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010014 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010015 // New coordinate is the scaled relative coordinate from the initial coordinate.
10016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10017 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10018 0, 0, 0, 0));
10019}
10020
10021/**
10022 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10023 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10024 * freeform gestures after two fingers start to move downwards.
10025 */
10026TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10027 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10028 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10029
10030 NotifyMotionArgs motionArgs;
10031
10032 // Two fingers down at once. Wider than the max swipe width.
10033 // The gesture is expected to be PRESS, then transformed to FREEFORM
10034 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10035
10036 processId(mapper, FIRST_TRACKING_ID);
10037 processPosition(mapper, x1, y1);
10038 processMTSync(mapper);
10039 processId(mapper, SECOND_TRACKING_ID);
10040 processPosition(mapper, x2, y2);
10041 processMTSync(mapper);
10042 processSync(mapper);
10043
10044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10045 ASSERT_EQ(1U, motionArgs.pointerCount);
10046 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010048 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010049 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10050 ASSERT_NO_FATAL_FAILURE(
10051 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10052
10053 int32_t movingDistance = 200;
10054
10055 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10056 // then two down events for two pointers.
10057 y1 += movingDistance;
10058 y2 += movingDistance;
10059
10060 processId(mapper, FIRST_TRACKING_ID);
10061 processPosition(mapper, x1, y1);
10062 processMTSync(mapper);
10063 processId(mapper, SECOND_TRACKING_ID);
10064 processPosition(mapper, x2, y2);
10065 processMTSync(mapper);
10066 processSync(mapper);
10067
10068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10069 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10070 ASSERT_EQ(1U, motionArgs.pointerCount);
10071 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10074 ASSERT_EQ(1U, motionArgs.pointerCount);
10075 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010078 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010079 ASSERT_EQ(2U, motionArgs.pointerCount);
10080 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010082 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010083 // Two pointers' scaled relative coordinates from their initial centroid.
10084 // Initial y coordinates are 0 as y1 and y2 have the same value.
10085 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10086 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10087 // When pointers move, the new coordinates equal to the initial coordinates plus
10088 // scaled moving distance.
10089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10090 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10091 0, 0, 0, 0));
10092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10093 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10094 0, 0, 0, 0));
10095
10096 // Move two fingers down again, expect one MOVE motion event.
10097 y1 += movingDistance;
10098 y2 += movingDistance;
10099
10100 processId(mapper, FIRST_TRACKING_ID);
10101 processPosition(mapper, x1, y1);
10102 processMTSync(mapper);
10103 processId(mapper, SECOND_TRACKING_ID);
10104 processPosition(mapper, x2, y2);
10105 processMTSync(mapper);
10106 processSync(mapper);
10107
10108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10109 ASSERT_EQ(2U, motionArgs.pointerCount);
10110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010112 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10114 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10115 0, 0, 0, 0, 0));
10116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10117 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10118 0, 0, 0, 0, 0));
10119}
10120
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010121// --- JoystickInputMapperTest ---
10122
10123class JoystickInputMapperTest : public InputMapperTest {
10124protected:
10125 static const int32_t RAW_X_MIN;
10126 static const int32_t RAW_X_MAX;
10127 static const int32_t RAW_Y_MIN;
10128 static const int32_t RAW_Y_MAX;
10129
10130 void SetUp() override {
10131 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10132 }
10133 void prepareAxes() {
10134 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10136 }
10137
10138 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10140 }
10141
10142 void processSync(JoystickInputMapper& mapper) {
10143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10144 }
10145
10146 void prepareVirtualDisplay(int32_t orientation) {
10147 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10148 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10149 NO_PORT, ViewportType::VIRTUAL);
10150 }
10151};
10152
10153const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10154const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10155const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10156const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10157
10158TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10159 prepareAxes();
10160 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10161
10162 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10163
10164 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10165
10166 // Send an axis event
10167 processAxis(mapper, ABS_X, 100);
10168 processSync(mapper);
10169
10170 NotifyMotionArgs args;
10171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10172 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10173
10174 // Send another axis event
10175 processAxis(mapper, ABS_Y, 100);
10176 processSync(mapper);
10177
10178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10179 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10180}
10181
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010182// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010183
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010184class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010185protected:
10186 static const char* DEVICE_NAME;
10187 static const char* DEVICE_LOCATION;
10188 static const int32_t DEVICE_ID;
10189 static const int32_t DEVICE_GENERATION;
10190 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010191 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010192 static const int32_t EVENTHUB_ID;
10193
10194 std::shared_ptr<FakeEventHub> mFakeEventHub;
10195 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010196 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010197 std::unique_ptr<InstrumentedInputReader> mReader;
10198 std::shared_ptr<InputDevice> mDevice;
10199
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010200 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010201 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010202 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010203 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010204 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010205 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010206 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10207 }
10208
10209 void SetUp() override { SetUp(DEVICE_CLASSES); }
10210
10211 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010212 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010213 mFakePolicy.clear();
10214 }
10215
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010216 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010217 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10218 mReader->requestRefreshConfiguration(changes);
10219 mReader->loopOnce();
10220 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010221 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010222 }
10223
10224 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10225 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010226 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010227 InputDeviceIdentifier identifier;
10228 identifier.name = name;
10229 identifier.location = location;
10230 std::shared_ptr<InputDevice> device =
10231 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10232 identifier);
10233 mReader->pushNextDevice(device);
10234 mFakeEventHub->addDevice(eventHubId, name, classes);
10235 mReader->loopOnce();
10236 return device;
10237 }
10238
10239 template <class T, typename... Args>
10240 T& addControllerAndConfigure(Args... args) {
10241 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10242
10243 return controller;
10244 }
10245};
10246
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010247const char* PeripheralControllerTest::DEVICE_NAME = "device";
10248const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10249const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10250const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10251const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010252const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10253 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010254const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010255
10256// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010257class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010258protected:
10259 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010260 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010261 }
10262};
10263
10264TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010265 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010266
10267 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10268 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10269}
10270
10271TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010272 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010273
10274 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10275 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10276}
10277
10278// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010279class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010280protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010281 void SetUp() override {
10282 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10283 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010284};
10285
Chris Ye85758332021-05-16 23:05:17 -070010286TEST_F(LightControllerTest, MonoLight) {
10287 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010288 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010289 .maxBrightness = 255,
10290 .flags = InputLightClass::BRIGHTNESS,
10291 .path = ""};
10292 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010293
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010294 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010295 InputDeviceInfo info;
10296 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010297 std::vector<InputDeviceLightInfo> lights = info.getLights();
10298 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010299 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10300 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10301
10302 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10303 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10304}
10305
10306TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10307 RawLightInfo infoMono = {.id = 1,
10308 .name = "mono_keyboard_backlight",
10309 .maxBrightness = 255,
10310 .flags = InputLightClass::BRIGHTNESS |
10311 InputLightClass::KEYBOARD_BACKLIGHT,
10312 .path = ""};
10313 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10314
10315 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10316 InputDeviceInfo info;
10317 controller.populateDeviceInfo(&info);
10318 std::vector<InputDeviceLightInfo> lights = info.getLights();
10319 ASSERT_EQ(1U, lights.size());
10320 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10321 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010322
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010323 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10324 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010325}
10326
10327TEST_F(LightControllerTest, RGBLight) {
10328 RawLightInfo infoRed = {.id = 1,
10329 .name = "red",
10330 .maxBrightness = 255,
10331 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10332 .path = ""};
10333 RawLightInfo infoGreen = {.id = 2,
10334 .name = "green",
10335 .maxBrightness = 255,
10336 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10337 .path = ""};
10338 RawLightInfo infoBlue = {.id = 3,
10339 .name = "blue",
10340 .maxBrightness = 255,
10341 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10342 .path = ""};
10343 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10344 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10345 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10346
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010347 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010348 InputDeviceInfo info;
10349 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010350 std::vector<InputDeviceLightInfo> lights = info.getLights();
10351 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010352 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10353 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10354 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10355
10356 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10357 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10358}
10359
10360TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10361 RawLightInfo infoRed = {.id = 1,
10362 .name = "red_keyboard_backlight",
10363 .maxBrightness = 255,
10364 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10365 InputLightClass::KEYBOARD_BACKLIGHT,
10366 .path = ""};
10367 RawLightInfo infoGreen = {.id = 2,
10368 .name = "green_keyboard_backlight",
10369 .maxBrightness = 255,
10370 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10371 InputLightClass::KEYBOARD_BACKLIGHT,
10372 .path = ""};
10373 RawLightInfo infoBlue = {.id = 3,
10374 .name = "blue_keyboard_backlight",
10375 .maxBrightness = 255,
10376 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10377 InputLightClass::KEYBOARD_BACKLIGHT,
10378 .path = ""};
10379 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10380 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10381 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10382
10383 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10384 InputDeviceInfo info;
10385 controller.populateDeviceInfo(&info);
10386 std::vector<InputDeviceLightInfo> lights = info.getLights();
10387 ASSERT_EQ(1U, lights.size());
10388 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10389 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10390 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10391
10392 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10393 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10394}
10395
10396TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10397 RawLightInfo infoRed = {.id = 1,
10398 .name = "red",
10399 .maxBrightness = 255,
10400 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10401 .path = ""};
10402 RawLightInfo infoGreen = {.id = 2,
10403 .name = "green",
10404 .maxBrightness = 255,
10405 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10406 .path = ""};
10407 RawLightInfo infoBlue = {.id = 3,
10408 .name = "blue",
10409 .maxBrightness = 255,
10410 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10411 .path = ""};
10412 RawLightInfo infoGlobal = {.id = 3,
10413 .name = "global_keyboard_backlight",
10414 .maxBrightness = 255,
10415 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10416 InputLightClass::KEYBOARD_BACKLIGHT,
10417 .path = ""};
10418 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10419 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10420 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10421 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10422
10423 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10424 InputDeviceInfo info;
10425 controller.populateDeviceInfo(&info);
10426 std::vector<InputDeviceLightInfo> lights = info.getLights();
10427 ASSERT_EQ(1U, lights.size());
10428 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10429 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10430 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010431
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010432 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10433 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010434}
10435
10436TEST_F(LightControllerTest, MultiColorRGBLight) {
10437 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010438 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010439 .maxBrightness = 255,
10440 .flags = InputLightClass::BRIGHTNESS |
10441 InputLightClass::MULTI_INTENSITY |
10442 InputLightClass::MULTI_INDEX,
10443 .path = ""};
10444
10445 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10446
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010447 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010448 InputDeviceInfo info;
10449 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010450 std::vector<InputDeviceLightInfo> lights = info.getLights();
10451 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010452 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10453 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10454 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10455
10456 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10457 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10458}
10459
10460TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10461 RawLightInfo infoColor = {.id = 1,
10462 .name = "multi_color_keyboard_backlight",
10463 .maxBrightness = 255,
10464 .flags = InputLightClass::BRIGHTNESS |
10465 InputLightClass::MULTI_INTENSITY |
10466 InputLightClass::MULTI_INDEX |
10467 InputLightClass::KEYBOARD_BACKLIGHT,
10468 .path = ""};
10469
10470 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10471
10472 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10473 InputDeviceInfo info;
10474 controller.populateDeviceInfo(&info);
10475 std::vector<InputDeviceLightInfo> lights = info.getLights();
10476 ASSERT_EQ(1U, lights.size());
10477 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10478 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10479 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010480
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010481 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10482 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010483}
10484
10485TEST_F(LightControllerTest, PlayerIdLight) {
10486 RawLightInfo info1 = {.id = 1,
10487 .name = "player1",
10488 .maxBrightness = 255,
10489 .flags = InputLightClass::BRIGHTNESS,
10490 .path = ""};
10491 RawLightInfo info2 = {.id = 2,
10492 .name = "player2",
10493 .maxBrightness = 255,
10494 .flags = InputLightClass::BRIGHTNESS,
10495 .path = ""};
10496 RawLightInfo info3 = {.id = 3,
10497 .name = "player3",
10498 .maxBrightness = 255,
10499 .flags = InputLightClass::BRIGHTNESS,
10500 .path = ""};
10501 RawLightInfo info4 = {.id = 4,
10502 .name = "player4",
10503 .maxBrightness = 255,
10504 .flags = InputLightClass::BRIGHTNESS,
10505 .path = ""};
10506 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10507 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10508 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10509 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10510
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010511 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010512 InputDeviceInfo info;
10513 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010514 std::vector<InputDeviceLightInfo> lights = info.getLights();
10515 ASSERT_EQ(1U, lights.size());
10516 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010517 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10518 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010519
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010520 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10521 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10522 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010523}
10524
Michael Wrightd02c5b62014-02-10 15:10:22 -080010525} // namespace android